Macro dispatch_service

Source
macro_rules! dispatch_service {
    (
        $service:ty,
        $hash_by:ident,
        $(
            $method:ident ($request:ty) -> $reply:ty,
        )*
    ) => { ... };
}
Expand description

Define the service and build the mapping relationship between tonic network tasks and your business tasks.

Parameters:

  • $service Original service name. Because we need to generate new service name based on this name, so do not give the module prefix.

  • $hash_by The field in request types which is used to calculate which business task to dispatched to. All request types should contain this field.

  • $method The gRPC method name. You need list all methods.

  • $request The gRPC request type.

  • $reply The gRPC response type.

This macro defines 3 items:

  • trait DispatchBackend This defines all your service’s gRPC methods, and you need to implement this trait for your service context.

  • fn start_simple_dispatch_backend This starts a simple kind of backend tasks, which just listen on the request channel. If you want more complex backend task (e.g. listen on another channel too), you have to create tasks and channels youself.

  • struct [<$service DispatchServer>] This defines the real tonic service, and this macro implement it automatically. If you use the start_simple_dispatch_backend which handles this struct already, then you do not need to touch this. But if you need to build backend tasks yourself, then you need to create channels and this struct with their Sender ends by its with_txs() method. See start_simple_dispatch_backend()’s code for example.

Read the DictService example’s source code for a better understanding.