mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? Add a `RecycleToken` function to our custom rate limiters. It seems that most of the rate limiters just wrap the `ClockedRateLimiter`, so let the `ClockedRateLimiter`'s `WaitN` function unblock the waiter if recycle is called, thus allowing us to recycle tokens and give them to any waiters if they exist. ## Why? Lots of invalid tasks combined with Activity task dispatch rate limiting can cause the actual rate to be noticeably below the maximum rate. This will make the actual rate match the max rps, as long as `time_to_recycle` is reasonably short and `recycle_rate` is reasonable (see discussion in the comments) ## How did you test it? ClockedRateLimiter unit test. ## Potential risks The max Activity RPS could break. ## Documentation <!-- Have you made sure this change doesn't falsify anything currently stated in `docs/`? If significant new behavior is added, have you described that in `docs/`? --> ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) --> --------- Co-authored-by: David Reiss <[email protected]>
173 lines
5.0 KiB
Go
173 lines
5.0 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package quotas
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
defaultRefreshInterval = time.Minute
|
|
defaultIncomingRateBurstRatio = float64(2)
|
|
defaultOutgoingRateBurstRatio = float64(1)
|
|
)
|
|
|
|
type (
|
|
// DynamicRateLimiterImpl implements a dynamic config wrapper around the rate limiter
|
|
DynamicRateLimiterImpl struct {
|
|
rateBurstFn RateBurst
|
|
refreshInterval time.Duration
|
|
|
|
refreshTimer *time.Timer
|
|
rateLimiter *RateLimiterImpl
|
|
}
|
|
)
|
|
|
|
var _ RateLimiter = (*DynamicRateLimiterImpl)(nil)
|
|
|
|
// NewDynamicRateLimiter returns a rate limiter which handles dynamic config
|
|
func NewDynamicRateLimiter(
|
|
rateBurstFn RateBurst,
|
|
refreshInterval time.Duration,
|
|
) *DynamicRateLimiterImpl {
|
|
rateLimiter := &DynamicRateLimiterImpl{
|
|
rateBurstFn: rateBurstFn,
|
|
refreshInterval: refreshInterval,
|
|
|
|
refreshTimer: time.NewTimer(refreshInterval),
|
|
rateLimiter: NewRateLimiter(rateBurstFn.Rate(), rateBurstFn.Burst()),
|
|
}
|
|
return rateLimiter
|
|
}
|
|
|
|
// NewDefaultIncomingRateLimiter returns a default rate limiter
|
|
// for incoming traffic, using fixed burst ratio of 2
|
|
// and fixed 1 minute refresh interval
|
|
func NewDefaultIncomingRateLimiter(
|
|
rateFn RateFn,
|
|
) *DynamicRateLimiterImpl {
|
|
return NewDynamicRateLimiter(
|
|
NewDefaultIncomingRateBurst(rateFn),
|
|
defaultRefreshInterval,
|
|
)
|
|
}
|
|
|
|
// NewDefaultOutgoingRateLimiter returns a default rate limiter
|
|
// for outgoing traffic, using fixed burst ratio of 2
|
|
// and fixed 1 minute refresh interval
|
|
func NewDefaultOutgoingRateLimiter(
|
|
rateFn RateFn,
|
|
) *DynamicRateLimiterImpl {
|
|
return NewDynamicRateLimiter(
|
|
NewDefaultOutgoingRateBurst(rateFn),
|
|
defaultRefreshInterval,
|
|
)
|
|
}
|
|
|
|
// NewDefaultRateLimiter returns a default rate limiter with a dynamic burst ratio
|
|
// and fixed 1 minute refresh interval
|
|
func NewDefaultRateLimiter(
|
|
rateFn RateFn,
|
|
burstRatioFn BurstRatioFn,
|
|
) *DynamicRateLimiterImpl {
|
|
return NewDynamicRateLimiter(
|
|
NewDefaultRateBurst(rateFn, burstRatioFn),
|
|
defaultRefreshInterval,
|
|
)
|
|
}
|
|
|
|
// Allow immediately returns with true or false indicating if a rate limit
|
|
// token is available or not
|
|
func (d *DynamicRateLimiterImpl) Allow() bool {
|
|
d.maybeRefresh()
|
|
return d.rateLimiter.Allow()
|
|
}
|
|
|
|
// AllowN immediately returns with true or false indicating if n rate limit
|
|
// token is available or not
|
|
func (d *DynamicRateLimiterImpl) AllowN(now time.Time, numToken int) bool {
|
|
d.maybeRefresh()
|
|
return d.rateLimiter.AllowN(now, numToken)
|
|
}
|
|
|
|
// Reserve reserves a rate limit token
|
|
func (d *DynamicRateLimiterImpl) Reserve() Reservation {
|
|
d.maybeRefresh()
|
|
return d.rateLimiter.Reserve()
|
|
}
|
|
|
|
// ReserveN reserves n rate limit token
|
|
func (d *DynamicRateLimiterImpl) ReserveN(now time.Time, numToken int) Reservation {
|
|
d.maybeRefresh()
|
|
return d.rateLimiter.ReserveN(now, numToken)
|
|
}
|
|
|
|
// Wait waits up till deadline for a rate limit token
|
|
func (d *DynamicRateLimiterImpl) Wait(ctx context.Context) error {
|
|
d.maybeRefresh()
|
|
return d.rateLimiter.Wait(ctx)
|
|
}
|
|
|
|
// WaitN waits up till deadline for n rate limit token
|
|
func (d *DynamicRateLimiterImpl) WaitN(ctx context.Context, numToken int) error {
|
|
d.maybeRefresh()
|
|
return d.rateLimiter.WaitN(ctx, numToken)
|
|
}
|
|
|
|
// Rate returns the rate per second for this rate limiter
|
|
func (d *DynamicRateLimiterImpl) Rate() float64 {
|
|
return d.rateLimiter.Rate()
|
|
}
|
|
|
|
// Burst returns the burst for this rate limiter
|
|
func (d *DynamicRateLimiterImpl) Burst() int {
|
|
return d.rateLimiter.Burst()
|
|
}
|
|
|
|
func (d *DynamicRateLimiterImpl) Refresh() {
|
|
d.rateLimiter.SetRateBurst(d.rateBurstFn.Rate(), d.rateBurstFn.Burst())
|
|
}
|
|
|
|
func (d *DynamicRateLimiterImpl) maybeRefresh() {
|
|
select {
|
|
case <-d.refreshTimer.C:
|
|
d.refreshTimer.Reset(d.refreshInterval)
|
|
d.Refresh()
|
|
|
|
default:
|
|
// noop
|
|
}
|
|
}
|
|
|
|
func (d *DynamicRateLimiterImpl) TokensAt(t time.Time) int {
|
|
return d.rateLimiter.TokensAt(t)
|
|
}
|
|
|
|
// RecycleToken returns a token to the rate limiter
|
|
func (d *DynamicRateLimiterImpl) RecycleToken() {
|
|
d.rateLimiter.RecycleToken()
|
|
}
|