Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(relay): remove KeepAlive::Until #4676

Merged
merged 8 commits into from
Oct 20, 2023
Merged
2 changes: 1 addition & 1 deletion protocols/relay/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<!-- Internal changes

- Allow deprecated usage of `KeepAlive::Until`
- Remove usage of `KeepAlive::Until`

-->

Expand Down
29 changes: 17 additions & 12 deletions protocols/relay/src/behaviour/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,8 @@ pub struct Handler {
>,
>,

/// Until when to keep the connection alive.
keep_alive: KeepAlive,
/// The point in time when this connection started idleing.
idle_at: Option<Instant>,

/// Future handling inbound reservation request.
reservation_request_future: Option<ReservationRequestFuture>,
Expand Down Expand Up @@ -411,13 +411,13 @@ impl Handler {
config,
queued_events: Default::default(),
pending_error: Default::default(),
idle_at: None,
reservation_request_future: Default::default(),
circuit_accept_futures: Default::default(),
circuit_deny_futures: Default::default(),
alive_lend_out_substreams: Default::default(),
circuits: Default::default(),
active_reservation: Default::default(),
keep_alive: KeepAlive::Yes,
pending_connect_requests: Default::default(),
}
}
Expand Down Expand Up @@ -616,7 +616,17 @@ impl ConnectionHandler for Handler {
}

fn connection_keep_alive(&self) -> KeepAlive {
self.keep_alive
// Only inbound connections need to be kept alive longer than they are active.
if self.endpoint.is_dialer() {
return KeepAlive::No;
}
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved
thomaseizinger marked this conversation as resolved.
Show resolved Hide resolved

match self.idle_at {
Some(idle_at) if Instant::now().duration_since(idle_at) > Duration::from_secs(10) => {
KeepAlive::No
}
_ => KeepAlive::Yes,
}
}

fn poll(
Expand Down Expand Up @@ -883,16 +893,11 @@ impl ConnectionHandler for Handler {
&& self.circuits.is_empty()
&& self.active_reservation.is_none()
{
#[allow(deprecated)]
match self.keep_alive {
KeepAlive::Yes => {
self.keep_alive = KeepAlive::Until(Instant::now() + Duration::from_secs(10));
}
KeepAlive::Until(_) => {}
KeepAlive::No => panic!("Handler never sets KeepAlive::No."),
if self.idle_at.is_none() {
self.idle_at = Some(Instant::now());
}
} else {
self.keep_alive = KeepAlive::Yes;
self.idle_at = None;
}

Poll::Pending
Expand Down