-
Notifications
You must be signed in to change notification settings - Fork 0
FMWK-788 Improved bandwidth limiter #315
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
Open
filkeith
wants to merge
11
commits into
main
Choose a base branch
from
FMWK-788-new-limiter
base: main
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
11 commits
Select commit
Hold shift + click to select a range
235f381
FMWK-788-new-limiter
filkeith a0d9fc5
Merge branch 'main' into FMWK-788-new-limiter
filkeith 8c302d6
FMWK-788-new-limiter
filkeith cf8e053
FMWK-788-new-limiter
filkeith 4f41fdf
Revert "FMWK-788-new-limiter"
filkeith 29ea8b1
FMWK-788-new-limiter
filkeith 329b80f
FMWK-788-new-limiter
filkeith 0663495
Merge branch 'main' into FMWK-788-new-limiter
filkeith 515df43
FMWK-788-new-limiter
filkeith 96cfa67
FMWK-788-new-limiter
filkeith 3f09e64
FMWK-788-new-limiter
filkeith 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
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
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
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
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
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
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
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
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
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
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,112 @@ | ||
// Copyright 2024 Aerospike, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bandwidth | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// Bucket implements a thread-safe leaky bucket rate limiter. | ||
type Bucket struct { | ||
// mu to make bucket thread safe. | ||
// As bucket is used for limiting speed, one mutex won't affect speed. | ||
mu sync.Mutex | ||
|
||
// Maximum tokens in the bucket. | ||
limit int64 | ||
|
||
// Rate at which tokens are added (tokens per nanosecond). | ||
rate float64 | ||
|
||
// Current available tokens (can be fractional for precise calculations). | ||
tokens float64 | ||
|
||
// Last time we leaked tokens. | ||
lastLeak time.Time | ||
} | ||
|
||
// NewBucket creates a new rate limiter with the specified limit and interval. | ||
func NewBucket(limit int64, interval time.Duration) (*Bucket, error) { | ||
if limit <= 0 { | ||
return nil, fmt.Errorf("limit must be greater than 0") | ||
} | ||
|
||
if interval <= 0 { | ||
return nil, fmt.Errorf("interval must be greater than 0") | ||
} | ||
// Calculate rate as tokens per nanosecond for precise calculations. | ||
rate := float64(limit) / float64(interval.Nanoseconds()) | ||
|
||
return &Bucket{ | ||
limit: limit, | ||
rate: rate, | ||
tokens: float64(limit), | ||
lastLeak: time.Now(), | ||
}, nil | ||
} | ||
|
||
// Wait blocks until n tokens are available. | ||
// It allows waiting for amounts larger than the limit. | ||
func (rl *Bucket) Wait(n int64) { | ||
filkeith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if n < 1 { | ||
return | ||
} | ||
|
||
rl.mu.Lock() | ||
defer rl.mu.Unlock() | ||
|
||
// Leak tokens. | ||
rl.leak() | ||
|
||
// If we have enough tokens, use them and return. | ||
if rl.tokens >= float64(n) { | ||
rl.tokens -= float64(n) | ||
return | ||
} | ||
|
||
// Calculate exact time needed to accumulate required tokens. | ||
tokensNeeded := float64(n) - rl.tokens | ||
waitDuration := time.Duration(tokensNeeded / rl.rate) | ||
|
||
// Wait for exact duration. | ||
time.Sleep(waitDuration) | ||
|
||
// After waiting, leak tokens again and consume. | ||
rl.leak() | ||
rl.tokens -= float64(n) | ||
} | ||
|
||
// leak updates the token count based on elapsed time. | ||
func (rl *Bucket) leak() { | ||
now := time.Now() | ||
elapsed := now.Sub(rl.lastLeak) | ||
reugn marked this conversation as resolved.
Show resolved
Hide resolved
filkeith marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Calculate exact tokens to add based on elapsed time. | ||
tokensToAdd := rl.rate * float64(elapsed.Nanoseconds()) | ||
|
||
if tokensToAdd > 0 { | ||
rl.tokens += tokensToAdd | ||
|
||
// Don't overflow the bucket. | ||
if rl.tokens > float64(rl.limit) { | ||
rl.tokens = float64(rl.limit) | ||
} | ||
|
||
// Update last leak time to current time. | ||
rl.lastLeak = now | ||
} | ||
} |
Oops, something went wrong.
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.