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:
-
$serviceOriginal service name. Because we need to generate new service name based on this name, so do not give the module prefix. -
$hash_byThe field in request types which is used to calculate which business task to dispatched to. All request types should contain this field. -
$methodThe gRPC method name. You need list all methods. -
$requestThe gRPC request type. -
$replyThe gRPC response type.
This macro defines 3 items:
-
trait DispatchBackendThis defines all your service’s gRPC methods, and you need to implement this trait for your service context. -
fn start_simple_dispatch_backendThis 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 thestart_simple_dispatch_backendwhich 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 theirSenderends by itswith_txs()method. Seestart_simple_dispatch_backend()’s code for example.
Read the DictService example’s source code for a better understanding.