From 3ca5ab180f0fa5048728b2ca3560b82b49fc4d89 Mon Sep 17 00:00:00 2001 From: andres-portainer <91705312+andres-portainer@users.noreply.github.com> Date: Thu, 23 Jan 2025 20:41:09 -0300 Subject: [PATCH] fix(system): optimize the memory usage when counting nodes BE-11575 (#342) --- api/http/handler/system/nodes_count.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/api/http/handler/system/nodes_count.go b/api/http/handler/system/nodes_count.go index 23f78749a..0b9971619 100644 --- a/api/http/handler/system/nodes_count.go +++ b/api/http/handler/system/nodes_count.go @@ -3,6 +3,7 @@ package system import ( "net/http" + portainer "github.com/portainer/portainer/api" statusutil "github.com/portainer/portainer/api/internal/nodes" "github.com/portainer/portainer/api/internal/snapshot" httperror "github.com/portainer/portainer/pkg/libhttp/error" @@ -31,14 +32,15 @@ func (handler *Handler) systemNodesCount(w http.ResponseWriter, r *http.Request) return httperror.InternalServerError("Failed to get environment list", err) } - for i := range endpoints { - err = snapshot.FillSnapshotData(handler.dataStore, &endpoints[i]) - if err != nil { + var nodes int + + for _, endpoint := range endpoints { + if err := snapshot.FillSnapshotData(handler.dataStore, &endpoint); err != nil { return httperror.InternalServerError("Unable to add snapshot data", err) } - } - nodes := statusutil.NodesCount(endpoints) + nodes += statusutil.NodesCount([]portainer.Endpoint{endpoint}) + } return response.JSON(w, &nodesCountResponse{Nodes: nodes}) }