diff --git a/services/tunnelbroker/src/constants.rs b/services/tunnelbroker/src/constants.rs --- a/services/tunnelbroker/src/constants.rs +++ b/services/tunnelbroker/src/constants.rs @@ -1,3 +1,6 @@ +use tokio::time::Duration; + pub const GRPC_TX_QUEUE_SIZE: usize = 32; -pub const GRPC_PING_INTERVAL_MS: u64 = 3000; pub const GRPC_SERVER_PORT: u64 = 50051; +pub const GRPC_KEEP_ALIVE_PING_INTERVAL: Duration = Duration::from_secs(3); +pub const GRPC_KEEP_ALIVE_PING_TIMEOUT: Duration = Duration::from_secs(10); diff --git a/services/tunnelbroker/src/server/mod.rs b/services/tunnelbroker/src/server/mod.rs --- a/services/tunnelbroker/src/server/mod.rs +++ b/services/tunnelbroker/src/server/mod.rs @@ -11,7 +11,6 @@ use futures::Stream; use std::pin::Pin; use tokio::sync::mpsc; -use tokio::time::{sleep, Duration}; use tokio_stream::{wrappers::ReceiverStream, StreamExt}; use tonic::{transport::Server, Request, Response, Status, Streaming}; use tracing::{debug, error}; @@ -195,29 +194,6 @@ }; } - // Spawning asynchronous Tokio task with the client pinging loop inside to - // make sure that the client is online - tokio::spawn({ - let session_id = session_id.clone(); - let tx = tx.clone(); - async move { - loop { - sleep(Duration::from_millis(constants::GRPC_PING_INTERVAL_MS)).await; - let result = tx_writer( - &session_id, - &tx, - Ok(tunnelbroker::MessageToClient { - data: Some(tunnelbroker::message_to_client::Data::Ping(())), - }), - ); - if let Err(err) = result.await { - debug!("Failed to write ping to a channel: {}", err); - break; - }; - } - } - }); - // Spawning asynchronous Tokio task to deliver new messages // to the client from delivery broker tokio::spawn({ @@ -373,6 +349,8 @@ pub async fn run_grpc_server() -> Result<()> { let addr = format!("[::1]:{}", constants::GRPC_SERVER_PORT).parse()?; Server::builder() + .http2_keepalive_interval(Some(constants::GRPC_KEEP_ALIVE_PING_INTERVAL)) + .http2_keepalive_timeout(Some(constants::GRPC_KEEP_ALIVE_PING_TIMEOUT)) .add_service(TunnelbrokerServiceServer::new( TunnelbrokerServiceHandlers::default(), ))