Changeset 709 in code for trunk/server.go


Ignore:
Timestamp:
Nov 17, 2021, 2:58:19 PM (4 years ago)
Author:
contact
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/server.go

    r708 r709  
    5757}
    5858
     59type int64Gauge struct {
     60        v int64 // atomic
     61}
     62
     63func (g *int64Gauge) Add(delta int64) {
     64        atomic.AddInt64(&g.v, delta)
     65}
     66
     67func (g *int64Gauge) Value() int64 {
     68        return atomic.LoadInt64(&g.v)
     69}
     70
     71func (g *int64Gauge) Float64() float64 {
     72        return float64(g.Value())
     73}
     74
    5975type Config struct {
    6076        Hostname        string
     
    7591        MetricsRegistry prometheus.Registerer // can be nil
    7692
    77         config    atomic.Value // *Config
    78         db        Database
    79         stopWG    sync.WaitGroup
    80         connCount int64 // atomic
     93        config atomic.Value // *Config
     94        db     Database
     95        stopWG sync.WaitGroup
    8196
    8297        lock      sync.Mutex
    8398        listeners map[net.Listener]struct{}
    8499        users     map[string]*user
     100
     101        metrics struct {
     102                downstreams int64Gauge
     103        }
    85104}
    86105
     
    145164                Name: "soju_downstreams_active",
    146165                Help: "Current number of downstream connections",
    147         }, func() float64 {
    148                 return float64(atomic.LoadInt64(&s.connCount))
    149         })
     166        }, s.metrics.downstreams.Float64)
    150167}
    151168
     
    235252        }()
    236253
    237         atomic.AddInt64(&s.connCount, 1)
     254        s.metrics.downstreams.Add(1)
    238255        id := atomic.AddUint64(&lastDownstreamID, 1)
    239256        dc := newDownstreamConn(s, ic, id)
     
    250267        }
    251268        dc.Close()
    252         atomic.AddInt64(&s.connCount, -1)
     269        s.metrics.downstreams.Add(-1)
    253270}
    254271
     
    334351        stats.Users = len(s.users)
    335352        s.lock.Unlock()
    336         stats.Downstreams = atomic.LoadInt64(&s.connCount)
     353        stats.Downstreams = s.metrics.downstreams.Value()
    337354        return &stats
    338355}
Note: See TracChangeset for help on using the changeset viewer.