MessageDispatcher

Trait MessageDispatcher 

Source
pub trait MessageDispatcher:
    Send
    + Sync
    + 'static {
    type Workload: Workload<Dispatcher = Self>;

    // Required method
    fn dispatch<'life0, 'life1, 'async_trait, C>(
        workload: &'life0 Self::Workload,
        envelope: RpcEnvelope,
        ctx: &'life1 C,
    ) -> Pin<Box<dyn Future<Output = ActorResult<Bytes>> + Send + 'async_trait>>
       where C: 'async_trait + Context,
             Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;
}
Expand description

MessageDispatcher - Message dispatcher interface

Responsible for routing incoming message envelopes to corresponding handler methods.

§Design Characteristics

  • Static routing: Implemented via compile-time match statements, no runtime lookup overhead
  • Zero-sized type (ZST): Router itself occupies no memory
  • Code generation: Implementation is auto-generated by actr-cli codegen

§Code Generation Example

// Generated by code generator
pub struct EchoServiceRouter<T: EchoServiceHandler>(PhantomData<T>);

#[async_trait]
impl<T: EchoServiceHandler> MessageDispatcher for EchoServiceRouter<T> {
    type Workload = EchoServiceWorkload<T>;

    async fn dispatch<C: Context>(
        workload: &Self::Workload,
        envelope: RpcEnvelope,
        ctx: &C,
    ) -> ActorResult<Bytes> {
        match envelope.route_key.as_str() {
            "echo.EchoService.Echo" => {
                let req = EchoRequest::decode(&*envelope.payload)?;
                let resp = workload.0.echo(req, ctx).await?;
                Ok(resp.encode_to_vec().into())
            }
            _ => Err(ProtocolError::Actr(ActrError::UnknownRoute {
                route_key: envelope.route_key.to_string()
            }))
        }
    }
}

Required Associated Types§

Source

type Workload: Workload<Dispatcher = Self>

Associated Workload type

Required Methods§

Source

fn dispatch<'life0, 'life1, 'async_trait, C>( workload: &'life0 Self::Workload, envelope: RpcEnvelope, ctx: &'life1 C, ) -> Pin<Box<dyn Future<Output = ActorResult<Bytes>> + Send + 'async_trait>>
where C: 'async_trait + Context, Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Dispatch message to corresponding handler method and execute full request-response cycle

§Responsibilities
  1. Routing: Select handler method based on envelope.route_key
  2. Decoding: Deserialize payload into concrete message type
  3. Dispatching: Invoke workload’s business logic method
  4. Encoding: Serialize response into bytes
§Parameters
  • workload: Workload instance (contains user business logic)
  • envelope: Message envelope (contains route_key and payload)
  • ctx: Execution context (generic, supports any Context implementation)
§Returns

Returns serialized response bytes

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§