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
Fix ratelimiting for federation /send
requests.
#8342
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
71a36f1
Add ResponseCache to incoming transactions path
erikjohnston cc8a26d
Move ratelimit of /send/ after queues
erikjohnston d34d7d7
Newsfile
erikjohnston a98f8cb
Spelling
erikjohnston 7323f7c
Only queue based on origin, not transaction ID
erikjohnston 81f2b25
Comment
erikjohnston 2e30b9b
Merge branch 'release-v1.20.0' into erikj/429_fixes
clokep 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Fix ratelimitng of federation `/send` requests. |
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 |
---|---|---|
|
@@ -97,10 +97,16 @@ def __init__(self, hs): | |
self.state = hs.get_state_handler() | ||
|
||
self.device_handler = hs.get_device_handler() | ||
self._federation_ratelimiter = hs.get_federation_ratelimiter() | ||
|
||
self._server_linearizer = Linearizer("fed_server") | ||
self._transaction_linearizer = Linearizer("fed_txn_handler") | ||
|
||
# We cache results for transaction with the same ID | ||
self._transaction_resp_cache = ResponseCache( | ||
hs, "fed_txn_handler", timeout_ms=30000 | ||
) | ||
|
||
self.transaction_actions = TransactionActions(self.store) | ||
|
||
self.registry = hs.get_federation_registry() | ||
|
@@ -135,22 +141,44 @@ async def on_incoming_transaction( | |
request_time = self._clock.time_msec() | ||
|
||
transaction = Transaction(**transaction_data) | ||
transaction_id = transaction.transaction_id # type: ignore | ||
|
||
if not transaction.transaction_id: # type: ignore | ||
if not transaction_id: | ||
raise Exception("Transaction missing transaction_id") | ||
|
||
logger.debug("[%s] Got transaction", transaction.transaction_id) # type: ignore | ||
logger.debug("[%s] Got transaction", transaction_id) | ||
|
||
# use a linearizer to ensure that we don't process the same transaction | ||
# multiple times in parallel. | ||
with ( | ||
await self._transaction_linearizer.queue( | ||
(origin, transaction.transaction_id) # type: ignore | ||
) | ||
): | ||
result = await self._handle_incoming_transaction( | ||
origin, transaction, request_time | ||
) | ||
# We wrap in a ResponseCache so that we de-duplicate retried | ||
# transactions. | ||
return await self._transaction_resp_cache.wrap( | ||
(origin, transaction_id), | ||
self._on_incoming_transaction_inner, | ||
origin, | ||
transaction, | ||
request_time, | ||
) | ||
|
||
async def _on_incoming_transaction_inner( | ||
self, origin: str, transaction: Transaction, request_time: int | ||
) -> Tuple[int, Dict[str, Any]]: | ||
# Use a linearizer to ensure that transactions from a remote are | ||
# processed in order. | ||
with await self._transaction_linearizer.queue(origin): | ||
# We rate limit here *after* we've queued up the incoming requests, | ||
# so that we don't fill up the ratelimiter with blocked requests. | ||
Comment on lines
+167
to
+168
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. I'm not sure I'm following what this change is. Could you describe it a bit more? I see how the rate-limiter is moved deeper into the stack, but I'm not sure how a "blocked request" would fill it up previously that won't now? Does blocked mean a 429 is returned or that it got stuck in the linearizer? |
||
# | ||
# This is important as the ratelimiter allows N concurrent requests | ||
# at a time, and only starts ratelimiting if there are more requests | ||
# than that being processed at a time. If we queued up requests in | ||
# the linearizer/response cache *after* the ratelimiting then those | ||
# queued up requests would count as part of the allowed limit of N | ||
# concurrent requests. | ||
with self._federation_ratelimiter.ratelimit(origin) as d: | ||
await d | ||
clokep marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
result = await self._handle_incoming_transaction( | ||
origin, transaction, request_time | ||
) | ||
|
||
return result | ||
|
||
|
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
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.
Curious about the reasoning about the timeout here. 30 seconds seems a bit short?
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.
We tend to default to 30s for these I think. Note that this is 30s after the response is sent, though given the response won't generally be large we could probably up it some more.
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.
Ah, I thought it was 30s from when the request was sent. That makes more sense now!