Skip to content

Commit

Permalink
feat(s2n-quic-dc): accept linger parameter instead of always setting it
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft committed Feb 17, 2025
1 parent 00e3371 commit 215dc72
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 6 deletions.
8 changes: 6 additions & 2 deletions dc/s2n-quic-dc/src/stream/client/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
socket::Protocol,
},
};
use std::{io, net::SocketAddr};
use std::{io, net::SocketAddr, time::Duration};
use tokio::net::TcpStream;

/// Connects using the UDP transport layer
Expand Down Expand Up @@ -54,6 +54,7 @@ pub async fn connect_tcp<H, Sub>(
acceptor_addr: SocketAddr,
env: &Environment<Sub>,
subscriber: Sub,
linger: Option<Duration>,
) -> io::Result<Stream<Sub>>
where
H: core::future::Future<Output = io::Result<secret::map::Peer>>,
Expand All @@ -64,7 +65,10 @@ where

// Make sure TCP_NODELAY is set
let _ = socket.set_nodelay(true);
let _ = socket.set_linger(Some(core::time::Duration::ZERO));

if linger.is_some() {
let _ = socket.set_linger(linger);
}

// if the acceptor_ip isn't known, then ask the socket to resolve it for us
let peer_addr = if acceptor_addr.ip().is_unspecified() {
Expand Down
5 changes: 5 additions & 0 deletions dc/s2n-quic-dc/src/stream/server/tokio/tcp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use crate::{
};
use core::{future::poll_fn, task::Poll};
use s2n_quic_core::{inet::SocketAddress, time::Clock};
use std::time::Duration;
use tokio::net::TcpListener;
use tracing::debug;

Expand All @@ -26,6 +27,7 @@ where
secrets: secret::Map,
backlog: usize,
accept_flavor: accept::Flavor,
linger: Option<Duration>,
subscriber: Sub,
}

Expand All @@ -42,6 +44,7 @@ where
secrets: &secret::Map,
backlog: usize,
accept_flavor: accept::Flavor,
linger: Option<Duration>,
subscriber: Sub,
) -> Self {
let acceptor = Self {
Expand All @@ -51,6 +54,7 @@ where
secrets: secrets.clone(),
backlog,
accept_flavor,
linger,
subscriber,
};

Expand Down Expand Up @@ -98,6 +102,7 @@ where
workers.insert(
remote_address,
socket,
self.linger,
&mut context,
subscriber_ctx,
&publisher,
Expand Down
3 changes: 3 additions & 0 deletions dc/s2n-quic-dc/src/stream/server/tokio/tcp/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ where
&mut self,
remote_address: SocketAddress,
stream: W::Stream,
linger: Option<Duration>,
cx: &mut W::Context,
connection_context: W::ConnectionContext,
publisher: &Pub,
Expand All @@ -179,6 +180,7 @@ where
self.inner.workers[idx].worker.replace(
remote_address,
stream,
linger,
connection_context,
publisher,
clock,
Expand Down Expand Up @@ -377,6 +379,7 @@ pub trait Worker {
&mut self,
remote_address: SocketAddress,
stream: Self::Stream,
linger: Option<Duration>,
connection_context: Self::ConnectionContext,
publisher: &Pub,
clock: &C,
Expand Down
2 changes: 2 additions & 0 deletions dc/s2n-quic-dc/src/stream/server/tokio/tcp/manager/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ impl super::Worker for Worker {
&mut self,
_remote_address: SocketAddress,
_stream: Self::Stream,
_linger: Option<Duration>,
_connection_context: Self::ConnectionContext,
_publisher: &Pub,
clock: &C,
Expand Down Expand Up @@ -160,6 +161,7 @@ impl Harness {
self.manager.insert(
SocketAddress::default(),
(),
None,
&mut (),
(),
&publisher(&self.subscriber, &self.clock),
Expand Down
6 changes: 5 additions & 1 deletion dc/s2n-quic-dc/src/stream/server/tokio/tcp/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ where
&mut self,
remote_address: SocketAddress,
stream: TcpStream,
linger: Option<Duration>,
subscriber_ctx: Self::ConnectionContext,
publisher: &Pub,
clock: &C,
Expand All @@ -107,7 +108,10 @@ where
{
// Make sure TCP_NODELAY is set
let _ = stream.set_nodelay(true);
let _ = stream.set_linger(Some(Duration::ZERO));

if linger.is_some() {
let _ = stream.set_linger(linger);
}

let now = clock.get_time();

Expand Down
12 changes: 9 additions & 3 deletions dc/s2n-quic-dc/src/stream/testing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,14 @@ impl Client {

match server.protocol {
Protocol::Tcp => {
stream_client::connect_tcp(handshake, server.local_addr, &self.env, subscriber)
.await
stream_client::connect_tcp(
handshake,
server.local_addr,
&self.env,
subscriber,
None,
)
.await
}
Protocol::Udp => {
stream_client::connect_udp(handshake, server.local_addr, &self.env, subscriber)
Expand Down Expand Up @@ -291,7 +297,7 @@ pub mod server {
let socket = tokio::net::TcpListener::from_std(socket).unwrap();

let acceptor = stream_server::tcp::Acceptor::new(
0, socket, &sender, &env, &map, backlog, flavor, subscriber,
0, socket, &sender, &env, &map, backlog, flavor, None, subscriber,
);
let acceptor = drop_handle_receiver.wrap(acceptor.run());
let acceptor = acceptor.instrument(tracing::info_span!("tcp"));
Expand Down

0 comments on commit 215dc72

Please sign in to comment.