feat: add per-request upload and download bandwidth limiting - #524
Open
ChrisJr404 wants to merge 1 commit into
Open
feat: add per-request upload and download bandwidth limiting#524ChrisJr404 wants to merge 1 commit into
ChrisJr404 wants to merge 1 commit into
Conversation
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.
What
Adds
Request.SetUploadLimitandRequest.SetDownloadLimit, so you can cap theupload or download speed of a request to a given number of bytes per second.
This is the feature requested in #256.
Global wrappers
req.SetUploadLimit/req.SetDownloadLimitare there too, tomatch the other request options.
Behavior
Both limits wrap the underlying body in a small reader that paces itself: after
each read it sleeps for however long those bytes should have taken at the limit,
so the average throughput converges to the configured rate. A value of 0 or less
clears the limit.
content decoding, so it reflects real network usage. It reuses the same
response body wrapping mechanism as
SetDownloadCallbackand composes with itwhen both are set.
GetBodyso a retried or redirected request stays limited.Default behavior is unchanged when neither limit is set.
Tests
Added
ratelimit_test.gocovering the limiter itself (bytes come through intactand the read is throttled),
SetDownloadLimitandSetUploadLimitend to endagainst a local test server, and that a zero/negative value clears the limit.
The timing assertions only check a lower bound so they don't get flaky on slow
machines.
go test ./...andgo vet ./...both pass.Closes #256