Files
ntfy/server/visitor_subscriptions.go
binwiederhier 647698c607 Keep the visitor's subscription methods in visitor.go
Only the gauge lives in visitor_subscriptions.go; the tests follow their code.

Claude-Session: https://claude.ai/code/session_01DeXuhSjq5Gta5qPJgSceFe
2026-10-29 13:24:57 +00:00

40 lines
930 B
Go

package server
import (
"sync"
)
// subscriptionGauge counts the open subscriptions per visitor key on this node. It is never shared
// with other nodes: it protects this node's connections.
type subscriptionGauge struct {
counts map[string]int64
mu sync.Mutex // Protects counts
}
func newSubscriptionGauge() *subscriptionGauge {
return &subscriptionGauge{
counts: make(map[string]int64),
}
}
// Acquire counts one more subscription for the key, unless the key already has limit of them
func (g *subscriptionGauge) Acquire(key string, limit int64) bool {
g.mu.Lock()
defer g.mu.Unlock()
if g.counts[key] >= limit {
return false
}
g.counts[key]++
return true
}
// Release counts one subscription less for the key, and forgets the key when it has none
func (g *subscriptionGauge) Release(key string) {
g.mu.Lock()
defer g.mu.Unlock()
g.counts[key]--
if g.counts[key] <= 0 {
delete(g.counts, key)
}
}