Ensure consistent error handling and improve resource cleanup across widgets and APIs.
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user