cronback_lib/clients/
dispatcher_client.rs1use std::collections::HashMap;
2
3use async_trait::async_trait;
4use derive_more::{Deref, DerefMut};
5use proto::dispatcher_proto::dispatcher_client::DispatcherClient as GenDispatcherClient;
6use tonic::codegen::InterceptedService;
7
8use crate::config::MainConfig;
9use crate::grpc_client_provider::{GrpcClientType, ScopedGrpcClient};
10use crate::grpc_helpers::GrpcRequestInterceptor;
11use crate::prelude::ValidShardedId;
12use crate::types::{ProjectId, RequestId};
13
14type DispatcherClient = GenDispatcherClient<
15 InterceptedService<tonic::transport::Channel, GrpcRequestInterceptor>,
16>;
17
18#[derive(Deref, DerefMut)]
19pub struct ScopedDispatcherClient(ScopedGrpcClient<DispatcherClient>);
20
21#[async_trait]
22impl GrpcClientType for ScopedDispatcherClient {
23 type RawGrpcClient = DispatcherClient;
24
25 fn get_mut(&mut self) -> &mut ScopedGrpcClient<Self::RawGrpcClient> {
26 &mut self.0
27 }
28
29 fn address_map(config: &MainConfig) -> &HashMap<u64, String> {
30 &config.dispatcher_cell_map
31 }
32
33 fn create_scoped_client(
34 project_id: ValidShardedId<ProjectId>,
35 request_id: RequestId,
36 channel: tonic::transport::Channel,
37 interceptor: GrpcRequestInterceptor,
38 ) -> Self {
39 let client =
40 GenDispatcherClient::with_interceptor(channel, interceptor);
41
42 Self(ScopedGrpcClient::new(project_id, request_id, client))
43 }
44}