-
-
Notifications
You must be signed in to change notification settings - Fork 113
feat: implement global and per-task download rate limiting #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SuperCoolPencil
wants to merge
10
commits into
main
Choose a base branch
from
speed-limit
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
65a27ce
feat: implement global and per-task download rate limiting using a to…
SuperCoolPencil 15a892a
test: add comprehensive rate limiting tests for token bucket and down…
SuperCoolPencil 2ea27c7
docs: add global and per-task rate limit configurations to SETTINGS.md
SuperCoolPencil 730fc2f
refactor: replace custom token bucket implementation with golang.org/…
SuperCoolPencil 3e46a46
feat: initialize global rate limiter from persisted configuration set…
SuperCoolPencil 6257a9a
feat: implement dynamic rate limiter updates and handle concurrent bu…
SuperCoolPencil 8b8b219
chore:
SuperCoolPencil 38887ac
address ai review comments
SuperCoolPencil a8396f8
fix: add nil check for settings and use errors.Is for rate limiter co…
SuperCoolPencil 827082b
feat: implement global and per-task download rate limiting using toke…
SuperCoolPencil 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 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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package concurrent | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/SurgeDM/Surge/internal/engine/types" | ||
| "github.com/SurgeDM/Surge/internal/testutil" | ||
| "github.com/SurgeDM/Surge/internal/utils" | ||
| ) | ||
|
|
||
| func TestConcurrentDownloader_GlobalRateLimit(t *testing.T) { | ||
| tmpDir, cleanup, _ := testutil.TempDir("surge-ratelimit-conc") | ||
| defer cleanup() | ||
|
|
||
| fileSize := int64(128 * 1024) // 128KB | ||
| server := testutil.NewMockServerT(t, | ||
| testutil.WithFileSize(fileSize), | ||
| testutil.WithRangeSupport(true), | ||
| ) | ||
| defer server.Close() | ||
|
|
||
| destPath := filepath.Join(tmpDir, "ratelimit_conc.bin") | ||
| state := types.NewProgressState("ratelimit-conc", fileSize) | ||
| runtime := &types.RuntimeConfig{ | ||
| MaxConnectionsPerHost: 2, | ||
| MinChunkSize: 32 * 1024, | ||
| } | ||
|
|
||
| downloader := NewConcurrentDownloader("ratelimit-id", nil, state, runtime) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| // Apply global rate limit of 64KB/s. | ||
| // First 64KB is instant (burst), next 64KB takes ~1s. | ||
| utils.GlobalRateLimiter.SetRate(64 * 1024) | ||
| defer utils.GlobalRateLimiter.SetRate(0) | ||
|
|
||
| if f, err := os.Create(destPath + ".surge"); err == nil { | ||
| _ = f.Close() | ||
| } | ||
|
|
||
| start := time.Now() | ||
| err := downloader.Download(ctx, server.URL(), nil, nil, destPath, fileSize) | ||
| if err != nil { | ||
| t.Fatalf("Download failed: %v", err) | ||
| } | ||
|
|
||
| elapsed := time.Since(start) | ||
| if elapsed < 800*time.Millisecond { | ||
| t.Errorf("Download completed too fast (%v), global rate limit not applied", elapsed) | ||
| } | ||
| } | ||
|
|
||
| func TestConcurrentDownloader_PerTaskRateLimit(t *testing.T) { | ||
| tmpDir, cleanup, _ := testutil.TempDir("surge-ratelimit-conc-task") | ||
| defer cleanup() | ||
|
|
||
| fileSize := int64(128 * 1024) // 128KB | ||
| server := testutil.NewMockServerT(t, | ||
| testutil.WithFileSize(fileSize), | ||
| testutil.WithRangeSupport(true), | ||
| ) | ||
| defer server.Close() | ||
|
|
||
| destPath := filepath.Join(tmpDir, "ratelimit_conc_task.bin") | ||
| state := types.NewProgressState("ratelimit-conc-task", fileSize) | ||
| // Apply per-task rate limit of 64KB/s | ||
| state.Limiter = utils.NewTokenBucket(64 * 1024) | ||
|
|
||
| runtime := &types.RuntimeConfig{ | ||
| MaxConnectionsPerHost: 2, | ||
| MinChunkSize: 32 * 1024, | ||
| } | ||
|
|
||
| downloader := NewConcurrentDownloader("ratelimit-task-id", nil, state, runtime) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| if f, err := os.Create(destPath + ".surge"); err == nil { | ||
| _ = f.Close() | ||
| } | ||
|
|
||
| start := time.Now() | ||
| err := downloader.Download(ctx, server.URL(), nil, nil, destPath, fileSize) | ||
| if err != nil { | ||
| t.Fatalf("Download failed: %v", err) | ||
| } | ||
|
|
||
| elapsed := time.Since(start) | ||
| if elapsed < 800*time.Millisecond { | ||
| t.Errorf("Download completed too fast (%v), per task rate limit not applied", elapsed) | ||
| } | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package single | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "path/filepath" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/SurgeDM/Surge/internal/engine/types" | ||
| "github.com/SurgeDM/Surge/internal/testutil" | ||
| "github.com/SurgeDM/Surge/internal/utils" | ||
| ) | ||
|
|
||
| func TestSingleDownloader_GlobalRateLimit(t *testing.T) { | ||
| tmpDir, cleanup, _ := testutil.TempDir("surge-ratelimit-single") | ||
| defer cleanup() | ||
|
|
||
| fileSize := int64(128 * 1024) // 128KB | ||
| server := testutil.NewMockServerT(t, | ||
| testutil.WithFileSize(fileSize), | ||
| testutil.WithRangeSupport(false), | ||
| ) | ||
| defer server.Close() | ||
|
|
||
| destPath := filepath.Join(tmpDir, "ratelimit_single.bin") | ||
| state := types.NewProgressState("ratelimit-single", fileSize) | ||
| runtime := &types.RuntimeConfig{} | ||
|
|
||
| downloader := NewSingleDownloader("ratelimit-id", nil, state, runtime) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| // Apply global rate limit of 64KB/s. | ||
| utils.GlobalRateLimiter.SetRate(64 * 1024) | ||
| defer utils.GlobalRateLimiter.SetRate(0) | ||
|
|
||
| if f, err := os.Create(destPath + ".surge"); err == nil { | ||
| _ = f.Close() | ||
| } | ||
|
|
||
| start := time.Now() | ||
| err := downloader.Download(ctx, server.URL(), destPath, fileSize, "ratelimit.bin") | ||
| if err != nil { | ||
| t.Fatalf("Download failed: %v", err) | ||
| } | ||
|
|
||
| elapsed := time.Since(start) | ||
| if elapsed < 800*time.Millisecond { | ||
| t.Errorf("Download completed too fast (%v), global rate limit not applied", elapsed) | ||
| } | ||
| } | ||
|
|
||
| func TestSingleDownloader_PerTaskRateLimit(t *testing.T) { | ||
| tmpDir, cleanup, _ := testutil.TempDir("surge-ratelimit-single-task") | ||
| defer cleanup() | ||
|
|
||
| fileSize := int64(128 * 1024) // 128KB | ||
| server := testutil.NewMockServerT(t, | ||
| testutil.WithFileSize(fileSize), | ||
| testutil.WithRangeSupport(false), | ||
| ) | ||
| defer server.Close() | ||
|
|
||
| destPath := filepath.Join(tmpDir, "ratelimit_single_task.bin") | ||
| state := types.NewProgressState("ratelimit-single-task", fileSize) | ||
| // Apply per-task rate limit of 64KB/s | ||
| state.Limiter = utils.NewTokenBucket(64 * 1024) | ||
|
|
||
| runtime := &types.RuntimeConfig{} | ||
|
|
||
| downloader := NewSingleDownloader("ratelimit-task-id", nil, state, runtime) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
| defer cancel() | ||
|
|
||
| if f, err := os.Create(destPath + ".surge"); err == nil { | ||
| _ = f.Close() | ||
| } | ||
|
|
||
| start := time.Now() | ||
| err := downloader.Download(ctx, server.URL(), destPath, fileSize, "ratelimit.bin") | ||
| if err != nil { | ||
| t.Fatalf("Download failed: %v", err) | ||
| } | ||
|
|
||
| elapsed := time.Since(start) | ||
| if elapsed < 800*time.Millisecond { | ||
| t.Errorf("Download completed too fast (%v), per-task rate limit not applied", elapsed) | ||
| } | ||
| } |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.