Update open connections gauge with connections count

This commit is contained in:
Romain 2024-07-23 11:52:04 +02:00 committed by GitHub
parent c5a6b49330
commit 4720caed04
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -514,24 +514,31 @@ type connectionTracker struct {
// AddConnection add a connection in the tracked connections list. // AddConnection add a connection in the tracked connections list.
func (c *connectionTracker) AddConnection(conn net.Conn) { func (c *connectionTracker) AddConnection(conn net.Conn) {
defer c.syncOpenConnectionGauge()
c.connsMu.Lock() c.connsMu.Lock()
c.conns[conn] = struct{}{} c.conns[conn] = struct{}{}
c.connsMu.Unlock() c.connsMu.Unlock()
if c.openConnectionsGauge != nil {
c.openConnectionsGauge.Add(1)
}
} }
// RemoveConnection remove a connection from the tracked connections list. // RemoveConnection remove a connection from the tracked connections list.
func (c *connectionTracker) RemoveConnection(conn net.Conn) { func (c *connectionTracker) RemoveConnection(conn net.Conn) {
defer c.syncOpenConnectionGauge()
c.connsMu.Lock() c.connsMu.Lock()
delete(c.conns, conn) delete(c.conns, conn)
c.connsMu.Unlock() c.connsMu.Unlock()
}
if c.openConnectionsGauge != nil { // syncOpenConnectionGauge updates openConnectionsGauge value with the conns map length.
c.openConnectionsGauge.Add(-1) func (c *connectionTracker) syncOpenConnectionGauge() {
if c.openConnectionsGauge == nil {
return
} }
c.connsMu.RLock()
c.openConnectionsGauge.Set(float64(len(c.conns)))
c.connsMu.RUnlock()
} }
func (c *connectionTracker) isEmpty() bool { func (c *connectionTracker) isEmpty() bool {