-
Notifications
You must be signed in to change notification settings - Fork 1k
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
fix(kad): preserve deadline in keep alive logic #3801
Conversation
3991197
to
7c84e94
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the patch!
protocols/kad/src/handler_priv.rs
Outdated
if self.outbound_substreams.is_empty() && self.inbound_substreams.is_empty() { | ||
// We destroyed all substreams in this function. | ||
self.keep_alive = KeepAlive::Until(Instant::now() + self.config.idle_timeout); | ||
self.keep_alive = if let KeepAlive::Until(time) = self.keep_alive { | ||
// Preserve the existing idle timeout | ||
KeepAlive::Until(time) | ||
} else { | ||
// Setup idle timeout | ||
KeepAlive::Until(Instant::now() + self.config.idle_timeout) | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functionality wise, this looks good to me. What do you think of decreasing the nesting?
- if self.outbound_substreams.is_empty() && self.inbound_substreams.is_empty() {
- // We destroyed all substreams in this function.
- self.keep_alive = KeepAlive::Until(Instant::now() + self.config.idle_timeout);
- } else {
- self.keep_alive = KeepAlive::Yes;
- }
+ let no_streams = self.outbound_substreams.is_empty() && self.inbound_substreams.is_empty();
+ self.keep_alive = match (no_streams, self.keep_alive) {
+ // No open streams. Preserve the existing idle timeout.
+ (true, k @ KeepAlive::Until(_)) => k,
+ // No open streams. Set idle timeout.
+ (true, _) => KeepAlive::Until(Instant::now() + self.config.idle_timeout),
+ // Keep alive for open streams.
+ (false, _) => KeepAlive::Yes,
+ };
Previous to this change if the ConnectionHandler::poll for kad was called more frequently than the connection idle timeout the timeout would continually be pushed further into the feature. After this change kad now will preserve the existing idle deadline.
7c84e94
to
20f25c8
Compare
@mxinden Thanks for the review. I agree I like the simpler logic. PR updated and rebased off master. |
I looked into the CI failures and they seem to be unrelated to the change. One is a clippy error for code I did not change. The other is a failed test in gossipsub. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the help @nathanielc! 🙏
Description
Previous to this change if the ConnectionHandler::poll for kad was called more frequently than the connection idle timeout the timeout would continually be pushed further into the future. After this change kad now will preserve the existing idle deadline.
Notes & open questions
How should I add tests?
Change checklist