Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c2e8e28
commit cb63b75
Showing
6 changed files
with
187 additions
and
99 deletions.
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,26 @@ | ||
package ratelimit_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/projectdiscovery/ratelimit" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAdaptiveRateLimit(t *testing.T) { | ||
limiter := ratelimit.NewUnlimited(context.Background()) | ||
start := time.Now() | ||
|
||
for i := 0; i < 132; i++ { | ||
limiter.Take() | ||
// got 429 / hit ratelimit after 100 | ||
if i == 100 { | ||
// Retry-After and new limiter (calibrate using different statergies) | ||
// new expected ratelimit 30req every 5 sec | ||
limiter.SleepandReset(time.Duration(5)*time.Second, 30, time.Duration(5)*time.Second) | ||
} | ||
} | ||
require.Equal(t, time.Since(start).Round(time.Second), time.Duration(10)*time.Second) | ||
} |
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
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