-
Notifications
You must be signed in to change notification settings - Fork 9
Implement sliding window rate limiter #178
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 all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f3b8d6
Implement sliding window rate limiter
hiepnv90 4d4ef5f
Update rate limiter
hiepnv90 c1074de
Update logic for rate limiter
hiepnv90 6ec1d79
Correct logic for rate limiter
hiepnv90 d58ce50
Correct access request for rate limmit
hiepnv90 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| package rate | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "sync" | ||
| "time" | ||
| ) | ||
|
|
||
| type Limiter struct { | ||
| limit int | ||
| period time.Duration | ||
|
|
||
| mu sync.Mutex | ||
| start, end, size int | ||
| requests []time.Time | ||
| } | ||
|
|
||
| func NewLimiter(limit int, period time.Duration) *Limiter { | ||
| return &Limiter{ | ||
| limit: limit, | ||
| period: period, | ||
| start: 0, | ||
| end: 0, | ||
| requests: make([]time.Time, limit), | ||
| } | ||
| } | ||
|
|
||
| func (l *Limiter) Wait(ctx context.Context) error { | ||
| return l.WaitN(ctx, 1) | ||
| } | ||
|
|
||
| func (l *Limiter) WaitN(ctx context.Context, n int) error { | ||
| l.mu.Lock() | ||
| defer l.mu.Unlock() | ||
|
|
||
| if n <= 0 { | ||
| return nil | ||
| } | ||
|
|
||
| if n > l.limit { | ||
| return fmt.Errorf("rate: Wait (n=%d) exceed limiter %d", n, l.limit) | ||
| } | ||
|
|
||
| // Get the oldest request in queue for waiting. | ||
| var ( | ||
| shouldWait bool | ||
| oldest time.Time | ||
| ) | ||
| if l.requestsSize()+n > l.limit { | ||
| shouldWait = true | ||
| oldest, _ = l.requestAt(l.requestsSize() + n - l.limit - 1) | ||
| } | ||
|
|
||
| // Wait if rate limit is reached. | ||
| if shouldWait { | ||
| waitDuration := l.period - time.Since(oldest) | ||
| if waitDuration > 0 { | ||
| timer := time.NewTimer(waitDuration) | ||
| defer timer.Stop() | ||
|
|
||
| select { | ||
| case <-timer.C: | ||
| // We can proceed. | ||
| case <-ctx.Done(): | ||
| // Context was canceled before we could proceed. | ||
| return ctx.Err() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Add new requests to queue. | ||
| for range n { | ||
| l.addRequest(time.Now()) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (l *Limiter) requestsSize() int { | ||
| return l.size | ||
| } | ||
|
|
||
| func (l *Limiter) requestAt(i int) (time.Time, bool) { | ||
| if i >= l.size { | ||
| return time.Now(), false | ||
| } | ||
|
|
||
| return l.requests[(l.start+i)%l.limit], true | ||
| } | ||
|
|
||
| func (l *Limiter) addRequest(t time.Time) { | ||
| if l.size == l.limit { | ||
| l.start++ | ||
| if l.start >= l.limit { | ||
| l.start = 0 | ||
| } | ||
| } else { | ||
| l.size++ | ||
| } | ||
|
|
||
| l.requests[l.end] = t | ||
|
|
||
| l.end++ | ||
| if l.end >= l.limit { | ||
| l.end = 0 | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.