mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
<!-- Describe what has changed in this PR --> **What changed?** I added `quotas.DelayedRateLimiter`, which adds a delay before an input `quotas.RateLimiter` actually takes effect. I also added some utilities to make its implementation easier. The delayed rate limiter depends on the new rate limiter delegator and no-op rate limiter structs. <!-- Tell your future self why have you made these changes --> **Why?** In some cases in the history service, we want to delay rate limiters for a short period after startup. <!-- How have you verified this change? Tested locally? Added a unit test? Checked in staging env? --> **How did you test it?** There is 100% test coverage for the new code. <!-- Assuming the worst case, what can be broken when deploying this change to production? --> **Potential risks** None. These are just additions. <!-- Is this PR a hotfix candidate or require that a notification be sent to the broader community? (Yes/No) --> **Is hotfix candidate?** No.
72 lines
3.0 KiB
Go
72 lines
3.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"
|
|
"sync/atomic"
|
|
"time"
|
|
)
|
|
|
|
// 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.
|
|
type RequestRateLimiterDelegator struct {
|
|
// delegate is an atomic.Value so that it can be safely read from and written to concurrently. It stores the rate
|
|
// limiter that this rate limiter delegates to as a monomorphicRequestRateLimiter.
|
|
delegate atomic.Value
|
|
}
|
|
|
|
// monomorphicRequestRateLimiter is a workaround for the fact that the value stored in an atomic.Value must always be
|
|
// the same type, but we want to allow the rate limiter delegate to be any type that implements the RequestRateLimiter
|
|
// interface.
|
|
type monomorphicRequestRateLimiter struct {
|
|
RequestRateLimiter
|
|
}
|
|
|
|
// SetRateLimiter sets the rate limiter to delegate to.
|
|
func (d *RequestRateLimiterDelegator) SetRateLimiter(rl RequestRateLimiter) {
|
|
d.delegate.Store(monomorphicRequestRateLimiter{rl})
|
|
}
|
|
|
|
// loadDelegate returns the rate limiter that this rate limiter delegates to.
|
|
func (d *RequestRateLimiterDelegator) loadDelegate() RequestRateLimiter {
|
|
return d.delegate.Load().(RequestRateLimiter)
|
|
}
|
|
|
|
// The following methods just delegate to the underlying rate limiter.
|
|
|
|
func (d *RequestRateLimiterDelegator) Allow(now time.Time, request Request) bool {
|
|
return d.loadDelegate().Allow(now, request)
|
|
}
|
|
|
|
func (d *RequestRateLimiterDelegator) Reserve(now time.Time, request Request) Reservation {
|
|
return d.loadDelegate().Reserve(now, request)
|
|
}
|
|
|
|
func (d *RequestRateLimiterDelegator) Wait(ctx context.Context, request Request) error {
|
|
return d.loadDelegate().Wait(ctx, request)
|
|
}
|