How to change mutable state on Server? #1094
Answered
by
davidpdrsn
Cosmoflips
asked this question in
Q&A
-
I have a mutable state on Server and i want it change according to the request from Client.But i find it can't refer self as #[derive(Clone,Debug, Default)]
pub struct ServerBuffer {
node:Arc<Mutex<Node>>,
message:String
}
#[tonic::async_trait]
impl AppendEntries for ServerBuffer {
async fn request(
&self,
request: Request<AppendEntriesRequest>,
) -> Result<Response<AppendEntriesReply>, Status> {
let message = request.metadata().get("message");
match message{
Some(message)=>{
println!("AppendEntriesRequest: {:?}", message.to_str());
self.message = message.to_str().unwrap().to_string();
self.node.lock().await.read(self.message.clone());
}
None=>{println!("No message in Metadata!");}
}
let reply = raft_rpc::AppendEntriesReply {
term: request.into_inner().term.into(),
success: true
};
Ok(Response::new(reply))
}
} Is there any way to achieve such result? |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Sep 29, 2022
Replies: 1 comment 2 replies
-
let mut node = self.node.lock().await; |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
Cosmoflips
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
lock
gives you a mutable reference even through a&self