Ensure consistent error handling and improve resource cleanup across widgets and APIs.

This commit is contained in:
2026-01-01 12:10:33 +01:00
parent af5a39ef75
commit 2a66278cae
9 changed files with 90 additions and 48 deletions

View File

@@ -3,6 +3,7 @@ package docker
import (
"ArinDash/util"
"context"
"io"
"strings"
containertypes "github.com/docker/docker/api/types/container"
@@ -34,7 +35,12 @@ func FetchDockerMetrics(ctx context.Context) ([]ContainerMetrics, error) {
if err != nil {
return nil, err
}
defer cli.Close()
defer func(cli *client.Client) {
err := cli.Close()
if err != nil {
return
}
}(cli)
_, err = cli.Ping(ctx)
if err != nil {
@@ -60,7 +66,12 @@ func FetchDockerMetrics(ctx context.Context) ([]ContainerMetrics, error) {
continue
}
func() {
defer stats.Body.Close()
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(stats.Body)
var sj containertypes.StatsResponse
if err := util.DecodeJSON(stats.Body, &sj); err != nil {
return
@@ -109,9 +120,7 @@ func cpuPercentFromStats(s containertypes.StatsResponse) float64 {
func memoryFromStats(s containertypes.StatsResponse) (usage, limit uint64, percent float64) {
usage = s.MemoryStats.Usage
limit = s.MemoryStats.Limit
// Optionally discount cached memory if stats present.
if stats := s.MemoryStats.Stats; stats != nil {
// The common Linux approach: usage - cache
if cache, ok := stats["cache"]; ok && cache <= usage {
usage -= cache
}