mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
## What changed? <!-- Describe what has changed in this PR --> - Allow configuring persistence rate limiter burst ratio, default is still 1, same as today. ## Why? <!-- Tell your future self why have you made these changes --> - It's hard to set rate limit value without burst support. We allow incoming traffic to burst, but call to persistence can't burst, which often lead to persistence rate limited errors. ## How did you test it? <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> - Existing tests. ## Potential risks <!-- Assuming the worst case, what can be broken when deploying this change to production? --> - N/A, default burst is still 1, no behavior change by default. ## 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/`? --> - N/A ## Is hotfix candidate? <!-- Is this PR a hotfix candidate or does it require a notification to be sent to the broader community? (Yes/No) --> - No
153 lines
3.6 KiB
Go
153 lines
3.6 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 (
|
|
"go.uber.org/atomic"
|
|
)
|
|
|
|
type (
|
|
// RateFn returns a float64 as the RPS
|
|
RateFn func() float64
|
|
|
|
// NamespaceRateFn returns a float64 as the RPS for the given namespace
|
|
NamespaceRateFn func(namespace string) float64
|
|
|
|
// BurstFn returns an int as the burst / bucket size
|
|
BurstFn func() int
|
|
|
|
// BurstRatioFn returns a float as the ratio of burst to rate
|
|
BurstRatioFn func() float64
|
|
|
|
// NamespaceBurstFn returns an int as the burst / bucket size for the given namespace
|
|
NamespaceBurstFn func(namespace string) float64
|
|
|
|
// RateBurst returns rate & burst for rate limiter
|
|
RateBurst interface {
|
|
Rate() float64
|
|
Burst() int
|
|
}
|
|
|
|
RateBurstImpl struct {
|
|
rateFn RateFn
|
|
burstFn BurstFn
|
|
}
|
|
|
|
// MutableRateBurstImpl stores the dynamic rate & burst for rate limiter
|
|
MutableRateBurstImpl struct {
|
|
rate *atomic.Float64
|
|
burst *atomic.Int64
|
|
}
|
|
|
|
MutableRateBurst interface {
|
|
SetRPS(rps float64)
|
|
SetBurst(burst int)
|
|
RateBurst
|
|
}
|
|
)
|
|
|
|
func NewRateBurst(
|
|
rateFn RateFn,
|
|
burstFn BurstFn,
|
|
) *RateBurstImpl {
|
|
return &RateBurstImpl{
|
|
rateFn: rateFn,
|
|
burstFn: burstFn,
|
|
}
|
|
}
|
|
|
|
func NewDefaultIncomingRateBurst(
|
|
rateFn RateFn,
|
|
) *RateBurstImpl {
|
|
return NewDefaultRateBurst(rateFn, func() float64 {
|
|
return defaultIncomingRateBurstRatio
|
|
})
|
|
}
|
|
|
|
func NewDefaultOutgoingRateBurst(
|
|
rateFn RateFn,
|
|
) *RateBurstImpl {
|
|
return NewDefaultRateBurst(rateFn, func() float64 {
|
|
return defaultOutgoingRateBurstRatio
|
|
})
|
|
}
|
|
|
|
func NewDefaultRateBurst(
|
|
rateFn RateFn,
|
|
rateToBurstRatio BurstRatioFn,
|
|
) *RateBurstImpl {
|
|
burstFn := func() int {
|
|
rate := rateFn()
|
|
if rate < 0 {
|
|
rate = 0
|
|
}
|
|
|
|
ratio := rateToBurstRatio()
|
|
if ratio < 0 {
|
|
ratio = 0
|
|
}
|
|
burst := int(rate * ratio)
|
|
if burst == 0 && rate > 0 && ratio > 0 {
|
|
burst = 1
|
|
}
|
|
return burst
|
|
}
|
|
return NewRateBurst(rateFn, burstFn)
|
|
}
|
|
|
|
func (d *RateBurstImpl) Rate() float64 {
|
|
return d.rateFn()
|
|
}
|
|
|
|
func (d *RateBurstImpl) Burst() int {
|
|
return d.burstFn()
|
|
}
|
|
|
|
func NewMutableRateBurst(
|
|
rate float64,
|
|
burst int,
|
|
) *MutableRateBurstImpl {
|
|
return &MutableRateBurstImpl{
|
|
rate: atomic.NewFloat64(rate),
|
|
burst: atomic.NewInt64(int64(burst)),
|
|
}
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) SetRPS(rate float64) {
|
|
d.rate.Store(rate)
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) SetBurst(burst int) {
|
|
d.burst.Store(int64(burst))
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) Rate() float64 {
|
|
return d.rate.Load()
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) Burst() int {
|
|
return int(d.burst.Load())
|
|
}
|