summaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorValentin Rothberg <rothberg@redhat.com>2020-09-23 13:32:58 +0200
committerValentin Rothberg <rothberg@redhat.com>2020-09-23 15:39:25 +0200
commit376ba349bfadc47a938084ccb3c2d112c92f09ca (patch)
treefa8acd364392a28b0f0cddc361b83806b4da4458 /cmd
parent5cedd830f7275e8dc3382502908b846bfa57a3b8 (diff)
downloadpodman-376ba349bfadc47a938084ccb3c2d112c92f09ca.tar.gz
podman-376ba349bfadc47a938084ccb3c2d112c92f09ca.tar.bz2
podman-376ba349bfadc47a938084ccb3c2d112c92f09ca.zip
stats refactor
Refactor the entities' stats API to simplify using it and reduce the risk of running into concurrency issues at the call sites. Further simplify the stats code by de-spaghetti-ing the logic and reducing duplicate code. `ContainerStats` now returns a data channel and an error. If the error is nil, callers can read from the channel. Signed-off-by: Valentin Rothberg <rothberg@redhat.com>
Diffstat (limited to 'cmd')
-rw-r--r--cmd/podman/containers/stats.go38
1 files changed, 18 insertions, 20 deletions
diff --git a/cmd/podman/containers/stats.go b/cmd/podman/containers/stats.go
index ddb5f32ef..1a4adb376 100644
--- a/cmd/podman/containers/stats.go
+++ b/cmd/podman/containers/stats.go
@@ -4,7 +4,6 @@ import (
"fmt"
"os"
"strings"
- "sync"
"text/tabwriter"
"text/template"
@@ -107,32 +106,31 @@ func stats(cmd *cobra.Command, args []string) error {
return errors.New("stats is not supported in rootless mode without cgroups v2")
}
}
- statsOptions.StatChan = make(chan []*define.ContainerStats, 1)
- wg := sync.WaitGroup{}
- wg.Add(1)
- go func() {
- for reports := range statsOptions.StatChan {
- if err := outputStats(reports); err != nil {
- logrus.Error(err)
- }
- }
- wg.Done()
- }()
- err := registry.ContainerEngine().ContainerStats(registry.Context(), args, statsOptions)
- wg.Wait()
- return err
+ statsChan, err := registry.ContainerEngine().ContainerStats(registry.Context(), args, statsOptions)
+ if err != nil {
+ return err
+ }
+ for report := range statsChan {
+ if report.Error != nil {
+ return report.Error
+ }
+ if err := outputStats(report.Stats); err != nil {
+ logrus.Error(err)
+ }
+ }
+ return nil
}
-func outputStats(reports []*define.ContainerStats) error {
+func outputStats(reports []define.ContainerStats) error {
if len(statsOptions.Format) < 1 && !statsOptions.NoReset {
tm.Clear()
tm.MoveCursor(1, 1)
tm.Flush()
}
- stats := make([]*containerStats, 0, len(reports))
+ stats := make([]containerStats, 0, len(reports))
for _, r := range reports {
- stats = append(stats, &containerStats{r})
+ stats = append(stats, containerStats{r})
}
if statsOptions.Format == "json" {
return outputJSON(stats)
@@ -163,7 +161,7 @@ func outputStats(reports []*define.ContainerStats) error {
}
type containerStats struct {
- *define.ContainerStats
+ define.ContainerStats
}
func (s *containerStats) ID() string {
@@ -213,7 +211,7 @@ func combineHumanValues(a, b uint64) string {
return fmt.Sprintf("%s / %s", units.HumanSize(float64(a)), units.HumanSize(float64(b)))
}
-func outputJSON(stats []*containerStats) error {
+func outputJSON(stats []containerStats) error {
type jstat struct {
Id string `json:"id"` //nolint
Name string `json:"name"`