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

fix(kad): preserve deadline in keep alive logic #3801

Merged
merged 3 commits into from
Apr 24, 2023

Conversation

nathanielc
Copy link
Contributor

@nathanielc nathanielc commented Apr 18, 2023

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

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • I have added tests that prove my fix is effective or that my feature works
  • A changelog entry has been made in the appropriate crates

@nathanielc nathanielc changed the title fix(protocols/kad): update KeepAlive::Until to preserve existing deadline fix(kad): preserve deadline in keep alive logic Apr 18, 2023
Copy link
Member

@mxinden mxinden left a 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!

Comment on lines 760 to 768
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)
};
Copy link
Member

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.
@nathanielc
Copy link
Contributor Author

@mxinden Thanks for the review. I agree I like the simpler logic. PR updated and rebased off master.

@nathanielc
Copy link
Contributor Author

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.

Copy link
Member

@mxinden mxinden left a 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! 🙏

@mergify mergify bot merged commit 1f50809 into libp2p:master Apr 24, 2023
@nathanielc nathanielc deleted the fix/kad-keep-alive branch April 27, 2023 13:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants