add rate limiting to outbound p2p msg sending #3560
Merged
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.
Resolves #3553
We treat peers as abusive if inbound or outbound msg rates exceed 500/min.
This PR modifies this to only consider inbound msg rate.
We do not want to treat a peer as abusive if we are responsible for sending lots of p2p messages.
Add basic "rate limiting" to
write_message()
by inspecting the previous timestamp tracked by the tracker.We ensure 150ms has elapsed based on previous sent message before attempting to send another.
In normal operation this is rarely the case. We occasionally send a ping and a block header close together and these are now separated by 150ms.
The scenario where this does come into play is during "fast sync" when requesting large numbers of full blocks from a small number of peers. Specifically "block archival sync" against archival nodes.
We no longer flood the peer connection with hundreds of block requests and these are now spaced out to avoid hitting the abusive peer limit on the receiving end.
As part of this PR cleaned up the readonly usage of
tracker
.The following fns have been removed, replaced with exposing a simple
tracker()
accessor -last_min_sent_bytes
last_min_received_bytes
last_min_message_counts
For example -
This makes the read locks more explicit and removes a case where we take multiple read locks in one go.
Introduced
elapsed_since_last_msg()
toRateCounter
for convenience.This simplifies the implementation of the rate limiting logic.