Files
tasks/common/quotas/dynamic_rate_limiter_impl.go
Alex ShtinandGitHub 8e8f1b0f7c Remove license header from every file (#7689)
## What changed?
<!-- Describe what has changed in this PR -->
Remove license header from every file. Because it is really hard to
follow in this PR here is the summary:
1. License header is removed from all `.go` and `.proto` files
:fireworks::fireworks:🎆.
2. `LICENSE` file in the root directory has only Temporal and Uber
copyrights.
3. 5 other `LICENSE` files added to the packages which have copyrights
different from Temporal and Uber: Datadog, Xargin, "Mat Ryer, Tyler
Bunnell and contributors".
4. `license_file` flag is removed from all code generation tools.
5. `copyright_file` flag is removed from `go:generate mockgen`
directive.
6. All copyright related targets are removed from `Makefile`.
7. Updated Temporal copyright year to 2025 everywhere.

## Why?
<!-- Tell your future self why have you made these changes -->
I double checked with legal department that it is not needed to have
license header in every file. One file per repo is enough. I put all
copyrights to the root `LICENSE` file and removed header from all other
files. Also updated tools and `Makefile`.
2025-05-01 18:50:21 -07:00

149 lines
3.8 KiB
Go

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()
}