Documentation
¶
Overview ¶
Package quotas is a generated GoMock package.
Package quotas is a generated GoMock package.
Package quotas is a generated GoMock package.
Index ¶
- Constants
- Variables
- type BurstFn
- type BurstRatioFn
- type ClockedRateLimiter
- func (l ClockedRateLimiter) Allow() bool
- func (l ClockedRateLimiter) AllowN(now time.Time, token int) bool
- func (l ClockedRateLimiter) RecycleToken()
- func (l ClockedRateLimiter) Reserve() ClockedReservation
- func (l ClockedRateLimiter) ReserveN(now time.Time, token int) ClockedReservation
- func (l ClockedRateLimiter) SetBurstAt(t time.Time, newBurst int)
- func (l ClockedRateLimiter) SetLimitAt(t time.Time, newLimit rate.Limit)
- func (l ClockedRateLimiter) TokensAt(t time.Time) int
- func (l ClockedRateLimiter) Wait(ctx context.Context) error
- func (l ClockedRateLimiter) WaitN(ctx context.Context, token int) error
- type ClockedReservation
- type DelayedRequestRateLimiter
- type DynamicRateLimiterImpl
- func NewDefaultIncomingRateLimiter(rateFn RateFn) *DynamicRateLimiterImpl
- func NewDefaultOutgoingRateLimiter(rateFn RateFn) *DynamicRateLimiterImpl
- func NewDefaultRateLimiter(rateFn RateFn, burstRatioFn BurstRatioFn) *DynamicRateLimiterImpl
- func NewDynamicRateLimiter(rateBurstFn RateBurst, refreshInterval time.Duration) *DynamicRateLimiterImpl
- func (d *DynamicRateLimiterImpl) Allow() bool
- func (d *DynamicRateLimiterImpl) AllowN(now time.Time, numToken int) bool
- func (d *DynamicRateLimiterImpl) Burst() int
- func (d *DynamicRateLimiterImpl) Rate() float64
- func (d *DynamicRateLimiterImpl) RecycleToken()
- func (d *DynamicRateLimiterImpl) Refresh()
- func (d *DynamicRateLimiterImpl) Reserve() Reservation
- func (d *DynamicRateLimiterImpl) ReserveN(now time.Time, numToken int) Reservation
- func (d *DynamicRateLimiterImpl) TokensAt(t time.Time) int
- func (d *DynamicRateLimiterImpl) Wait(ctx context.Context) error
- func (d *DynamicRateLimiterImpl) WaitN(ctx context.Context, numToken int) error
- type MapRequestRateLimiterImpl
- type MockRateLimiter
- func (m *MockRateLimiter) Allow() bool
- func (m *MockRateLimiter) AllowN(now time.Time, numToken int) bool
- func (m *MockRateLimiter) Burst() int
- func (m *MockRateLimiter) EXPECT() *MockRateLimiterMockRecorder
- func (m *MockRateLimiter) Rate() float64
- func (m *MockRateLimiter) RecycleToken()
- func (m *MockRateLimiter) Reserve() Reservation
- func (m *MockRateLimiter) ReserveN(now time.Time, numToken int) Reservation
- func (m *MockRateLimiter) TokensAt(t time.Time) int
- func (m *MockRateLimiter) Wait(ctx context.Context) error
- func (m *MockRateLimiter) WaitN(ctx context.Context, numToken int) error
- type MockRateLimiterMockRecorder
- func (mr *MockRateLimiterMockRecorder) Allow() *gomock.Call
- func (mr *MockRateLimiterMockRecorder) AllowN(now, numToken any) *gomock.Call
- func (mr *MockRateLimiterMockRecorder) Burst() *gomock.Call
- func (mr *MockRateLimiterMockRecorder) Rate() *gomock.Call
- func (mr *MockRateLimiterMockRecorder) RecycleToken() *gomock.Call
- func (mr *MockRateLimiterMockRecorder) Reserve() *gomock.Call
- func (mr *MockRateLimiterMockRecorder) ReserveN(now, numToken any) *gomock.Call
- func (mr *MockRateLimiterMockRecorder) TokensAt(t any) *gomock.Call
- func (mr *MockRateLimiterMockRecorder) Wait(ctx any) *gomock.Call
- func (mr *MockRateLimiterMockRecorder) WaitN(ctx, numToken any) *gomock.Call
- type MockRequestRateLimiter
- func (m *MockRequestRateLimiter) Allow(now time.Time, request Request) bool
- func (m *MockRequestRateLimiter) EXPECT() *MockRequestRateLimiterMockRecorder
- func (m *MockRequestRateLimiter) Reserve(now time.Time, request Request) Reservation
- func (m *MockRequestRateLimiter) Wait(ctx context.Context, request Request) error
- type MockRequestRateLimiterMockRecorder
- type MockReservation
- type MockReservationMockRecorder
- func (mr *MockReservationMockRecorder) Cancel() *gomock.Call
- func (mr *MockReservationMockRecorder) CancelAt(now any) *gomock.Call
- func (mr *MockReservationMockRecorder) Delay() *gomock.Call
- func (mr *MockReservationMockRecorder) DelayFrom(now any) *gomock.Call
- func (mr *MockReservationMockRecorder) OK() *gomock.Call
- type MultiRateLimiterImpl
- func (rl *MultiRateLimiterImpl) Allow() bool
- func (rl *MultiRateLimiterImpl) AllowN(now time.Time, numToken int) bool
- func (rl *MultiRateLimiterImpl) Burst() int
- func (rl *MultiRateLimiterImpl) Rate() float64
- func (rl *MultiRateLimiterImpl) RecycleToken()
- func (rl *MultiRateLimiterImpl) Reserve() Reservation
- func (rl *MultiRateLimiterImpl) ReserveN(now time.Time, numToken int) Reservation
- func (rl *MultiRateLimiterImpl) TokensAt(t time.Time) int
- func (rl *MultiRateLimiterImpl) Wait(ctx context.Context) error
- func (rl *MultiRateLimiterImpl) WaitN(ctx context.Context, numToken int) error
- type MultiRequestRateLimiterImpl
- type MultiReservationImpl
- type MutableRateBurst
- type MutableRateBurstImpl
- type NamespaceBurstFn
- type NamespaceBurstRatioFn
- type NamespaceRateBurstImpl
- type NamespaceRateFn
- type NoopRequestRateLimiterImpl
- type NoopReservationImpl
- type OperatorRateBurstImpl
- type PriorityRateLimiterImpl
- type PriorityReservationImpl
- type RateBurst
- type RateBurstImpl
- type RateFn
- type RateLimiter
- type RateLimiterImpl
- func (rl *RateLimiterImpl) Burst() int
- func (rl *RateLimiterImpl) Rate() float64
- func (rl *RateLimiterImpl) RecycleToken()
- func (rl *RateLimiterImpl) Reserve() Reservation
- func (rl *RateLimiterImpl) ReserveN(now time.Time, n int) Reservation
- func (rl *RateLimiterImpl) SetBurst(burst int)
- func (rl *RateLimiterImpl) SetRPS(rps float64)
- func (rl *RateLimiterImpl) SetRateBurst(rps float64, burst int)
- func (rl *RateLimiterImpl) TokensAt(t time.Time) int
- type Request
- type RequestPriorityFn
- type RequestRateLimiter
- type RequestRateLimiterAdapterImpl
- type RequestRateLimiterDelegator
- func (d *RequestRateLimiterDelegator) Allow(now time.Time, request Request) bool
- func (d *RequestRateLimiterDelegator) Reserve(now time.Time, request Request) Reservation
- func (d *RequestRateLimiterDelegator) SetRateLimiter(rl RequestRateLimiter)
- func (d *RequestRateLimiterDelegator) Wait(ctx context.Context, request Request) error
- type RequestRateLimiterFn
- type RequestRateLimiterKeyFn
- type Reservation
- type RoutingRateLimiterImpl
Constants ¶
const (
InfDuration = rate.InfDuration
)
const (
// OperatorPriority is used to give precedence to calls coming from web UI or tctl
OperatorPriority = 0
)
Variables ¶
var ( ErrRateLimiterWaitInterrupted = errors.New("rate limiter wait interrupted") ErrRateLimiterReservationCannotBeMade = errors.New("rate limiter reservation cannot be made due to insufficient quota") ErrRateLimiterReservationWouldExceedContextDeadline = errors.New("rate limiter reservation would exceed context deadline") )
var ( DefaultIncomingBurstRatioFn = func() float64 { return defaultIncomingRateBurstRatio } DefaultOutgoingBurstRatioFn = func() float64 { return defaultOutgoingRateBurstRatio } DefaultIncomingNamespaceBurstRatioFn = func(_ string) float64 { return defaultIncomingRateBurstRatio } DefaultOutgoingNamespaceBurstRatioFn = func(_ string) float64 { return defaultOutgoingRateBurstRatio } )
var ErrNegativeDelay = errors.New("delay cannot be negative")
Functions ¶
This section is empty.
Types ¶
type BurstRatioFn ¶
type BurstRatioFn func() float64
BurstRatioFn returns a float as the ratio of burst to rate
type ClockedRateLimiter ¶
type ClockedRateLimiter struct {
// contains filtered or unexported fields
}
ClockedRateLimiter wraps a rate.Limiter with a clock.TimeSource. It is used to ensure that the rate limiter respects the time determined by the timeSource.
func NewClockedRateLimiter ¶
func NewClockedRateLimiter(rateLimiter *rate.Limiter, timeSource clock.TimeSource) ClockedRateLimiter
func (ClockedRateLimiter) Allow ¶
func (l ClockedRateLimiter) Allow() bool
func (ClockedRateLimiter) AllowN ¶
func (l ClockedRateLimiter) AllowN(now time.Time, token int) bool
func (ClockedRateLimiter) RecycleToken ¶
func (l ClockedRateLimiter) RecycleToken()
RecycleToken should be called when the action being rate limited was not completed for some reason (i.e. a task is not dispatched because it was invalid). In this case, we want to immediately unblock another process that is waiting for one token so that the actual rate of completed actions is as close to the intended rate limit as possible. If no process is waiting for a token when RecycleToken is called, this is a no-op.
func (ClockedRateLimiter) Reserve ¶
func (l ClockedRateLimiter) Reserve() ClockedReservation
func (ClockedRateLimiter) ReserveN ¶
func (l ClockedRateLimiter) ReserveN(now time.Time, token int) ClockedReservation
func (ClockedRateLimiter) SetBurstAt ¶
func (l ClockedRateLimiter) SetBurstAt(t time.Time, newBurst int)
func (ClockedRateLimiter) SetLimitAt ¶
func (l ClockedRateLimiter) SetLimitAt(t time.Time, newLimit rate.Limit)
func (ClockedRateLimiter) WaitN ¶
func (l ClockedRateLimiter) WaitN(ctx context.Context, token int) error
WaitN is the only method that is different from rate.Limiter. We need to fully reimplement this method because the original method uses time.Now(), and does not allow us to pass in a time.Time. Fortunately, it can be built on top of ReserveN. However, there are some optimizations that we can make.
type ClockedReservation ¶
type ClockedReservation struct {
// contains filtered or unexported fields
}
ClockedReservation wraps a rate.Reservation with a clockwork.Clock. It is used to ensure that the reservation respects the time determined by the timeSource.
func (ClockedReservation) Cancel ¶
func (r ClockedReservation) Cancel()
func (ClockedReservation) CancelAt ¶
func (r ClockedReservation) CancelAt(t time.Time)
func (ClockedReservation) Delay ¶
func (r ClockedReservation) Delay() time.Duration
func (ClockedReservation) DelayFrom ¶
func (r ClockedReservation) DelayFrom(t time.Time) time.Duration
func (ClockedReservation) OK ¶
func (r ClockedReservation) OK() bool
type DelayedRequestRateLimiter ¶
type DelayedRequestRateLimiter struct {
// RequestRateLimiter is the delegate that we switch to after the delay expires.
RequestRateLimiter
// contains filtered or unexported fields
}
DelayedRequestRateLimiter is a rate limiter that allows all requests without any delay for a given duration. After the delay expires, it delegates to another rate limiter. This rate limiter is useful for cases where you want to allow all requests for a given duration, e.g. during something volatile like a deployment, and then switch to another rate limiter after the duration expires.
func NewDelayedRequestRateLimiter ¶
func NewDelayedRequestRateLimiter( rl RequestRateLimiter, delay time.Duration, timeSource clock.TimeSource, ) (*DelayedRequestRateLimiter, error)
NewDelayedRequestRateLimiter returns a DelayedRequestRateLimiter that delegates to the given rate limiter after a delay. The timeSource is used to create the timer that triggers the switch. It returns an error if the given delay is negative.
func (*DelayedRequestRateLimiter) Cancel ¶
func (rl *DelayedRequestRateLimiter) Cancel() bool
Cancel stops the timer that triggers the rate limiter to delegate to the underlying rate limiter. It returns true if the timer was stopped before it expired.
type DynamicRateLimiterImpl ¶
type DynamicRateLimiterImpl struct {
// contains filtered or unexported fields
}
DynamicRateLimiterImpl implements a dynamic config wrapper around the rate limiter
func NewDefaultIncomingRateLimiter ¶
func NewDefaultIncomingRateLimiter( rateFn RateFn, ) *DynamicRateLimiterImpl
NewDefaultIncomingRateLimiter returns a default rate limiter for incoming traffic, using fixed burst ratio of 2 and fixed 1 minute refresh interval
func NewDefaultOutgoingRateLimiter ¶
func NewDefaultOutgoingRateLimiter( rateFn RateFn, ) *DynamicRateLimiterImpl
NewDefaultOutgoingRateLimiter returns a default rate limiter for outgoing traffic, using fixed burst ratio of 2 and fixed 1 minute refresh interval
func NewDefaultRateLimiter ¶
func NewDefaultRateLimiter( rateFn RateFn, burstRatioFn BurstRatioFn, ) *DynamicRateLimiterImpl
NewDefaultRateLimiter returns a default rate limiter with a dynamic burst ratio and fixed 1 minute refresh interval
func NewDynamicRateLimiter ¶
func NewDynamicRateLimiter( rateBurstFn RateBurst, refreshInterval time.Duration, ) *DynamicRateLimiterImpl
NewDynamicRateLimiter returns a rate limiter which handles dynamic config
func (*DynamicRateLimiterImpl) Allow ¶
func (d *DynamicRateLimiterImpl) Allow() bool
Allow immediately returns with true or false indicating if a rate limit token is available or not
func (*DynamicRateLimiterImpl) AllowN ¶
func (d *DynamicRateLimiterImpl) AllowN(now time.Time, numToken int) bool
AllowN immediately returns with true or false indicating if n rate limit token is available or not
func (*DynamicRateLimiterImpl) Burst ¶
func (d *DynamicRateLimiterImpl) Burst() int
Burst returns the burst for this rate limiter
func (*DynamicRateLimiterImpl) Rate ¶
func (d *DynamicRateLimiterImpl) Rate() float64
Rate returns the rate per second for this rate limiter
func (*DynamicRateLimiterImpl) RecycleToken ¶
func (d *DynamicRateLimiterImpl) RecycleToken()
RecycleToken returns a token to the rate limiter
func (*DynamicRateLimiterImpl) Refresh ¶
func (d *DynamicRateLimiterImpl) Refresh()
func (*DynamicRateLimiterImpl) Reserve ¶
func (d *DynamicRateLimiterImpl) Reserve() Reservation
Reserve reserves a rate limit token
func (*DynamicRateLimiterImpl) ReserveN ¶
func (d *DynamicRateLimiterImpl) ReserveN(now time.Time, numToken int) Reservation
ReserveN reserves n rate limit token
func (*DynamicRateLimiterImpl) TokensAt ¶
func (d *DynamicRateLimiterImpl) TokensAt(t time.Time) int
type MapRequestRateLimiterImpl ¶
type MapRequestRateLimiterImpl[K comparable] struct { sync.RWMutex // contains filtered or unexported fields }
MapRequestRateLimiterImpl is a generic wrapper rate limiter for a set of rate limiters identified by a key. It evicts entries idle past the TTL; eviction is traffic-triggered (once per interval) and swept in a short-lived goroutine.
func NewMapRequestRateLimiter ¶
func NewMapRequestRateLimiter[K comparable]( rateLimiterGenFn RequestRateLimiterFn, rateLimiterKeyFn RequestRateLimiterKeyFn[K], ) *MapRequestRateLimiterImpl[K]
func NewNamespaceRequestRateLimiter ¶
func NewNamespaceRequestRateLimiter( rateLimiterGenFn RequestRateLimiterFn, ) *MapRequestRateLimiterImpl[string]
func (*MapRequestRateLimiterImpl[_]) Allow ¶
Allow attempts to allow a request to go through. The method returns immediately with a true or false indicating if the request can make progress
type MockRateLimiter ¶
type MockRateLimiter struct {
// contains filtered or unexported fields
}
MockRateLimiter is a mock of RateLimiter interface.
func NewMockRateLimiter ¶
func NewMockRateLimiter(ctrl *gomock.Controller) *MockRateLimiter
NewMockRateLimiter creates a new mock instance.
func (*MockRateLimiter) AllowN ¶
func (m *MockRateLimiter) AllowN(now time.Time, numToken int) bool
AllowN mocks base method.
func (*MockRateLimiter) EXPECT ¶
func (m *MockRateLimiter) EXPECT() *MockRateLimiterMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockRateLimiter) RecycleToken ¶
func (m *MockRateLimiter) RecycleToken()
RecycleToken mocks base method.
func (*MockRateLimiter) Reserve ¶
func (m *MockRateLimiter) Reserve() Reservation
Reserve mocks base method.
func (*MockRateLimiter) ReserveN ¶
func (m *MockRateLimiter) ReserveN(now time.Time, numToken int) Reservation
ReserveN mocks base method.
func (*MockRateLimiter) TokensAt ¶
func (m *MockRateLimiter) TokensAt(t time.Time) int
TokensAt mocks base method.
type MockRateLimiterMockRecorder ¶
type MockRateLimiterMockRecorder struct {
// contains filtered or unexported fields
}
MockRateLimiterMockRecorder is the mock recorder for MockRateLimiter.
func (*MockRateLimiterMockRecorder) Allow ¶
func (mr *MockRateLimiterMockRecorder) Allow() *gomock.Call
Allow indicates an expected call of Allow.
func (*MockRateLimiterMockRecorder) AllowN ¶
func (mr *MockRateLimiterMockRecorder) AllowN(now, numToken any) *gomock.Call
AllowN indicates an expected call of AllowN.
func (*MockRateLimiterMockRecorder) Burst ¶
func (mr *MockRateLimiterMockRecorder) Burst() *gomock.Call
Burst indicates an expected call of Burst.
func (*MockRateLimiterMockRecorder) Rate ¶
func (mr *MockRateLimiterMockRecorder) Rate() *gomock.Call
Rate indicates an expected call of Rate.
func (*MockRateLimiterMockRecorder) RecycleToken ¶
func (mr *MockRateLimiterMockRecorder) RecycleToken() *gomock.Call
RecycleToken indicates an expected call of RecycleToken.
func (*MockRateLimiterMockRecorder) Reserve ¶
func (mr *MockRateLimiterMockRecorder) Reserve() *gomock.Call
Reserve indicates an expected call of Reserve.
func (*MockRateLimiterMockRecorder) ReserveN ¶
func (mr *MockRateLimiterMockRecorder) ReserveN(now, numToken any) *gomock.Call
ReserveN indicates an expected call of ReserveN.
func (*MockRateLimiterMockRecorder) TokensAt ¶
func (mr *MockRateLimiterMockRecorder) TokensAt(t any) *gomock.Call
TokensAt indicates an expected call of TokensAt.
type MockRequestRateLimiter ¶
type MockRequestRateLimiter struct {
// contains filtered or unexported fields
}
MockRequestRateLimiter is a mock of RequestRateLimiter interface.
func NewMockRequestRateLimiter ¶
func NewMockRequestRateLimiter(ctrl *gomock.Controller) *MockRequestRateLimiter
NewMockRequestRateLimiter creates a new mock instance.
func (*MockRequestRateLimiter) Allow ¶
func (m *MockRequestRateLimiter) Allow(now time.Time, request Request) bool
Allow mocks base method.
func (*MockRequestRateLimiter) EXPECT ¶
func (m *MockRequestRateLimiter) EXPECT() *MockRequestRateLimiterMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockRequestRateLimiter) Reserve ¶
func (m *MockRequestRateLimiter) Reserve(now time.Time, request Request) Reservation
Reserve mocks base method.
type MockRequestRateLimiterMockRecorder ¶
type MockRequestRateLimiterMockRecorder struct {
// contains filtered or unexported fields
}
MockRequestRateLimiterMockRecorder is the mock recorder for MockRequestRateLimiter.
func (*MockRequestRateLimiterMockRecorder) Allow ¶
func (mr *MockRequestRateLimiterMockRecorder) Allow(now, request any) *gomock.Call
Allow indicates an expected call of Allow.
type MockReservation ¶
type MockReservation struct {
// contains filtered or unexported fields
}
MockReservation is a mock of Reservation interface.
func NewMockReservation ¶
func NewMockReservation(ctrl *gomock.Controller) *MockReservation
NewMockReservation creates a new mock instance.
func (*MockReservation) CancelAt ¶
func (m *MockReservation) CancelAt(now time.Time)
CancelAt mocks base method.
func (*MockReservation) Delay ¶
func (m *MockReservation) Delay() time.Duration
Delay mocks base method.
func (*MockReservation) DelayFrom ¶
func (m *MockReservation) DelayFrom(now time.Time) time.Duration
DelayFrom mocks base method.
func (*MockReservation) EXPECT ¶
func (m *MockReservation) EXPECT() *MockReservationMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
type MockReservationMockRecorder ¶
type MockReservationMockRecorder struct {
// contains filtered or unexported fields
}
MockReservationMockRecorder is the mock recorder for MockReservation.
func (*MockReservationMockRecorder) Cancel ¶
func (mr *MockReservationMockRecorder) Cancel() *gomock.Call
Cancel indicates an expected call of Cancel.
func (*MockReservationMockRecorder) CancelAt ¶
func (mr *MockReservationMockRecorder) CancelAt(now any) *gomock.Call
CancelAt indicates an expected call of CancelAt.
func (*MockReservationMockRecorder) Delay ¶
func (mr *MockReservationMockRecorder) Delay() *gomock.Call
Delay indicates an expected call of Delay.
func (*MockReservationMockRecorder) DelayFrom ¶
func (mr *MockReservationMockRecorder) DelayFrom(now any) *gomock.Call
DelayFrom indicates an expected call of DelayFrom.
func (*MockReservationMockRecorder) OK ¶
func (mr *MockReservationMockRecorder) OK() *gomock.Call
OK indicates an expected call of OK.
type MultiRateLimiterImpl ¶
type MultiRateLimiterImpl struct {
// contains filtered or unexported fields
}
MultiRateLimiterImpl is a wrapper around the limiter interface
func NewMultiRateLimiter ¶
func NewMultiRateLimiter( rateLimiters []RateLimiter, ) *MultiRateLimiterImpl
NewMultiRateLimiter returns a new rate limiter that have multiple stage
func (*MultiRateLimiterImpl) Allow ¶
func (rl *MultiRateLimiterImpl) Allow() bool
Allow immediately returns with true or false indicating if a rate limit token is available or not
func (*MultiRateLimiterImpl) AllowN ¶
func (rl *MultiRateLimiterImpl) AllowN(now time.Time, numToken int) bool
AllowN immediately returns with true or false indicating if n rate limit token is available or not
func (*MultiRateLimiterImpl) Burst ¶
func (rl *MultiRateLimiterImpl) Burst() int
Burst returns the minimum burst for this rate limiter
func (*MultiRateLimiterImpl) Rate ¶
func (rl *MultiRateLimiterImpl) Rate() float64
Rate returns the minimum rate per second for this rate limiter
func (*MultiRateLimiterImpl) RecycleToken ¶
func (rl *MultiRateLimiterImpl) RecycleToken()
RecycleToken should be called when the action being rate limited was not completed for some reason (i.e. a task is not dispatched because it was invalid). In this case, we want to immediately unblock another process that is waiting for one token so that the actual rate of completed actions is as close to the intended rate limit as possible. If no process is waiting for a token when RecycleToken is called, this is a no-op.
For most rate limiters, recycle token is implemented in the base level ClockedRateLimiter, but because MultiRateLimiter implements WaitN by calling ReserveN on all its sub-rate-limiters, instead of WaitN, there is no one waiting on ClockedRateLimiter's recycle token channel. So MultiRateLimiter needs its own recycle method and channel.
Since we don't know how many tokens were reserved by the process calling recycle, we will only unblock new reservations that are for one token (otherwise we could recycle a 1-token-reservation and unblock a 100-token-reservation). If all waiting processes are waiting for >1 tokens, this is a no-op.
Because recycleCh is an unbuffered channel, the token will be reused for the next waiter only if there exists a waiter at the time RecycleToken is called. Usually the attempted rate is consistently above or below the limit for a period of time, so if rate limiting is in effect and recycling matters, most likely there will be a waiter. If the actual rate is erratically bouncing to either side of the rate limit AND we perform many recycles, this will drop some recycled tokens. If that situation turns out to be common, we may want to make it a buffered channel instead.
Our goal is to ensure that each token in our bucket is used every second, meaning the time between taking and successfully using a token must be <= 1s. For this to be true, we must have:
time_to_recycle * number_of_recycles_per_second <= 1s time_to_recycle * probability_of_recycle * number_of_attempts_per_second <= 1s
Therefore, it is also possible for this strategy to be inaccurate if the delay between taking and successfully using a token is greater than one second.
Currently, RecycleToken is called when we take a token to attempt a matching task dispatch and then later find out (usually via RPC to History) that the task should not be dispatched. If history rpc takes 10ms --> 100 opportunities for the token to be used that second --> 99% recycle probability is ok. If recycle probability is 50% --> need at least 2 opportunities for token to be used --> 500ms history rpc time is ok. Note that the task forwarder rate limit impacts the rate of recycle, which can add inaccuracy.
func (*MultiRateLimiterImpl) Reserve ¶
func (rl *MultiRateLimiterImpl) Reserve() Reservation
Reserve returns a Reservation that indicates how long the caller must wait before event happen.
func (*MultiRateLimiterImpl) ReserveN ¶
func (rl *MultiRateLimiterImpl) ReserveN(now time.Time, numToken int) Reservation
ReserveN calls ReserveN on its list of rate limiters and returns a MultiReservation that is a list of the individual reservation objects indicating how long the caller must wait before the event can happen.
type MultiRequestRateLimiterImpl ¶
type MultiRequestRateLimiterImpl struct {
// contains filtered or unexported fields
}
func NewMultiRequestRateLimiter ¶
func NewMultiRequestRateLimiter( requestRateLimiters ...RequestRateLimiter, ) *MultiRequestRateLimiterImpl
func (*MultiRequestRateLimiterImpl) Allow ¶
func (rl *MultiRequestRateLimiterImpl) Allow(now time.Time, request Request) bool
func (*MultiRequestRateLimiterImpl) Reserve ¶
func (rl *MultiRequestRateLimiterImpl) Reserve(now time.Time, request Request) Reservation
type MultiReservationImpl ¶
type MultiReservationImpl struct {
// contains filtered or unexported fields
}
func NewMultiReservation ¶
func NewMultiReservation( ok bool, reservations []Reservation, ) *MultiReservationImpl
func (*MultiReservationImpl) Cancel ¶
func (r *MultiReservationImpl) Cancel()
Cancel indicates that the reservation holder will not perform the reserved action and reverses the effects of this Reservation on the rate limit as much as possible
func (*MultiReservationImpl) CancelAt ¶
func (r *MultiReservationImpl) CancelAt(now time.Time)
CancelAt indicates that the reservation holder will not perform the reserved action and reverses the effects of this Reservation on the rate limit as much as possible
func (*MultiReservationImpl) Delay ¶
func (r *MultiReservationImpl) Delay() time.Duration
Delay returns the duration for which the reservation holder must wait before taking the reserved action. Zero duration means act immediately.
func (*MultiReservationImpl) DelayFrom ¶
func (r *MultiReservationImpl) DelayFrom(now time.Time) time.Duration
DelayFrom returns the duration for which the reservation holder must wait before taking the reserved action. Zero duration means act immediately. MultiReservation DelayFrom returns the maximum delay of all its sub-reservations.
func (*MultiReservationImpl) OK ¶
func (r *MultiReservationImpl) OK() bool
OK returns whether the limiter can provide the requested number of tokens
type MutableRateBurst ¶
type MutableRateBurstImpl ¶
type MutableRateBurstImpl struct {
// contains filtered or unexported fields
}
MutableRateBurstImpl stores the dynamic rate & burst for rate limiter
func NewMutableRateBurst ¶
func NewMutableRateBurst( rate float64, burst int, ) *MutableRateBurstImpl
func (*MutableRateBurstImpl) Burst ¶
func (d *MutableRateBurstImpl) Burst() int
func (*MutableRateBurstImpl) Rate ¶
func (d *MutableRateBurstImpl) Rate() float64
func (*MutableRateBurstImpl) SetBurst ¶
func (d *MutableRateBurstImpl) SetBurst(burst int)
func (*MutableRateBurstImpl) SetRPS ¶
func (d *MutableRateBurstImpl) SetRPS(rate float64)
type NamespaceBurstFn ¶
NamespaceBurstFn returns an int as the burst / bucket size for the given namespace
type NamespaceBurstRatioFn ¶
NamespaceBurstRatioFn returns a float as the ratio of burst to rate for the given namespace
type NamespaceRateBurstImpl ¶
type NamespaceRateBurstImpl struct {
// contains filtered or unexported fields
}
func NewNamespaceRateBurst ¶
func NewNamespaceRateBurst( namespaceName string, rateFn NamespaceRateFn, burstRatioFn NamespaceBurstRatioFn, ) *NamespaceRateBurstImpl
func (*NamespaceRateBurstImpl) Burst ¶
func (n *NamespaceRateBurstImpl) Burst() int
func (*NamespaceRateBurstImpl) Rate ¶
func (n *NamespaceRateBurstImpl) Rate() float64
type NamespaceRateFn ¶
NamespaceRateFn returns a float64 as the RPS for the given namespace
type NoopRequestRateLimiterImpl ¶
type NoopRequestRateLimiterImpl struct{}
NoopRequestRateLimiterImpl is a no-op implementation for RequestRateLimiter
func (*NoopRequestRateLimiterImpl) Allow ¶
func (r *NoopRequestRateLimiterImpl) Allow( _ time.Time, _ Request, ) bool
func (*NoopRequestRateLimiterImpl) Reserve ¶
func (r *NoopRequestRateLimiterImpl) Reserve( _ time.Time, _ Request, ) Reservation
type NoopReservationImpl ¶
type NoopReservationImpl struct{}
func (*NoopReservationImpl) Cancel ¶
func (r *NoopReservationImpl) Cancel()
Cancel indicates that the reservation holder will not perform the reserved action and reverses the effects of this Reservation on the rate limit as much as possible
func (*NoopReservationImpl) CancelAt ¶
func (r *NoopReservationImpl) CancelAt(_ time.Time)
CancelAt indicates that the reservation holder will not perform the reserved action and reverses the effects of this Reservation on the rate limit as much as possible
func (*NoopReservationImpl) Delay ¶
func (r *NoopReservationImpl) Delay() time.Duration
Delay returns the duration for which the reservation holder must wait before taking the reserved action. Zero duration means act immediately.
func (*NoopReservationImpl) DelayFrom ¶
func (r *NoopReservationImpl) DelayFrom(_ time.Time) time.Duration
DelayFrom returns the duration for which the reservation holder must wait before taking the reserved action. Zero duration means act immediately.
func (*NoopReservationImpl) OK ¶
func (r *NoopReservationImpl) OK() bool
OK returns whether the limiter can provide the requested number of tokens
type OperatorRateBurstImpl ¶
type OperatorRateBurstImpl struct {
// contains filtered or unexported fields
}
func NewOperatorRateBurst ¶
func NewOperatorRateBurst( baseRateBurstFn RateBurst, operatorRateRatio func() float64, ) *OperatorRateBurstImpl
func (*OperatorRateBurstImpl) Burst ¶
func (c *OperatorRateBurstImpl) Burst() int
func (*OperatorRateBurstImpl) Rate ¶
func (c *OperatorRateBurstImpl) Rate() float64
type PriorityRateLimiterImpl ¶
type PriorityRateLimiterImpl struct {
// contains filtered or unexported fields
}
PriorityRateLimiterImpl is a wrapper around the golang rate limiter
func NewPriorityRateLimiter ¶
func NewPriorityRateLimiter( requestPriorityFn RequestPriorityFn, priorityToRateLimiters map[int]RequestRateLimiter, ) *PriorityRateLimiterImpl
NewPriorityRateLimiter returns a new rate limiter that can handle dynamic configuration updates
func (*PriorityRateLimiterImpl) Allow ¶
func (p *PriorityRateLimiterImpl) Allow( now time.Time, request Request, ) bool
func (*PriorityRateLimiterImpl) Reserve ¶
func (p *PriorityRateLimiterImpl) Reserve( now time.Time, request Request, ) Reservation
type PriorityReservationImpl ¶
type PriorityReservationImpl struct {
// contains filtered or unexported fields
}
func NewPriorityReservation ¶
func NewPriorityReservation( decidingReservation Reservation, otherReservations []Reservation, ) *PriorityReservationImpl
func (*PriorityReservationImpl) Cancel ¶
func (r *PriorityReservationImpl) Cancel()
Cancel indicates that the reservation holder will not perform the reserved action and reverses the effects of this Reservation on the rate limit as much as possible
func (*PriorityReservationImpl) CancelAt ¶
func (r *PriorityReservationImpl) CancelAt(now time.Time)
CancelAt indicates that the reservation holder will not perform the reserved action and reverses the effects of this Reservation on the rate limit as much as possible
func (*PriorityReservationImpl) Delay ¶
func (r *PriorityReservationImpl) Delay() time.Duration
Delay returns the duration for which the reservation holder must wait before taking the reserved action. Zero duration means act immediately.
func (*PriorityReservationImpl) DelayFrom ¶
func (r *PriorityReservationImpl) DelayFrom(now time.Time) time.Duration
DelayFrom returns the duration for which the reservation holder must wait before taking the reserved action. Zero duration means act immediately.
func (*PriorityReservationImpl) OK ¶
func (r *PriorityReservationImpl) OK() bool
OK returns whether the limiter can provide the requested number of tokens
type RateBurstImpl ¶
type RateBurstImpl struct {
// contains filtered or unexported fields
}
func NewDefaultIncomingRateBurst ¶
func NewDefaultIncomingRateBurst( rateFn RateFn, ) *RateBurstImpl
func NewDefaultOutgoingRateBurst ¶
func NewDefaultOutgoingRateBurst( rateFn RateFn, ) *RateBurstImpl
func NewDefaultRateBurst ¶
func NewDefaultRateBurst( rateFn RateFn, rateToBurstRatio BurstRatioFn, ) *RateBurstImpl
func NewRateBurst ¶
func NewRateBurst( rateFn RateFn, burstFn BurstFn, ) *RateBurstImpl
func (*RateBurstImpl) Burst ¶
func (d *RateBurstImpl) Burst() int
func (*RateBurstImpl) Rate ¶
func (d *RateBurstImpl) Rate() float64
type RateLimiter ¶
type RateLimiter interface {
// Allow attempts to allow a request to go through. The method returns
// immediately with a true or false indicating if the request can make
// progress
Allow() bool
// AllowN attempts to allow a request to go through. The method returns
// immediately with a true or false indicating if the request can make
// progress
AllowN(now time.Time, numToken int) bool
// Reserve returns a Reservation that indicates how long the caller
// must wait before event happen.
Reserve() Reservation
// ReserveN returns a Reservation that indicates how long the caller
// must wait before event happen.
ReserveN(now time.Time, numToken int) Reservation
// Wait waits till the deadline for a rate limit token to allow the request
// to go through.
Wait(ctx context.Context) error
// WaitN waits till the deadline for n rate limit token to allow the request
// to go through.
WaitN(ctx context.Context, numToken int) error
// Rate returns the rate per second for this rate limiter
Rate() float64
// Burst returns the burst for this rate limiter
Burst() int
// TokensAt returns the number of tokens that will be available at time t
TokensAt(t time.Time) int
// RecycleToken immediately unblocks another process that is waiting for a token, if
// a waiter exists. A token should be recycled when the action being rate limited was
// not completed for some reason (i.e. a task is not dispatched because it was invalid).
RecycleToken()
}
RateLimiter corresponds to basic rate limiting functionality.
type RateLimiterImpl ¶
type RateLimiterImpl struct {
sync.RWMutex
ClockedRateLimiter
// contains filtered or unexported fields
}
RateLimiterImpl is a wrapper around the golang rate limiter
func NewRateLimiter ¶
func NewRateLimiter(newRPS float64, newBurst int) *RateLimiterImpl
NewRateLimiter returns a new rate limiter that can handle dynamic configuration updates
func (*RateLimiterImpl) Burst ¶
func (rl *RateLimiterImpl) Burst() int
Burst returns the burst for this rate limiter
func (*RateLimiterImpl) Rate ¶
func (rl *RateLimiterImpl) Rate() float64
Rate returns the rps for this rate limiter
func (*RateLimiterImpl) RecycleToken ¶
func (rl *RateLimiterImpl) RecycleToken()
RecycleToken returns a token to the rate limiter
func (*RateLimiterImpl) Reserve ¶
func (rl *RateLimiterImpl) Reserve() Reservation
func (*RateLimiterImpl) ReserveN ¶
func (rl *RateLimiterImpl) ReserveN(now time.Time, n int) Reservation
func (*RateLimiterImpl) SetBurst ¶
func (rl *RateLimiterImpl) SetBurst(burst int)
SetBurst sets the burst of the rate limiter
func (*RateLimiterImpl) SetRPS ¶
func (rl *RateLimiterImpl) SetRPS(rps float64)
SetRPS sets the rate of the rate limiter
func (*RateLimiterImpl) SetRateBurst ¶
func (rl *RateLimiterImpl) SetRateBurst(rps float64, burst int)
SetRateBurst sets the rps & burst of the rate limiter
type Request ¶
type RequestPriorityFn ¶
RequestPriorityFn returns a priority for the given Request
type RequestRateLimiter ¶
type RequestRateLimiter interface {
// Allow attempts to allow a request to go through. The method returns
// immediately with a true or false indicating if the request can make
// progress
Allow(now time.Time, request Request) bool
// Reserve returns a Reservation that indicates how long the caller
// must wait before event happen.
Reserve(now time.Time, request Request) Reservation
// Wait waits till the deadline for a rate limit token to allow the request
// to go through.
Wait(ctx context.Context, request Request) error
}
RequestRateLimiter corresponds to basic rate limiting functionality.
var NoopRequestRateLimiter RequestRateLimiter = &NoopRequestRateLimiterImpl{}
func NewPriorityRateLimiterHelper ¶
func NewPriorityRateLimiterHelper( rateBurstFn RateBurst, operatorRPSRatio func() float64, requestPriorityFn RequestPriorityFn, prioritiesOrdered []int, ) RequestRateLimiter
func NewRequestRateLimiterAdapter ¶
func NewRequestRateLimiterAdapter( rateLimiter RateLimiter, ) RequestRateLimiter
type RequestRateLimiterAdapterImpl ¶
type RequestRateLimiterAdapterImpl struct {
// contains filtered or unexported fields
}
func (*RequestRateLimiterAdapterImpl) Allow ¶
func (r *RequestRateLimiterAdapterImpl) Allow( now time.Time, request Request, ) bool
func (*RequestRateLimiterAdapterImpl) Reserve ¶
func (r *RequestRateLimiterAdapterImpl) Reserve( now time.Time, request Request, ) Reservation
type RequestRateLimiterDelegator ¶
type RequestRateLimiterDelegator struct {
// contains filtered or unexported fields
}
RequestRateLimiterDelegator is a request rate limiter that delegates to another rate limiter. The delegate can be changed at runtime by calling SetRateLimiter. This rate limiter is useful for cases where you want to substitute one rate limiter implementation for another at runtime. All methods of this type are thread-safe.
func (*RequestRateLimiterDelegator) Allow ¶
func (d *RequestRateLimiterDelegator) Allow(now time.Time, request Request) bool
func (*RequestRateLimiterDelegator) Reserve ¶
func (d *RequestRateLimiterDelegator) Reserve(now time.Time, request Request) Reservation
func (*RequestRateLimiterDelegator) SetRateLimiter ¶
func (d *RequestRateLimiterDelegator) SetRateLimiter(rl RequestRateLimiter)
SetRateLimiter sets the rate limiter to delegate to.
type RequestRateLimiterFn ¶
type RequestRateLimiterFn func(req Request) RequestRateLimiter
RequestRateLimiterFn returns generate a namespace specific rate limiter
type RequestRateLimiterKeyFn ¶
type RequestRateLimiterKeyFn[K comparable] func(req Request) K
RequestRateLimiterKeyFn extracts the map key from the request
type Reservation ¶
type Reservation interface {
// OK returns whether the limiter can provide the requested number of tokens
OK() bool
// Cancel indicates that the reservation holder will not perform the reserved action
// and reverses the effects of this Reservation on the rate limit as much as possible
Cancel()
// CancelAt indicates that the reservation holder will not perform the reserved action
// and reverses the effects of this Reservation on the rate limit as much as possible
CancelAt(now time.Time)
// Delay returns the duration for which the reservation holder must wait
// before taking the reserved action. Zero duration means act immediately.
Delay() time.Duration
// DelayFrom returns the duration for which the reservation holder must wait
// before taking the reserved action. Zero duration means act immediately.
DelayFrom(now time.Time) time.Duration
}
Reservation holds information about events that are permitted by a Limiter to happen after a delay
var NoopReservation Reservation = &NoopReservationImpl{}
type RoutingRateLimiterImpl ¶
type RoutingRateLimiterImpl struct {
// contains filtered or unexported fields
}
RoutingRateLimiterImpl is a rate limiter special built for multi-tenancy
func NewRoutingRateLimiter ¶
func NewRoutingRateLimiter( apiToRateLimiter map[string]RequestRateLimiter, ) *RoutingRateLimiterImpl
func (*RoutingRateLimiterImpl) Allow ¶
func (r *RoutingRateLimiterImpl) Allow( now time.Time, request Request, ) bool
Allow attempts to allow a request to go through. The method returns immediately with a true or false indicating if the request can make progress
func (*RoutingRateLimiterImpl) Reserve ¶
func (r *RoutingRateLimiterImpl) Reserve( now time.Time, request Request, ) Reservation
Reserve returns a Reservation that indicates how long the caller must wait before event happen.
Source Files
¶
- clocked_rate_limiter.go
- delayed_request_rate_limiter.go
- dynamic_rate_limiter_impl.go
- map_request_rate_limiter_impl.go
- multi_rate_limiter_impl.go
- multi_request_rate_limiter_impl.go
- multi_reservation_impl.go
- noop_request_rate_limiter_impl.go
- noop_reservation_impl.go
- priority.go
- priority_rate_limiter_impl.go
- priority_reservation_impl.go
- rate_burst.go
- rate_limiter.go
- rate_limiter_impl.go
- rate_limiter_mock.go
- request.go
- request_rate_limiter.go
- request_rate_limiter_adapter_impl.go
- request_rate_limiter_delegator.go
- request_rate_limiter_mock.go
- reservation.go
- reservation_mock.go
- routing_rate_limiter_impl.go