mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-11-11 23:20:56 +08:00
Only the gauge lives in visitor_subscriptions.go; the tests follow their code. Claude-Session: https://claude.ai/code/session_01DeXuhSjq5Gta5qPJgSceFe
40 lines
930 B
Go
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)
|
|
}
|
|
}
|