apache_dubbo/protocol/grpc/
grpc_invoker.rs1use std::sync::Once;
19
20use tonic::client::Grpc;
21use tonic::transport::Channel;
22use tonic::transport::Endpoint;
23
24use crate::common::url::Url;
25use crate::invocation::{Request, Response};
26use crate::protocol::Invoker;
27
28pub struct GrpcInvoker {
29 client: Grpc<Channel>,
30 url: Url,
31 once: Once,
32}
33
34impl GrpcInvoker {
35 pub fn new(url: Url) -> GrpcInvoker {
36 let endpoint = Endpoint::new(url.to_url()).unwrap();
37 let conn = endpoint.connect_lazy();
38 Self {
39 url,
40 client: Grpc::new(conn),
41 once: Once::new(),
42 }
43 }
44}
45
46impl Invoker for GrpcInvoker {
47 fn is_available(&self) -> bool {
48 true
49 }
50
51 fn destroy(&self) {
52 self.once.call_once(|| println!("destroy..."))
53 }
54
55 fn get_url(&self) -> Url {
56 self.url.to_owned()
57 }
58
59 fn invoke<M1>(&self, req: Request<M1>) -> Response<String>
61 where
62 M1: Send + 'static,
63 {
64 let (metadata, _) = req.into_parts();
65
66 let resp = Response::new("string");
67 let (_resp_meta, msg) = resp.into_parts();
68
69 Response::from_parts(metadata, msg.to_string())
70 }
71}
72
73impl Clone for GrpcInvoker {
74 fn clone(&self) -> Self {
75 Self {
76 client: self.client.clone(),
77 url: self.url.clone(),
78 once: Once::new(),
79 }
80 }
81}