add apiContainerList variable to reduce memory allocations

This commit is contained in:
Henry Dollman
2024-09-27 16:10:31 -04:00
parent 56c0b86025
commit 4694642674

View File

@@ -30,6 +30,7 @@ type Agent struct {
addr string // Adress that the ssh server listens on addr string // Adress that the ssh server listens on
pubKey []byte // Public key for ssh server pubKey []byte // Public key for ssh server
sem chan struct{} // Semaphore to limit concurrent access to docker api sem chan struct{} // Semaphore to limit concurrent access to docker api
debug bool // true if LOG_LEVEL is set to debug
fsNames []string // List of filesystem device names being monitored fsNames []string // List of filesystem device names being monitored
fsStats map[string]*system.FsStats // Keeps track of disk stats for each filesystem fsStats map[string]*system.FsStats // Keeps track of disk stats for each filesystem
netInterfaces map[string]struct{} // Stores all valid network interfaces netInterfaces map[string]struct{} // Stores all valid network interfaces
@@ -37,8 +38,8 @@ type Agent struct {
prevContainerStatsMap map[string]*container.PrevContainerStats // Keeps track of container stats prevContainerStatsMap map[string]*container.PrevContainerStats // Keeps track of container stats
prevContainerStatsMutex *sync.Mutex // Mutex to prevent concurrent access to prevContainerStatsMap prevContainerStatsMutex *sync.Mutex // Mutex to prevent concurrent access to prevContainerStatsMap
dockerClient *http.Client // HTTP client to query docker api dockerClient *http.Client // HTTP client to query docker api
apiContainerList *[]container.ApiInfo // List of containers from docker host
sensorsContext context.Context // Sensors context to override sys location sensorsContext context.Context // Sensors context to override sys location
debug bool // true if LOG_LEVEL is set to debug
} }
func NewAgent(pubKey []byte, addr string) *Agent { func NewAgent(pubKey []byte, addr string) *Agent {
@@ -223,22 +224,21 @@ func (a *Agent) getDockerStats() ([]container.Stats, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
// docker host container list response if err := json.NewDecoder(resp.Body).Decode(&a.apiContainerList); err != nil {
var res []container.ApiInfo
if err := json.NewDecoder(resp.Body).Decode(&res); err != nil {
slog.Error("Error decoding containers", "err", err) slog.Error("Error decoding containers", "err", err)
return nil, err return nil, err
} }
containerStats := make([]container.Stats, 0, len(res)) containersLength := len(*a.apiContainerList)
containerStats := make([]container.Stats, 0, containersLength)
containerStatsMutex := sync.Mutex{} containerStatsMutex := sync.Mutex{}
// store valid ids to clean up old container ids from map // store valid ids to clean up old container ids from map
validIds := make(map[string]struct{}, len(res)) validIds := make(map[string]struct{}, containersLength)
var wg sync.WaitGroup var wg sync.WaitGroup
for _, ctr := range res { for _, ctr := range *a.apiContainerList {
ctr.IdShort = ctr.Id[:12] ctr.IdShort = ctr.Id[:12]
validIds[ctr.IdShort] = struct{}{} validIds[ctr.IdShort] = struct{}{}
// check if container is less than 1 minute old (possible restart) // check if container is less than 1 minute old (possible restart)