Files
tasks/service/matching/fairness_util.go
David Reiss 0108f692b9 Matching fairness: per-key rate limits (#8038)
## What changed?
- Add support for per-key rate limits that are scaled by weight (so
maintaining dispatch in the same order that tasks are read in).
- Refactor simpleLimiter to split parameters and ready time.

## Why?
Support simple per-key limits.

This uses an LRU cache to track recent tasks by fairness key, default
size 2000. With target dispatch rate of 1000 tasks/second/partition,
that corresponds to at least 2 seconds of keys (equal in the worst case
of fully uniform key distribution, which is unlikely). That means that
rates >= 0.5 tasks/second/key/partition should be handled accurately (2
tasks/second/key for default 4 partitions). To handle even lower rates,
partition count and task size can be tuned.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [x] covered by existing tests
- [x] added new unit test(s)
- [ ] added new functional test(s)
2025-07-22 16:43:59 +01:00

27 lines
596 B
Go

package matching
import commonpb "go.temporal.io/api/common/v1"
const (
// minWeight * strideFactor must be >= 1
strideFactor = 1000
minWeight = 0.001
)
type fairnessWeightOverrides map[string]float32
func getEffectiveWeight(overrides fairnessWeightOverrides, pri *commonpb.Priority) float32 {
key := pri.GetFairnessKey()
weight, ok := overrides[key]
if !ok {
weight = pri.GetFairnessWeight()
}
// zero means default weight (1.0). negative doesn't make sense, map it to 1.0 also.
if weight <= 0.0 {
weight = 1.0
} else {
weight = max(weight, minWeight)
}
return weight
}