-
Notifications
You must be signed in to change notification settings - Fork 82
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
Implement concurrency limit template #32
Open
masim05
wants to merge
3
commits into
hexdigest:master
Choose a base branch
from
masim05:concurrency-limit-template
base: master
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
3 commits
Select commit
Hold shift + click to select a range
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
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,46 @@ | ||
{{ $decorator := (or .Vars.DecoratorName (printf "%sWithConcurrencyLimit" .Interface.Name)) }} | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// {{$decorator}} implements {{.Interface.Type}} | ||
type {{$decorator}} struct { | ||
_base {{.Interface.Type}} | ||
_burst chan int | ||
} | ||
|
||
// New{{$decorator}} instruments an implementation of the {{.Interface.Type}} with concurrency limiting | ||
func New{{$decorator}}(base {{.Interface.Type}}, concurrentCalls int) *{{$decorator}} { | ||
d := &{{$decorator}}{ | ||
_base: base, | ||
_burst: make(chan int, concurrentCalls), | ||
} | ||
|
||
return d | ||
} | ||
|
||
{{range $method := .Interface.Methods}} | ||
// {{$method.Name}} implements {{$.Interface.Type}} | ||
func (_d *{{$decorator}}) {{$method.Declaration}} { | ||
|
||
{{- if (and $method.AcceptsContext $method.ReturnsError)}} | ||
select { | ||
case <-ctx.Done(): | ||
err = ctx.Err() | ||
return | ||
case _d._burst<-1: | ||
defer func() { | ||
<-_d._burst | ||
}() | ||
} | ||
{{else}} | ||
_d._burst<-1 | ||
defer func() { | ||
<-_d._burst | ||
}() | ||
{{end}} | ||
|
||
{{ $method.Pass "_d._base." }} | ||
} | ||
{{end}} |
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,72 @@ | ||
package templatestests | ||
|
||
import "context" | ||
|
||
// DO NOT EDIT! | ||
// This code is generated with http://github.com/hexdigest/gowrap tool | ||
// using ../templates/concurrencylimit template | ||
|
||
//go:generate gowrap gen -p github.com/hexdigest/gowrap/templates_tests -i TestInterface -t ../templates/concurrencylimit -o interface_with_concurrency_limit.go | ||
|
||
// TestInterfaceWithConcurrencyLimit implements TestInterface | ||
type TestInterfaceWithConcurrencyLimit struct { | ||
_base TestInterface | ||
_burst chan int | ||
} | ||
|
||
// NewTestInterfaceWithConcurrencyLimit instruments an implementation of the TestInterface with concurrency limiting | ||
func NewTestInterfaceWithConcurrencyLimit(base TestInterface, concurrentCalls int) *TestInterfaceWithConcurrencyLimit { | ||
d := &TestInterfaceWithConcurrencyLimit{ | ||
_base: base, | ||
_burst: make(chan int, concurrentCalls), | ||
} | ||
|
||
return d | ||
} | ||
|
||
// Channels implements TestInterface | ||
func (_d *TestInterfaceWithConcurrencyLimit) Channels(chA chan bool, chB chan<- bool, chanC <-chan bool) { | ||
_d._burst <- 1 | ||
defer func() { | ||
<-_d._burst | ||
}() | ||
|
||
_d._base.Channels(chA, chB, chanC) | ||
return | ||
} | ||
|
||
// F implements TestInterface | ||
func (_d *TestInterfaceWithConcurrencyLimit) F(ctx context.Context, a1 string, a2 ...string) (result1 string, result2 string, err error) { | ||
select { | ||
case <-ctx.Done(): | ||
err = ctx.Err() | ||
return | ||
case _d._burst <- 1: | ||
defer func() { | ||
<-_d._burst | ||
}() | ||
} | ||
|
||
return _d._base.F(ctx, a1, a2...) | ||
} | ||
|
||
// NoError implements TestInterface | ||
func (_d *TestInterfaceWithConcurrencyLimit) NoError(s1 string) (s2 string) { | ||
_d._burst <- 1 | ||
defer func() { | ||
<-_d._burst | ||
}() | ||
|
||
return _d._base.NoError(s1) | ||
} | ||
|
||
// NoParamsOrResults implements TestInterface | ||
func (_d *TestInterfaceWithConcurrencyLimit) NoParamsOrResults() { | ||
_d._burst <- 1 | ||
defer func() { | ||
<-_d._burst | ||
}() | ||
|
||
_d._base.NoParamsOrResults() | ||
return | ||
} |
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,46 @@ | ||
package templatestests | ||
|
||
import ( | ||
"context" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTestInterfaceWithConcurrencyLimit_F(t *testing.T) { | ||
impl := &testImpl{r1: "1", r2: "2", delay: 100 * time.Millisecond} | ||
|
||
wrapped := NewTestInterfaceWithConcurrencyLimit(impl, 3) | ||
|
||
for i := 0; i < 10; i++ { | ||
go func() { | ||
r1, r2, err := wrapped.F(context.Background(), "a1") | ||
assert.NoError(t, err) | ||
assert.Equal(t, "1", r1) | ||
assert.Equal(t, "2", r2) | ||
|
||
}() | ||
} | ||
|
||
<-time.After(10 * time.Millisecond) | ||
|
||
counter := atomic.LoadUint64(&impl.callCounter) | ||
assert.EqualValues(t, 3, counter) // the first burst | ||
|
||
<-time.After(100 * time.Millisecond) | ||
|
||
counter = atomic.LoadUint64(&impl.callCounter) | ||
assert.EqualValues(t, 6, counter) // the second burst | ||
|
||
<-time.After(100 * time.Millisecond) | ||
|
||
counter = atomic.LoadUint64(&impl.callCounter) | ||
assert.EqualValues(t, 9, counter) // the third burst | ||
|
||
<-time.After(100 * time.Millisecond) | ||
|
||
counter = atomic.LoadUint64(&impl.callCounter) | ||
assert.EqualValues(t, 10, counter) // the 10th call | ||
} |
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.
"limits THE amount" please