i use tonic grpc now, and this is server code, when a receive a grpc call(for example heart_beat request), it automatically execute async fn heart_beat . and i want save the request to a struct , so some other function can use it. but i don't how to borrow a struct obj to tonic grpc server function.
tonic grpc
#[tonic::async_trait]
impl a for AService{
async fn heart_beat(
&self,
request: Request<HeartBeatRequest>
) -> Result<Response<HeartBeatResponse>,Status> {
println!("heart beat request: {:#?}", request);
let _req = request.into_inner();
// want to save _req (String) to b's Member variable
// question is how to borrow b to here??? should use global variable?
??????????????????????????????????
let _response = HeartBeatResponse{
msg: format!("User is old!").into()
};
Ok(Response::new(_response))
}
}
// main func is here
#[tokio::main]
async fn main()-> Result<(), Box<dyn std::error::Error>>{
let addr:SocketAddr = "[::1]:50050".parse()?;
let abc:AService = AService::default();
// declare a struct B here
b:B = B::new();
Server::builder()
.add_service(AServer::new(abc))
.serve(addr)
.await?;
Ok(())
}
pub struct B {
a:HashMap<i32,String>,
b:HashMap<i32,String>,
c:String
}
if use lazy_static, there is another problem:
lazy_static! {
static ref bb: Mutex<B> = Mutex::new({
let _tt = B::new();
_tt
});
}
the compiler notice that struct B did not implement sync, so i need to implement sync trait?