Files
tasks/common/taskqueue/stats.go
KannanandGitHub 185b2e2c72 Extract task queue stats code into a reusable library (#9462)
### What changed?

- **Extract MergeStats and DedupPollers** to `common/taskqueue` for
reuse across matching and tests

### Why?

To reuse in other places where we need to aggregate responses from
multiple cells.

### How did you test it?
- [x] Unit tests
2026-03-13 19:49:03 +00:00

44 lines
1.1 KiB
Go

package taskqueue
import (
taskqueuepb "go.temporal.io/api/taskqueue/v1"
"google.golang.org/protobuf/types/known/durationpb"
)
// MergeStats merges from into into. Mutates into.
func MergeStats(into, from *taskqueuepb.TaskQueueStats) {
if from == nil {
return
}
into.ApproximateBacklogCount += from.ApproximateBacklogCount
into.ApproximateBacklogAge = oldestBacklogAge(into.ApproximateBacklogAge, from.ApproximateBacklogAge)
into.TasksAddRate += from.TasksAddRate
into.TasksDispatchRate += from.TasksDispatchRate
}
// DedupPollers removes duplicate pollers by identity.
func DedupPollers(pollerInfos []*taskqueuepb.PollerInfo) []*taskqueuepb.PollerInfo {
allKeys := make(map[string]bool)
var list []*taskqueuepb.PollerInfo
for _, item := range pollerInfos {
if _, value := allKeys[item.GetIdentity()]; !value {
allKeys[item.GetIdentity()] = true
list = append(list, item)
}
}
return list
}
func oldestBacklogAge(left, right *durationpb.Duration) *durationpb.Duration {
if left == nil {
left = durationpb.New(0)
}
if right == nil {
right = durationpb.New(0)
}
if left.AsDuration() > right.AsDuration() {
return left
}
return right
}