Skip to content

Commit

Permalink
Added support of 0 limit (passthrough)
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed May 2, 2024
1 parent f1c4216 commit 916df71
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions throttle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func (t *Throttler[T]) Do(fn Fn[T]) (T, error) {

// advance updates the throttler state, advancing the window or incrementing the counter as necessary.
func (t *Throttler[T]) advance() {
// pass through
if t.limit == 0 {
return
}

now := time.Now()

// if this is the first operation, initialize the window
Expand Down
8 changes: 8 additions & 0 deletions throttle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ func TestThrottler_Wait_Consistent(t *testing.T) {
Limit uint64
Calls int
}{
{
Limit: 0,
Calls: 10,
},
{
Limit: 1,
Calls: 10,
Expand Down Expand Up @@ -70,6 +74,10 @@ func TestThrottler_Wait_Consistent(t *testing.T) {
expected := useCase.Limit

for _, actual := range groups {
if expected == 0 {
expected = uint64(useCase.Calls)
}

if actual > expected {
t.Fatal(fmt.Sprintf("Expected %d per second, but got %d", expected, actual))
}
Expand Down

0 comments on commit 916df71

Please sign in to comment.