ractor icon indicating copy to clipboard operation
ractor copied to clipboard

Feature request: utility to call the same actor with multiple messages

Open msparkles opened this issue 1 year ago • 8 comments

We have written up a (crude) utility function to send multiple messages to the same actor and receive the response with a mpsc channel.

This is mainly to minimize overhead.

Is it possible to include such feature in ractor?

Here's my implementation. One big flaw is that this requires making another message type for MpscSender instead of using RpcReplyPort

pub async fn call_multi<TMessage, TReply, TMsgBuilder>(
    actor: &ActorCell,
    msg_builder: TMsgBuilder,
    size: usize,
) -> Result<Vec<TReply>, MessagingErr<TMessage>>
where
    TMessage: Message,
    TMsgBuilder: FnOnce(MpscSender<TReply>) -> Vec<TMessage>,
{
    let (tx, mut rx) = concurrency::mpsc_bounded(size);
    let msgs = msg_builder(tx);

    for msg in msgs {
        actor.send_message::<TMessage>(msg)?;
    }

    let mut replies = Vec::with_capacity(size);

    // wait for the reply
    while let Some(result) = rx.recv().await {
        replies.push(result);
    }

    Ok(replies)
}

msparkles avatar May 12 '23 01:05 msparkles