Add int64 gauge abstraction
We want to serve metrics via both BouncerServ and Prometheus. Add a tiny abstraction to avoid updating multiple metrics at once.
This commit is contained in:
parent
df6b161f55
commit
7fa8c78122
31
server.go
31
server.go
@ -56,6 +56,22 @@ func (l *prefixLogger) Printf(format string, v ...interface{}) {
|
|||||||
l.logger.Printf("%v"+format, v...)
|
l.logger.Printf("%v"+format, v...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type int64Gauge struct {
|
||||||
|
v int64 // atomic
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *int64Gauge) Add(delta int64) {
|
||||||
|
atomic.AddInt64(&g.v, delta)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *int64Gauge) Value() int64 {
|
||||||
|
return atomic.LoadInt64(&g.v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (g *int64Gauge) Float64() float64 {
|
||||||
|
return float64(g.Value())
|
||||||
|
}
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Hostname string
|
Hostname string
|
||||||
Title string
|
Title string
|
||||||
@ -77,11 +93,14 @@ type Server struct {
|
|||||||
config atomic.Value // *Config
|
config atomic.Value // *Config
|
||||||
db Database
|
db Database
|
||||||
stopWG sync.WaitGroup
|
stopWG sync.WaitGroup
|
||||||
connCount int64 // atomic
|
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
listeners map[net.Listener]struct{}
|
listeners map[net.Listener]struct{}
|
||||||
users map[string]*user
|
users map[string]*user
|
||||||
|
|
||||||
|
metrics struct {
|
||||||
|
downstreams int64Gauge
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewServer(db Database) *Server {
|
func NewServer(db Database) *Server {
|
||||||
@ -144,9 +163,7 @@ func (s *Server) registerMetrics() {
|
|||||||
factory.NewGaugeFunc(prometheus.GaugeOpts{
|
factory.NewGaugeFunc(prometheus.GaugeOpts{
|
||||||
Name: "soju_downstreams_active",
|
Name: "soju_downstreams_active",
|
||||||
Help: "Current number of downstream connections",
|
Help: "Current number of downstream connections",
|
||||||
}, func() float64 {
|
}, s.metrics.downstreams.Float64)
|
||||||
return float64(atomic.LoadInt64(&s.connCount))
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Shutdown() {
|
func (s *Server) Shutdown() {
|
||||||
@ -234,7 +251,7 @@ func (s *Server) handle(ic ircConn) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
atomic.AddInt64(&s.connCount, 1)
|
s.metrics.downstreams.Add(1)
|
||||||
id := atomic.AddUint64(&lastDownstreamID, 1)
|
id := atomic.AddUint64(&lastDownstreamID, 1)
|
||||||
dc := newDownstreamConn(s, ic, id)
|
dc := newDownstreamConn(s, ic, id)
|
||||||
if err := dc.runUntilRegistered(); err != nil {
|
if err := dc.runUntilRegistered(); err != nil {
|
||||||
@ -249,7 +266,7 @@ func (s *Server) handle(ic ircConn) {
|
|||||||
dc.user.events <- eventDownstreamDisconnected{dc}
|
dc.user.events <- eventDownstreamDisconnected{dc}
|
||||||
}
|
}
|
||||||
dc.Close()
|
dc.Close()
|
||||||
atomic.AddInt64(&s.connCount, -1)
|
s.metrics.downstreams.Add(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Serve(ln net.Listener) error {
|
func (s *Server) Serve(ln net.Listener) error {
|
||||||
@ -333,6 +350,6 @@ func (s *Server) Stats() *ServerStats {
|
|||||||
s.lock.Lock()
|
s.lock.Lock()
|
||||||
stats.Users = len(s.users)
|
stats.Users = len(s.users)
|
||||||
s.lock.Unlock()
|
s.lock.Unlock()
|
||||||
stats.Downstreams = atomic.LoadInt64(&s.connCount)
|
stats.Downstreams = s.metrics.downstreams.Value()
|
||||||
return &stats
|
return &stats
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user