package metrics

import "sync"

// PendingPairGauge keeps process-wide in-flight totals correct when a service
// has multiple workers updating the same pair of pending gauges.
type PendingPairGauge struct {
	mu     sync.Mutex
	first  int
	second int
}

func (g *PendingPairGauge) Add(registry *Registry, firstMetric string, secondMetric string, firstDelta int, secondDelta int) {
	if g == nil {
		return
	}
	g.mu.Lock()
	g.first += firstDelta
	g.second += secondDelta
	if g.first < 0 {
		g.first = 0
	}
	if g.second < 0 {
		g.second = 0
	}
	first := g.first
	second := g.second
	g.mu.Unlock()

	if registry == nil {
		return
	}
	registry.SetGauge(firstMetric, nil, float64(first))
	registry.SetGauge(secondMetric, nil, float64(second))
}
