macro_rules! dispatch_service_async {
(
$service:ty,
$hash_by:ident : $hash_type:ty,
[ $(
$shard_mutable_method:ident ($shard_mutable_request:ty) -> $shard_mutable_reply:ty,
)* ],
[ $(
$shard_readonly_method:ident ($shard_readonly_request:ty) -> $shard_readonly_reply:ty,
)* ],
[ $(
$item_mutable_method:ident ($item_mutable_request:ty) -> $item_mutable_reply:ty,
)* ],
[ $(
$item_readonly_method:ident ($item_readonly_request:ty) -> $item_readonly_reply:ty,
)* ]
) => { ... };
}Expand description
Define the service and build the mapping relationship between tonic network tasks and your asynchronous business tasks.
Use dispatch_service_sync! instead for synchronous mode.
See the module-level documentation for more information
about the 2 modes.
Parameters:
-
$serviceOriginal service name. Because we need to generate new service name based on this name, so do not give the module prefix. -
$hash_by: $hash_typeThe field in request types which is used to calculate which business task to dispatched to. All request types should contain this field. -
$shard_mutable_method ($shard_mutable_request) -> $shard_mutable_replygRPC methods that work on mutable shard (but not on item). E.g. create or remove items on the shard. -
$shard_readonly_method ($shard_readonly_request) -> $shard_readonly_replygRPC methods that work on readonly shard (but not on item). E.g. list items on the shard. -
$item_mutable_method ($item_mutable_request) -> $item_mutable_replygRPC mutable methods that work on item. E.g. update item itself. -
$item_readonly_method ($item_readonly_request) -> $item_readonly_replygRPC readonly methods that work on item. E.g. query item itself.
This macro defines 4 items:
-
trait DispatchBackendShardis for each backend shard. You need to implement this trait for your shard context. It has 2 parts:- associated type Item, and get_item/get_item_mut methods;
- gRPC methods that works at shard (but not item), e.g. create/delete.
-
trait DispatchBackendItemis for each backend item. It has mutable and readonly gRPC methods that works at item. You need to implement this trait for your item.The formats of all methods are similar to the original tonic ones, except that changes
- self: from
&selfto&mut self(for mutable methods) - parameter: from
Request<R>toR - retuen value: from
Response<R>toR
However the meaning of
selfchanges. For the original tonic methods, theselfpoints to a global service context. While here, for shard methods theselfpoints to a context for each shard, and for item mutable/readonly methods theselfpoints to the item. - self: from
-
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.