Skip to content

Commit

Permalink
Optimize some code in async server.rs
Browse files Browse the repository at this point in the history
1、Modify the unit of measurement for timeout
2、Add clearly error message
3、Add Check if it is already shutdown no need select in handle_msg
  • Loading branch information
jokemanfire committed Nov 6, 2024
1 parent 0210c7a commit ef0d3c2
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/asynchronous/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ use crate::r#async::stream::{
use crate::r#async::utils;
use crate::r#async::{MethodHandler, StreamHandler, TtrpcContext};

const DEFAULT_CONN_SHUTDOWN_TIMEOUT: Duration = Duration::from_millis(5000);
const DEFAULT_SERVER_SHUTDOWN_TIMEOUT: Duration = Duration::from_millis(10000);
const DEFAULT_CONN_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
const DEFAULT_SERVER_SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(10);

pub struct Service {
pub methods: HashMap<String, Box<dyn MethodHandler + Send + Sync>>,
Expand Down Expand Up @@ -202,7 +202,7 @@ impl Server {
).await;
}
Err(e) => {
error!("{:?}", e)
error!("incoming conn fail {:?}", e)
}
}

Expand Down Expand Up @@ -381,14 +381,17 @@ impl ReaderDelegate for ServerReader {
async fn handle_msg(&self, msg: GenMessage) {
let handler_shutdown_waiter = self.handler_shutdown.subscribe();
let context = self.context();
let (wait_tx, wait_rx) = tokio::sync::oneshot::channel::<()>();
spawn(async move {
select! {
_ = context.handle_msg(msg, wait_tx) => {}
_ = handler_shutdown_waiter.wait_shutdown() => {}
}
});
wait_rx.await.unwrap_or_default();
//Check if it is already shutdown no need select wait
if !handler_shutdown_waiter.is_shutdown(){
let (wait_tx, wait_rx) = tokio::sync::oneshot::channel::<()>();
spawn(async move {
select! {
_ = context.handle_msg(msg, wait_tx) => {}
_ = handler_shutdown_waiter.wait_shutdown() => {}
}
});
wait_rx.await.unwrap_or_default();
}
}

async fn handle_err(&self, header: MessageHeader, e: Error) {
Expand Down

0 comments on commit ef0d3c2

Please sign in to comment.