Skip to content

Commit

Permalink
Add an option to enable TCP_NODELAY (#435)
Browse files Browse the repository at this point in the history
  • Loading branch information
Patryk27 authored Jan 2, 2025
1 parent 912b939 commit 66f9416
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
1 change: 1 addition & 0 deletions russh/examples/ratatui_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ impl AppServer {
keys: vec![
russh_keys::PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap(),
],
nodelay: true,
..Default::default()
};

Expand Down
1 change: 1 addition & 0 deletions russh/examples/ratatui_shared_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ impl AppServer {
keys: vec![
russh_keys::PrivateKey::random(&mut OsRng, ssh_key::Algorithm::Ed25519).unwrap(),
],
nodelay: true,
..Default::default()
};

Expand Down
15 changes: 14 additions & 1 deletion russh/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::task::{Context, Poll};
use async_trait::async_trait;
use bytes::Bytes;
use futures::future::Future;
use log::{debug, error};
use log::{debug, error, warn};
use russh_keys::map_err;
use russh_util::runtime::JoinHandle;
use ssh_key::{Certificate, PrivateKey};
Expand Down Expand Up @@ -91,6 +91,8 @@ pub struct Config {
pub keepalive_interval: Option<std::time::Duration>,
/// If this many keepalives have been sent without reply, close the connection.
pub keepalive_max: usize,
/// If active, invoke `set_nodelay(true)` on client sockets; disabled by default (i.e. Nagle's algorithm is active).
pub nodelay: bool,
}

impl Default for Config {
Expand All @@ -115,6 +117,7 @@ impl Default for Config {
inactivity_timeout: Some(std::time::Duration::from_secs(600)),
keepalive_interval: None,
keepalive_max: 3,
nodelay: false,
}
}
}
Expand Down Expand Up @@ -757,7 +760,14 @@ pub trait Server {
let config = config.clone();
let handler = self.new_client(socket.peer_addr().ok());
let error_tx = error_tx.clone();

russh_util::runtime::spawn(async move {
if config.nodelay {
if let Err(e) = socket.set_nodelay(true) {
warn!("set_nodelay() failed: {e:?}");
}
}

let session = match run_stream(config, socket, handler).await {
Ok(s) => s,
Err(e) => {
Expand All @@ -766,6 +776,7 @@ pub trait Server {
return
}
};

match session.await {
Ok(_) => debug!("Connection closed"),
Err(e) => {
Expand All @@ -775,9 +786,11 @@ pub trait Server {
}
});
}

_ => break,
}
},

Some(error) = error_rx.recv() => {
self.handle_session_error(error);
}
Expand Down

0 comments on commit 66f9416

Please sign in to comment.