-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: memory gcra limiter inconsistency when rate < cost < burst (#177)
Co-authored-by: Francesco Casula <fracasula@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
38 additions
and
1 deletion.
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,35 @@ | ||
package throttling | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMemoryGCRA(t *testing.T) { | ||
t.Run("burst and cost greater than rate", func(t *testing.T) { | ||
l := &gcra{} | ||
|
||
burst := int64(5) | ||
rate := int64(1) | ||
period := int64(1) | ||
|
||
limit, err := l.limit(context.Background(), "key", burst+rate, burst, rate, period) | ||
require.NoError(t, err) | ||
require.True(t, limit, "it should be able to fill the bucket (burst)") | ||
|
||
// next request should be allowed after 5 seconds | ||
start := time.Now() | ||
var allowed bool | ||
|
||
require.Eventually(t, func() bool { | ||
allowed, err = l.limit(context.Background(), "key", burst, burst, rate, period) | ||
require.NoError(t, err) | ||
return allowed | ||
}, 10*time.Second, 1*time.Second, "next request should be eventually allowed") | ||
|
||
require.GreaterOrEqual(t, time.Since(start).Seconds(), 5.0, "next request should be allowed after 5 seconds") | ||
}) | ||
} |