This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Wrap connections in an N minute timeout to ensure they get reaped correctly #1725
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f5a4001
Merge branch 'release-v0.18.5' of github.com:matrix-org/synapse
erikjohnston 5b6672c
Wrap connections in an N minute timeout to ensure they get reaped cor…
erikjohnston b7336ff
Clean up
erikjohnston 68030fd
Spelling and comments
erikjohnston b4bc6fe
Respect long_retries param and default to off
erikjohnston 97ffc56
Manually abort the underlying TLS connection.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
# limitations under the License. | ||
|
||
from twisted.internet.endpoints import SSL4ClientEndpoint, TCP4ClientEndpoint | ||
from twisted.internet import defer | ||
from twisted.internet import defer, reactor | ||
from twisted.internet.error import ConnectError | ||
from twisted.names import client, dns | ||
from twisted.names.error import DNSNameError, DomainError | ||
|
@@ -66,13 +66,67 @@ def matrix_federation_endpoint(reactor, destination, ssl_context_factory=None, | |
default_port = 8448 | ||
|
||
if port is None: | ||
return SRVClientEndpoint( | ||
return _WrappingEndpointFac(SRVClientEndpoint( | ||
reactor, "matrix", domain, protocol="tcp", | ||
default_port=default_port, endpoint=transport_endpoint, | ||
endpoint_kw_args=endpoint_kw_args | ||
) | ||
)) | ||
else: | ||
return transport_endpoint(reactor, domain, port, **endpoint_kw_args) | ||
return _WrappingEndpointFac(transport_endpoint( | ||
reactor, domain, port, **endpoint_kw_args | ||
)) | ||
|
||
|
||
class _WrappingEndpointFac(object): | ||
def __init__(self, endpoint_fac): | ||
self.endpoint_fac = endpoint_fac | ||
|
||
@defer.inlineCallbacks | ||
def connect(self, protocolFactory): | ||
conn = yield self.endpoint_fac.connect(protocolFactory) | ||
conn = _WrappedConnection(conn) | ||
defer.returnValue(conn) | ||
|
||
|
||
class _WrappedConnection(object): | ||
"""Wraps a connection and calls abort on it if it hasn't seen any actio | ||
for 5 minutes | ||
""" | ||
__slots__ = ["conn", "last_request"] | ||
|
||
def __init__(self, conn): | ||
object.__setattr__(self, "conn", conn) | ||
object.__setattr__(self, "last_request", time.time()) | ||
|
||
def __getattr__(self, name): | ||
return getattr(self.conn, name) | ||
|
||
def __setattr__(self, name, value): | ||
setattr(self.conn, name, value) | ||
|
||
def _time_things_out_maybe(self): | ||
# We use a slightly shorter timeout here just in case the callLater is | ||
# triggered early. Paranoia ftw. | ||
if time.time() - self.last_request >= 2.5 * 60: | ||
self.abort() | ||
|
||
def request(self, request): | ||
self.last_request = time.time() | ||
|
||
# Time this connection out if we haven't send a request in the last | ||
# N minutes | ||
reactor.callLater(3 * 60, self._time_things_out_maybe) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is it 3 mins here, but 2 mins in the _time_things_out_maybe above? |
||
|
||
d = self.conn.request(request) | ||
|
||
def update_request_time(res): | ||
self.last_request = time.time() | ||
reactor.callLater(3 * 60, self._time_things_out_maybe) | ||
return res | ||
|
||
d.addCallback(update_request_time) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we do this by both a callback and a reactor.callLater()? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We schedule a timeout in 3 minutes both before we send the request, and after we received the response. |
||
|
||
return d | ||
|
||
|
||
class SpiderEndpoint(object): | ||
|
This file contains 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
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.
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.
missing "n" in "action". Also, the comment says 5 minutes, but the code says 3 minutes?