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 -->
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`.
131 lines
2.4 KiB
Go
131 lines
2.4 KiB
Go
package quotas
|
|
|
|
import (
|
|
"math"
|
|
"sync/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.Uint64
|
|
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 {
|
|
d := &MutableRateBurstImpl{}
|
|
d.SetRPS(rate)
|
|
d.SetBurst(burst)
|
|
|
|
return d
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) SetRPS(rate float64) {
|
|
d.rate.Store(math.Float64bits(rate))
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) SetBurst(burst int) {
|
|
d.burst.Store(int64(burst))
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) Rate() float64 {
|
|
return math.Float64frombits(d.rate.Load())
|
|
}
|
|
|
|
func (d *MutableRateBurstImpl) Burst() int {
|
|
return int(d.burst.Load())
|
|
}
|