-
Notifications
You must be signed in to change notification settings - Fork 142
add ConcurrencyLimit to worker to enable dynamic tuning of concurrencies #1410
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14e3c01
add concurrencylimit entity to worker
shijiesheng 082d125
wip
shijiesheng c5f2d53
add PermitChannel
shijiesheng 40cd57c
fix unit test and address comment on AcquireChan return
shijiesheng 6342ff8
add unit test and fix comments
shijiesheng 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,7 +60,7 @@ func TestPermit_Simulation(t *testing.T) { | |
| maxTestDuration: 250 * time.Millisecond, // at least need 100ms * 1000 / 200 = 500ms to acquire all permit | ||
| capacity: []int{200}, | ||
| goroutines: 1000, | ||
| expectFailuresRange: []int{1, 999}, // should at least pass some acquires | ||
| expectFailuresRange: []int{400, 600}, // should at least pass some acquires | ||
| }, | ||
| { | ||
| name: "not enough permit at beginning but due to capacity change, blocking but all acquire", | ||
|
|
@@ -74,7 +74,7 @@ func TestPermit_Simulation(t *testing.T) { | |
| maxTestDuration: 250 * time.Millisecond, | ||
| capacity: []int{600, 400, 200}, | ||
| goroutines: 1000, | ||
| expectFailuresRange: []int{1, 999}, | ||
| expectFailuresRange: []int{1, 500}, // the worst case with 200 capacity will at least pass 500 acquires | ||
| }, | ||
| { | ||
| name: "not enough permit for any acquire, fail all", | ||
|
|
@@ -145,18 +145,139 @@ func TestPermit_Simulation(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func Test_Permit_AcquireChan(t *testing.T) { | ||
| permit := NewResizablePermit(2) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
| defer cancel() | ||
| permitChan, done := permit.AcquireChan(ctx) | ||
| select { | ||
| case <-permitChan: | ||
| assert.Equal(t, 2, permit.Quota()) | ||
| // Test_Permit_Acquire tests the basic acquire functionality | ||
| // before each acquire will wait 100ms | ||
| func Test_Permit_Acquire(t *testing.T) { | ||
|
|
||
| t.Run("acquire 1 permit", func(t *testing.T) { | ||
| permit := NewResizablePermit(1) | ||
| err := permit.Acquire(context.Background()) | ||
| assert.NoError(t, err) | ||
| assert.Equal(t, 1, permit.Count()) | ||
| case <-ctx.Done(): | ||
| t.Error("unexpected timeout") | ||
| }) | ||
|
|
||
| t.Run("acquire timeout", func(t *testing.T) { | ||
| permit := NewResizablePermit(1) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| time.Sleep(100 * time.Millisecond) | ||
| err := permit.Acquire(ctx) | ||
| assert.ErrorContains(t, err, "context deadline exceeded") | ||
| assert.Empty(t, permit.Count()) | ||
| }) | ||
|
|
||
| t.Run("cancel acquire", func(t *testing.T) { | ||
| permit := NewResizablePermit(1) | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
| err := permit.Acquire(ctx) | ||
| assert.ErrorContains(t, err, "canceled") | ||
| assert.Empty(t, permit.Count()) | ||
| }) | ||
|
|
||
| t.Run("acquire more than quota", func(t *testing.T) { | ||
| permit := NewResizablePermit(1) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| err := permit.Acquire(ctx) | ||
| assert.NoError(t, err) | ||
| err = permit.Acquire(ctx) | ||
| assert.ErrorContains(t, err, "failed to acquire permit") | ||
| assert.Equal(t, 1, permit.Count()) | ||
| }) | ||
| } | ||
|
|
||
| func Test_Permit_Release(t *testing.T) { | ||
| for _, tt := range []struct { | ||
| name string | ||
| quota, acquire, release int | ||
| expectPanic bool | ||
| }{ | ||
| {"release all acquired permits", 10, 5, 5, false}, | ||
| {"release partial acquired permit", 10, 5, 1, false}, | ||
| {"release non acquired permit", 10, 5, 0, false}, | ||
| {"release more than acquired permit", 10, 5, 10, true}, | ||
| } { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| permit := NewResizablePermit(tt.quota) | ||
| for i := 0; i < tt.acquire; i++ { | ||
| err := permit.Acquire(context.Background()) | ||
| assert.NoError(t, err) | ||
| } | ||
| releaseOp := func() { | ||
| for i := 0; i < tt.release; i++ { | ||
| permit.Release() | ||
| } | ||
| } | ||
|
|
||
| if tt.expectPanic { | ||
| assert.Panics(t, releaseOp) | ||
| } else { | ||
| assert.NotPanics(t, releaseOp) | ||
| assert.Equal(t, tt.acquire-tt.release, permit.Count()) | ||
| } | ||
| }) | ||
| } | ||
| done() | ||
| permit.Release() | ||
| } | ||
|
|
||
| func Test_Permit_AcquireChan(t *testing.T) { | ||
| t.Run("acquire 1 permit", func(t *testing.T) { | ||
| permit := NewResizablePermit(1) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| channel, done := permit.AcquireChan(ctx) | ||
| defer done() | ||
| select { | ||
| case <-channel: | ||
| assert.Equal(t, 1, permit.Count()) | ||
| case <-ctx.Done(): | ||
| t.Errorf("permit not acquired") | ||
| } | ||
| }) | ||
|
|
||
| t.Run("acquire timeout", func(t *testing.T) { | ||
| permit := NewResizablePermit(1) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
| time.Sleep(100 * time.Millisecond) | ||
|
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. same as above, this makes it identical to the cancel case below (the chan is closed before it starts) |
||
| channel, done := permit.AcquireChan(ctx) | ||
| defer done() | ||
| select { | ||
| case <-channel: | ||
| t.Errorf("permit acquired") | ||
| case <-ctx.Done(): | ||
| assert.Empty(t, permit.Count()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("cancel acquire", func(t *testing.T) { | ||
| permit := NewResizablePermit(1) | ||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| cancel() | ||
| channel, done := permit.AcquireChan(ctx) | ||
| defer done() | ||
| select { | ||
| case <-channel: | ||
| t.Errorf("permit acquired") | ||
| case <-ctx.Done(): | ||
| assert.Empty(t, permit.Count()) | ||
| } | ||
| }) | ||
|
|
||
| t.Run("acquire more than quota", func(t *testing.T) { | ||
| permit := NewResizablePermit(4) | ||
| ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
| defer cancel() | ||
|
|
||
| for i := 0; i < 10; i++ { | ||
| channel, done := permit.AcquireChan(ctx) | ||
| select { | ||
| case <-channel: | ||
| case <-ctx.Done(): | ||
| } | ||
| done() | ||
| } | ||
|
|
||
| assert.Equal(t, 4, permit.Count()) | ||
| }) | ||
| } | ||
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.
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.
these two tests are the same fwiw - you probably want to have one that does