mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
Full module path rebrand from Temporal to Hanzo Tasks: - go.mod module: github.com/hanzoai/tasks - All internal Go imports updated (1906 files) - cmd/server renamed to cmd/tasksd - Binary name: tasksd (was temporal-server) - Docker images: hanzoai/tasks (was temporalio/server) - README rewritten with Hanzo Tasks branding - LLM.md created with integration notes - Makefile, goreleaser, CI workflows updated - go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos) Fork of Temporal.io (MIT license).
160 lines
5.1 KiB
Go
160 lines
5.1 KiB
Go
package client
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/hanzoai/tasks/common/dynamicconfig"
|
|
"github.com/hanzoai/tasks/common/log"
|
|
"github.com/hanzoai/tasks/common/log/tag"
|
|
"github.com/hanzoai/tasks/common/metrics"
|
|
"github.com/hanzoai/tasks/common/persistence"
|
|
"github.com/hanzoai/tasks/common/quotas"
|
|
)
|
|
|
|
const (
|
|
DefaultRefreshInterval = 10 * time.Second
|
|
DefaultRateBurstRatio = 1.0
|
|
DefaultInitialRateMultiplier = 1.0
|
|
)
|
|
|
|
type (
|
|
HealthRequestRateLimiterImpl struct {
|
|
enabled atomic.Bool
|
|
params DynamicRateLimitingParams // dynamic config struct
|
|
curOptions atomic.Pointer[dynamicconfig.DynamicRateLimitingParams] // current dynamic config values (updated on refresh)
|
|
|
|
rateLimiter *quotas.RateLimiterImpl
|
|
healthSignals persistence.HealthSignalAggregator
|
|
|
|
refreshTimer *time.Ticker
|
|
|
|
rateBurst quotas.RateBurst
|
|
|
|
curRateMultiplier atomic.Pointer[float64]
|
|
|
|
metricsHandler metrics.Handler
|
|
logger log.Logger
|
|
}
|
|
)
|
|
|
|
var _ quotas.RequestRateLimiter = (*HealthRequestRateLimiterImpl)(nil)
|
|
|
|
func NewHealthRequestRateLimiterImpl(
|
|
healthSignals persistence.HealthSignalAggregator,
|
|
rateFn quotas.RateFn,
|
|
params DynamicRateLimitingParams,
|
|
burstRatio PersistenceBurstRatio,
|
|
metricsHandler metrics.Handler,
|
|
logger log.Logger,
|
|
) *HealthRequestRateLimiterImpl {
|
|
rateBurst := quotas.NewDefaultRateBurst(rateFn, quotas.BurstRatioFn(burstRatio))
|
|
limiter := &HealthRequestRateLimiterImpl{
|
|
enabled: atomic.Bool{},
|
|
rateLimiter: quotas.NewRateLimiter(rateBurst.Rate(), rateBurst.Burst()),
|
|
healthSignals: healthSignals,
|
|
rateBurst: rateBurst,
|
|
params: params,
|
|
refreshTimer: time.NewTicker(DefaultRefreshInterval),
|
|
metricsHandler: metricsHandler,
|
|
logger: logger,
|
|
}
|
|
curRateMultiplier := new(float64)
|
|
*curRateMultiplier = DefaultInitialRateMultiplier
|
|
limiter.curRateMultiplier.Store(curRateMultiplier)
|
|
limiter.refreshDynamicParams()
|
|
return limiter
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) Allow(now time.Time, request quotas.Request) bool {
|
|
rl.maybeRefresh()
|
|
if !rl.enabled.Load() {
|
|
return true
|
|
}
|
|
return rl.rateLimiter.AllowN(now, request.Token)
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) Reserve(now time.Time, request quotas.Request) quotas.Reservation {
|
|
rl.maybeRefresh()
|
|
if !rl.enabled.Load() {
|
|
return quotas.NoopReservation
|
|
}
|
|
return rl.rateLimiter.ReserveN(now, request.Token)
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) Wait(ctx context.Context, request quotas.Request) error {
|
|
rl.maybeRefresh()
|
|
if !rl.enabled.Load() {
|
|
return nil
|
|
}
|
|
return rl.rateLimiter.WaitN(ctx, request.Token)
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) maybeRefresh() {
|
|
select {
|
|
case <-rl.refreshTimer.C:
|
|
rl.refreshDynamicParams()
|
|
if rl.enabled.Load() {
|
|
rl.refreshRate()
|
|
}
|
|
rl.updateRefreshTimer()
|
|
|
|
default:
|
|
// no-op
|
|
}
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) refreshRate() {
|
|
curOptions := *rl.curOptions.Load()
|
|
curRateMultiplier := *rl.curRateMultiplier.Load()
|
|
if rl.latencyThresholdExceeded() || rl.errorThresholdExceeded() {
|
|
// limit exceeded, do backoff
|
|
curRateMultiplier = math.Max(curOptions.RateMultiMin, curRateMultiplier-curOptions.RateBackoffStepSize)
|
|
metrics.DynamicRateLimiterMultiplier.With(rl.metricsHandler).Record(curRateMultiplier)
|
|
rl.logger.Info(
|
|
"Health threshold exceeded, reducing rate limit.",
|
|
tag.Float64("newMulti", curRateMultiplier),
|
|
tag.Float64("newRate", rl.rateLimiter.Rate()),
|
|
tag.Float64("latencyAvg", rl.healthSignals.AverageLatency()),
|
|
tag.Float64("errorRatio", rl.healthSignals.ErrorRatio()),
|
|
)
|
|
} else if curRateMultiplier < curOptions.RateMultiMax {
|
|
// already doing backoff and under thresholds, increase limit
|
|
curRateMultiplier = math.Min(curOptions.RateMultiMax, curRateMultiplier+curOptions.RateIncreaseStepSize)
|
|
metrics.DynamicRateLimiterMultiplier.With(rl.metricsHandler).Record(curRateMultiplier)
|
|
rl.logger.Info(
|
|
"System healthy, increasing rate limit.",
|
|
tag.Float64("newMulti", curRateMultiplier),
|
|
tag.Float64("newRate", rl.rateLimiter.Rate()),
|
|
tag.Float64("latencyAvg", rl.healthSignals.AverageLatency()),
|
|
tag.Float64("errorRatio", rl.healthSignals.ErrorRatio()),
|
|
)
|
|
}
|
|
rl.curRateMultiplier.Store(&curRateMultiplier)
|
|
// Always set rate to pickup changes to underlying rate limit dynamic config
|
|
rl.rateLimiter.SetRPS(curRateMultiplier * rl.rateBurst.Rate())
|
|
rl.rateLimiter.SetBurst(int(curRateMultiplier * float64(rl.rateBurst.Burst())))
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) refreshDynamicParams() {
|
|
options := rl.params()
|
|
rl.enabled.Store(options.Enabled)
|
|
rl.curOptions.Store(&options)
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) updateRefreshTimer() {
|
|
rl.refreshTimer.Reset(rl.curOptions.Load().RefreshInterval)
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) latencyThresholdExceeded() bool {
|
|
curOptions := *rl.curOptions.Load()
|
|
return curOptions.LatencyThreshold > 0 && rl.healthSignals.AverageLatency() > curOptions.LatencyThreshold
|
|
}
|
|
|
|
func (rl *HealthRequestRateLimiterImpl) errorThresholdExceeded() bool {
|
|
curOptions := *rl.curOptions.Load()
|
|
return curOptions.ErrorThreshold > 0 && rl.healthSignals.ErrorRatio() > curOptions.ErrorThreshold
|
|
}
|