Skip to content

Commit

Permalink
added idle_connection_timeout for swarm builder
Browse files Browse the repository at this point in the history
  • Loading branch information
startup-dreamer committed Jul 8, 2023
1 parent b6b8844 commit a0a5d44
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
23 changes: 20 additions & 3 deletions swarm/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ where

local_supported_protocols: HashSet<StreamProtocol>,
remote_supported_protocols: HashSet<StreamProtocol>,
idle_connection_timeout: Option<u64>, // Timeout in seconds
}

impl<THandler> fmt::Debug for Connection<THandler>
Expand Down Expand Up @@ -176,9 +177,9 @@ where
mut handler: THandler,
substream_upgrade_protocol_override: Option<upgrade::Version>,
max_negotiating_inbound_streams: usize,
idle_connection_timeout: Option<u64>, // Add this parameter
) -> Self {
let initial_protocols = gather_supported_protocols(&handler);

if !initial_protocols.is_empty() {
handler.on_connection_event(ConnectionEvent::LocalProtocolsChange(
ProtocolsChange::Added(ProtocolsAdded::from_set(&initial_protocols)),
Expand All @@ -196,6 +197,7 @@ where
requested_substreams: Default::default(),
local_supported_protocols: initial_protocols,
remote_supported_protocols: Default::default(),
idle_connection_timeout,
}
}

Expand Down Expand Up @@ -227,6 +229,7 @@ where
substream_upgrade_protocol_override,

Check failure on line 229 in swarm/src/connection.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection.rs
local_supported_protocols: supported_protocols,
remote_supported_protocols,
idle_connection_timeout
} = self.get_mut();

loop {
Expand Down Expand Up @@ -351,9 +354,19 @@ where
*shutdown = Shutdown::Later(Delay::new(dur), t)
}
}
(_, KeepAlive::No) => *shutdown = Shutdown::Asap,
(_, KeepAlive::No) => {
// handle idle_connection_timeout
let timeout = idle_connection_timeout.unwrap_or(0); // Default timeout is 0 seconds
if timeout > 0 {
let duration = Duration::from_secs(timeout);
let deadline = Instant::now() + duration;
*shutdown = Shutdown::Later(Delay::new(duration), deadline);
} else {
*shutdown = Shutdown::Asap;
}

Check failure on line 366 in swarm/src/connection.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection.rs
}
(_, KeepAlive::Yes) => *shutdown = Shutdown::None,
};
};

// Check if the connection (and handler) should be shut down.
// As long as we're still negotiating substreams, shutdown is always postponed.
Expand Down Expand Up @@ -713,6 +726,7 @@ mod tests {
keep_alive::ConnectionHandler,

Check failure on line 726 in swarm/src/connection.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection.rs
None,
max_negotiating_inbound_streams,
None
);

let result = connection.poll_noop_waker();
Expand All @@ -736,6 +750,7 @@ mod tests {
MockConnectionHandler::new(upgrade_timeout),

Check failure on line 750 in swarm/src/connection.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection.rs
None,
2,
None
);

connection.handler.open_new_outbound();
Expand All @@ -758,6 +773,7 @@ mod tests {
ConfigurableProtocolConnectionHandler::default(),

Check failure on line 773 in swarm/src/connection.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection.rs
None,
0,
None
);

// First, start listening on a single protocol.
Expand Down Expand Up @@ -796,6 +812,7 @@ mod tests {
ConfigurableProtocolConnectionHandler::default(),

Check failure on line 812 in swarm/src/connection.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection.rs
None,
0,
None
);

// First, remote supports a single protocol.
Expand Down
2 changes: 2 additions & 0 deletions swarm/src/connection/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,7 @@ where
endpoint: &ConnectedPoint,

Check failure on line 492 in swarm/src/connection/pool.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection/pool.rs
connection: NewConnection,
handler: THandler,
idle_connection_timeout: Option<u64>
) {
let connection = connection.extract();

Expand All @@ -518,6 +519,7 @@ where
handler,

Check failure on line 519 in swarm/src/connection/pool.rs

View workflow job for this annotation

GitHub Actions / rustfmt

Diff in /home/runner/work/rust-libp2p/rust-libp2p/swarm/src/connection/pool.rs
self.substream_upgrade_protocol_override,
self.max_negotiating_inbound_streams,
idle_connection_timeout
);

self.executor.spawn(task::new_for_established_connection(
Expand Down
2 changes: 1 addition & 1 deletion swarm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ where
.expect("n + 1 is always non-zero; qed");

self.pool
.spawn_connection(id, peer_id, &endpoint, connection, handler);
.spawn_connection(id, peer_id, &endpoint, connection, handler, None);

log::debug!(
"Connection established: {:?} {:?}; Total (peer): {}.",
Expand Down

0 comments on commit a0a5d44

Please sign in to comment.