From 4720caed0437865a1fdf1a7dd54f1f228ab00ac7 Mon Sep 17 00:00:00 2001 From: Romain Date: Tue, 23 Jul 2024 11:52:04 +0200 Subject: [PATCH] Update open connections gauge with connections count --- pkg/server/server_entrypoint_tcp.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/pkg/server/server_entrypoint_tcp.go b/pkg/server/server_entrypoint_tcp.go index 248c3eb21..ca901c90f 100644 --- a/pkg/server/server_entrypoint_tcp.go +++ b/pkg/server/server_entrypoint_tcp.go @@ -514,24 +514,31 @@ type connectionTracker struct { // AddConnection add a connection in the tracked connections list. func (c *connectionTracker) AddConnection(conn net.Conn) { + defer c.syncOpenConnectionGauge() + c.connsMu.Lock() c.conns[conn] = struct{}{} c.connsMu.Unlock() - - if c.openConnectionsGauge != nil { - c.openConnectionsGauge.Add(1) - } } // RemoveConnection remove a connection from the tracked connections list. func (c *connectionTracker) RemoveConnection(conn net.Conn) { + defer c.syncOpenConnectionGauge() + c.connsMu.Lock() delete(c.conns, conn) c.connsMu.Unlock() +} - if c.openConnectionsGauge != nil { - c.openConnectionsGauge.Add(-1) +// syncOpenConnectionGauge updates openConnectionsGauge value with the conns map length. +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 {