From 17f5e10547da7bd2be4ecde1af1a0b2f50e3b44f Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Sun, 11 Aug 2024 19:01:39 +0200 Subject: [PATCH] ClientConnection no longer has public fields. --- cmd/converge/prometheus.go | 22 ++++++++++++++++++---- pkg/server/admin/admin.go | 16 ++++++++++------ pkg/server/converge/matchmaker.go | 2 +- 3 files changed, 29 insertions(+), 11 deletions(-) diff --git a/cmd/converge/prometheus.go b/cmd/converge/prometheus.go index 98d78e6..99db458 100644 --- a/cmd/converge/prometheus.go +++ b/cmd/converge/prometheus.go @@ -257,8 +257,15 @@ func removeAgentMetrics(agent *models.Agent) { ok1 := agentInfo.Delete(agentLabels(*agent)) guidLabels := prometheus.Labels{"agent_guid": agent.Guid} ok2 := agentStartTime.Delete(guidLabels) - ok3 := agentDuration.Delete(guidLabels) - if !ok1 || !ok2 || !ok3 { + // delayed deletion of the duration sow we are sure the prometheus has the last data. + go func() { + time.Sleep(60 * time.Second) + ok := agentDuration.Delete(guidLabels) + if !ok { + log.Printf("Could not delete duration timeseries for agent %s", agent.Guid) + } + }() + if !ok1 || !ok2 { log.Printf("Could not delete all timeseries for agent %s", agent.Guid) } } @@ -267,8 +274,15 @@ func removeClientMetrics(client *models.Client) { ok1 := clientInfo.Delete(clientLabels(*client)) guidLabels := prometheus.Labels{"client_guid": client.Guid} ok2 := clientStartTime.Delete(guidLabels) - ok3 := clientDuration.Delete(guidLabels) - if !ok1 || !ok2 || !ok3 { + // delayed deletion of the duration sow we are sure the prometheus has the last data. + go func() { + time.Sleep(60 * time.Second) + ok := clientDuration.Delete(guidLabels) + if !ok { + log.Printf("Could not delete duration timeseries for client %s", client.Guid) + } + }() + if !ok1 || !ok2 { log.Printf("Could not delete all timeseries for client %s", client.Guid) } } diff --git a/pkg/server/admin/admin.go b/pkg/server/admin/admin.go index 7e0f621..f65e6a4 100644 --- a/pkg/server/admin/admin.go +++ b/pkg/server/admin/admin.go @@ -26,8 +26,8 @@ var clientIdGenerator = concurrency.NewAtomicCounter() type ClientConnection struct { models.Client - AgentConnection net.Conn - ClientConnection iowrappers2.ReadWriteAddrCloser + agentConnection net.Conn + clientConnection iowrappers2.ReadWriteAddrCloser } func newAgent(commChannel comms.CommChannel, publicId string, agentInfo comms.EnvironmentInfo) *agentConnection { @@ -53,11 +53,15 @@ func newClient(publicId string, clientConn iowrappers2.ReadWriteAddrCloser, ClientId: strconv.Itoa(clientIdGenerator.IncrementAndGet()), StartTime: time.Now(), }, - AgentConnection: agentConn, - ClientConnection: clientConn, + agentConnection: agentConn, + clientConnection: clientConn, } } +func (match *ClientConnection) Synchronize() { + iowrappers2.SynchronizeStreams("client -- agent", match.clientConnection, match.agentConnection) +} + type Admin struct { // map of public id to agent mutex sync.Mutex @@ -213,8 +217,8 @@ func (admin *Admin) RemoveClient(client *ClientConnection) error { log.Printf("Removing client: '%s' created at %s\n", client.ClientId, client.StartTime.Format(time.DateTime)) // try to explicitly close connection to the agent. - _ = client.AgentConnection.Close() - _ = client.ClientConnection.Close() + _ = client.agentConnection.Close() + _ = client.clientConnection.Close() for i, _client := range admin.clients { if _client.ClientId == client.ClientId { diff --git a/pkg/server/converge/matchmaker.go b/pkg/server/converge/matchmaker.go index 3cc22ae..a152948 100644 --- a/pkg/server/converge/matchmaker.go +++ b/pkg/server/converge/matchmaker.go @@ -117,7 +117,7 @@ func (converge *MatchMaker) Connect(wsProxyMode bool, publicId string, conn iowr client.EnvironmentInfo = clientEnvironment } converge.logStatus() - iowrappers2.SynchronizeStreams("client -- agent", client.ClientConnection, client.AgentConnection) + client.Synchronize() return nil }