-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
--ratelimit and --ratelimit-window flags
when set, ratelimit operations during the prepare phase and the bench to a global ratelimit threshold to each worker
- Loading branch information
Showing
15 changed files
with
288 additions
and
8 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package bench | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Ratelimit struct { | ||
limit float64 | ||
window time.Duration | ||
counter int64 | ||
start time.Time | ||
} | ||
|
||
func InitRatelimit(limit float64, window time.Duration) Ratelimit { | ||
return Ratelimit{ | ||
limit: limit, | ||
window: window, | ||
counter: 0, | ||
} | ||
} | ||
|
||
// Limit returns true if the ratelimit is effective, false otherwise | ||
// Check there is room for a new request, if not, sleeping for the duration | ||
// that will requests rate to stay whithin the ratelimit | ||
func (r *Ratelimit) Limit() bool { | ||
if r.limit <= 0 { | ||
return false | ||
} | ||
|
||
// init the relative clock when first used | ||
if r.start.IsZero() { | ||
r.start = time.Now() | ||
} | ||
|
||
// calculate the time to sleep before next request to stay under the limit within the window | ||
// sleepDuration <= 0: we have room for new requests, no need to ratelimit | ||
// sleepDuration > 0: no more room for new requests, we have to wait before allowing new requests | ||
elapsed := time.Since(r.start) | ||
sleepDuration := time.Duration(float64(r.counter)/(r.limit/float64(r.window))) - elapsed | ||
|
||
// increment the number of requests | ||
r.counter++ | ||
|
||
if sleepDuration <= 0 { | ||
return false | ||
} | ||
|
||
time.Sleep(sleepDuration) | ||
return true | ||
} |
Oops, something went wrong.