swarm/one_shot: Initialize handler with KeepAlive::Until#1698
Merged
romanb merged 5 commits intolibp2p:masterfrom Aug 13, 2020
Merged
swarm/one_shot: Initialize handler with KeepAlive::Until#1698romanb merged 5 commits intolibp2p:masterfrom
romanb merged 5 commits intolibp2p:masterfrom
Conversation
A `OneShotHandler` without any ongoing requests should not keep the underlying connection alive indefinitely.
The `OneShotHandler` `keep_alive` property is altered on incoming and outgoing reqeusts. By default it is initialized in `KeepAlive::Yes`. In case there are no incoming or outgoing requests happening, this state is never changed and thus the handler keeps the underlying connection alive indefinitely. With this commit the handler is initialized with `KeepAlive::Until`. As before the `keep_alive` timer is updated on incoming requests and set to `KeepAlive::Yes` on outgoing requests.
romanb
reviewed
Aug 13, 2020
| dial_negotiated: 0, | ||
| max_dial_negotiated: 8, | ||
| keep_alive: KeepAlive::Yes, | ||
| keep_alive: KeepAlive::Until(Instant::now() + config.keep_alive_timeout), |
Contributor
There was a problem hiding this comment.
It may be better to just move this condition from inject_fully_negotiated_outbound
if self.dial_negotiated == 0 && self.dial_queue.is_empty() {
self.keep_alive = KeepAlive::Until(Instant::now() + self.config.keep_alive_timeout);
}into poll()
...
if !self.dial_queue.is_empty() {
...
} else {
self.dial_queue.shrink_to_fit();
if self.keep_alive.is_yes() && self.dial_negotiated == 0 {
self.keep_alive = KeepAlive::Until(Instant::now() + self.config.keep_alive_timeout);
}
}The reason being that starting timers on creation of connection handlers is mildly problematic, because at least right now, handlers are created before the connection is even established, whereas a connection handler is poll()ed as soon as it is established and it gives the handler a chance to "make its first step" before the timeout starts (if there is indeed nothing that the handler wishes to do, as determined by poll()).
A `ProtocolsHandler` can be created before the underlying connection is established. Thus setting a keep alive timeout might be problematic. Instead set `keep_alive` to `Yes` at construction and alter it within `ProtocolsHandler::poll`.
Member
Author
|
Thanks @romanb for pointing out the early initialization issue! Could you take another look? |
romanb
approved these changes
Aug 13, 2020
This was referenced Aug 14, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
OneShotHandlerkeep_aliveproperty is altered on incoming andoutgoing reqeusts. By default it is initialized in
KeepAlive::Yes. Incase there are no incoming or outgoing requests happening, this state is
never changed and thus the handler keeps the underlying connection alive
indefinitely.
With this commit the handler is initialized with
KeepAlive::Until. Asbefore the
keep_alivetimer is updated on incoming requests and set toKeepAlive::Yeson outgoing requests.Related to paritytech/polkadot#1544.