-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Remove trailing slashes from outbound federation requests #4840
Changes from 11 commits
525dd02
64ff110
f8740d5
a5dd335
a8a028d
f18dca2
66f205e
802cb5d
4868b12
0ea8582
97653ef
cf301e3
7e75d96
7d053cf
09626bf
5526b05
660b77f
220607a
9dd0e34
ee8ba39
c2d848b
c991e7a
bec3138
66cdb84
7c0295f
5ca857a
26f8e2d
8d16ffa
45524f2
86c60bd
9a2e22f
b2df0e8
ecea5af
621e7f3
a8ad39e
94cb793
551ea11
c69df5d
cd36a12
bb52a2e
2150151
b41c2ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Remove trailing slashes from certain outbound federation requests. Retry if receiving a 404. Context: #3622. |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -196,7 +196,8 @@ def _send_request( | |||||||
timeout=None, | ||||||||
long_retries=False, | ||||||||
ignore_backoff=False, | ||||||||
backoff_on_404=False | ||||||||
backoff_on_404=False, | ||||||||
try_trailing_slash_on_404=False, | ||||||||
): | ||||||||
""" | ||||||||
Sends a request to the given server. | ||||||||
|
@@ -212,6 +213,11 @@ def _send_request( | |||||||
|
||||||||
backoff_on_404 (bool): Back off if we get a 404 | ||||||||
|
||||||||
try_trailing_slash_on_404 (bool): True if on a 404 response we | ||||||||
should try appending a trailing slash to the end of the | ||||||||
request. This will be attempted before backing off if backing | ||||||||
off has been enabled. | ||||||||
|
||||||||
Returns: | ||||||||
Deferred[twisted.web.client.Response]: resolves with the HTTP | ||||||||
response object on success. | ||||||||
|
@@ -473,7 +479,8 @@ def put_json(self, destination, path, args={}, data={}, | |||||||
json_data_callback=None, | ||||||||
long_retries=False, timeout=None, | ||||||||
ignore_backoff=False, | ||||||||
backoff_on_404=False): | ||||||||
backoff_on_404=False, | ||||||||
try_trailing_slash_on_404=False): | ||||||||
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.
Suggested change
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. flake unfortunately complains about visual indentations if we do this. 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. /me shakes fist |
||||||||
""" Sends the specifed json data using PUT | ||||||||
|
||||||||
Args: | ||||||||
|
@@ -493,7 +500,11 @@ def put_json(self, destination, path, args={}, data={}, | |||||||
and try the request anyway. | ||||||||
backoff_on_404 (bool): True if we should count a 404 response as | ||||||||
a failure of the server (and should therefore back off future | ||||||||
requests) | ||||||||
requests). | ||||||||
try_trailing_slash_on_404 (bool): True if on a 404 response we | ||||||||
should try appending a trailing slash to the end of the | ||||||||
request. This will be attempted before backing off if backing | ||||||||
off has been enabled. | ||||||||
|
||||||||
Returns: | ||||||||
Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The | ||||||||
|
@@ -509,7 +520,6 @@ def put_json(self, destination, path, args={}, data={}, | |||||||
RequestSendFailed: If there were problems connecting to the | ||||||||
remote, due to e.g. DNS failures, connection timeouts etc. | ||||||||
""" | ||||||||
|
||||||||
request = MatrixFederationRequest( | ||||||||
method="PUT", | ||||||||
destination=destination, | ||||||||
|
@@ -519,13 +529,26 @@ def put_json(self, destination, path, args={}, data={}, | |||||||
json=data, | ||||||||
) | ||||||||
|
||||||||
response = yield self._send_request( | ||||||||
request, | ||||||||
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. nit: it's probably best to leave it as one arg on each line - it gets a bit impenetrable otherwise. |
||||||||
long_retries=long_retries, | ||||||||
timeout=timeout, | ||||||||
ignore_backoff=ignore_backoff, | ||||||||
backoff_on_404=backoff_on_404, | ||||||||
) | ||||||||
send_request_args = { | ||||||||
"request": request, | ||||||||
"long_retries": long_retries, | ||||||||
"timeout": timeout, | ||||||||
"ignore_backoff": ignore_backoff, | ||||||||
# Do not backoff on the initial request if we're trying with trailing slashes | ||||||||
# Otherwise we may end up waiting to contact a server that is actually up | ||||||||
"backoff_on_404": False if try_trailing_slash_on_404 else backoff_on_404, | ||||||||
} | ||||||||
|
||||||||
response = yield self._send_request(**send_request_args) | ||||||||
|
||||||||
# If enabled, retry with a trailing slash if we received a 404 | ||||||||
if try_trailing_slash_on_404 and response.code == 404: | ||||||||
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. you seem to have missed the 400 case here, which brings me to: how about implementing this with a wrapper which you can use in both cases?
|
||||||||
args["path"] += "/" | ||||||||
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. are you sure this is the right thing to do? |
||||||||
|
||||||||
# Re-enable backoff if enabled | ||||||||
send_request_args["backoff_on_404"] = backoff_on_404 | ||||||||
|
||||||||
response = yield self.get_json(**send_request_args) | ||||||||
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 switch to |
||||||||
|
||||||||
body = yield _handle_json_response( | ||||||||
self.hs.get_reactor(), self.default_timeout, request, response, | ||||||||
|
@@ -592,7 +615,8 @@ def post_json(self, destination, path, data={}, long_retries=False, | |||||||
|
||||||||
@defer.inlineCallbacks | ||||||||
def get_json(self, destination, path, args=None, retry_on_dns_fail=True, | ||||||||
timeout=None, ignore_backoff=False): | ||||||||
timeout=None, ignore_backoff=False, | ||||||||
try_trailing_slash_on_404=False): | ||||||||
""" GETs some json from the given host homeserver and path | ||||||||
|
||||||||
Args: | ||||||||
|
@@ -606,6 +630,9 @@ def get_json(self, destination, path, args=None, retry_on_dns_fail=True, | |||||||
be retried. | ||||||||
ignore_backoff (bool): true to ignore the historical backoff data | ||||||||
and try the request anyway. | ||||||||
try_trailing_slash_on_404 (bool): True if on a 404 response we | ||||||||
should try appending a trailing slash to the end of the | ||||||||
request. | ||||||||
Returns: | ||||||||
Deferred[dict|list]: Succeeds when we get a 2xx HTTP response. The | ||||||||
result will be the decoded JSON body. | ||||||||
|
@@ -631,12 +658,19 @@ def get_json(self, destination, path, args=None, retry_on_dns_fail=True, | |||||||
query=args, | ||||||||
) | ||||||||
|
||||||||
response = yield self._send_request( | ||||||||
request, | ||||||||
retry_on_dns_fail=retry_on_dns_fail, | ||||||||
timeout=timeout, | ||||||||
ignore_backoff=ignore_backoff, | ||||||||
) | ||||||||
send_request_args = { | ||||||||
"request": request, | ||||||||
"retry_on_dns_fail": retry_on_dns_fail, | ||||||||
"timeout": timeout, | ||||||||
"ignore_backoff": ignore_backoff, | ||||||||
} | ||||||||
|
||||||||
response = yield self._send_request(**send_request_args) | ||||||||
|
||||||||
# If enabled, retry with a trailing slash if we received a 404 | ||||||||
if try_trailing_slash_on_404 and response.code == 404: | ||||||||
args["path"] += "/" | ||||||||
response = yield self._send_request(**send_request_args) | ||||||||
|
||||||||
body = yield _handle_json_response( | ||||||||
self.hs.get_reactor(), self.default_timeout, request, response, | ||||||||
|
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.
it might be worth adding a note on why this is a useful thing to do ("workaround for #3622 in Synapse 0.99.2 and earlier" ?)