diff --git a/cli/command/container/formatter_stats.go b/cli/command/container/formatter_stats.go index 1b5055ea17..c079c913ff 100644 --- a/cli/command/container/formatter_stats.go +++ b/cli/command/container/formatter_stats.go @@ -43,7 +43,7 @@ type StatsEntry struct { // Stats represents an entity to store containers statistics synchronously type Stats struct { - mutex sync.Mutex + mutex sync.RWMutex StatsEntry err error } @@ -51,8 +51,8 @@ type Stats struct { // GetError returns the container statistics error. // This is used to determine whether the statistics are valid or not func (cs *Stats) GetError() error { - cs.mutex.Lock() - defer cs.mutex.Unlock() + cs.mutex.RLock() + defer cs.mutex.RUnlock() return cs.err } @@ -94,8 +94,8 @@ func (cs *Stats) SetStatistics(s StatsEntry) { // GetStatistics returns container statistics with other meta data such as the container name func (cs *Stats) GetStatistics() StatsEntry { - cs.mutex.Lock() - defer cs.mutex.Unlock() + cs.mutex.RLock() + defer cs.mutex.RUnlock() return cs.StatsEntry } diff --git a/cli/command/container/stats.go b/cli/command/container/stats.go index 38d89450c4..0e938f3fad 100644 --- a/cli/command/container/stats.go +++ b/cli/command/container/stats.go @@ -184,13 +184,13 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error { waitFirst.Wait() var errs []string - cStats.mu.Lock() + cStats.mu.RLock() for _, c := range cStats.cs { if err := c.GetError(); err != nil { errs = append(errs, err.Error()) } } - cStats.mu.Unlock() + cStats.mu.RUnlock() if len(errs) > 0 { return errors.New(strings.Join(errs, "\n")) } @@ -221,11 +221,11 @@ func runStats(dockerCli command.Cli, opts *statsOptions) error { for range ticker.C { cleanScreen() ccstats := []StatsEntry{} - cStats.mu.Lock() + cStats.mu.RLock() for _, c := range cStats.cs { ccstats = append(ccstats, c.GetStatistics()) } - cStats.mu.Unlock() + cStats.mu.RUnlock() if err = statsFormatWrite(statsCtx, ccstats, daemonOSType, !opts.noTrunc); err != nil { break } diff --git a/cli/command/container/stats_helpers.go b/cli/command/container/stats_helpers.go index 299bd26baf..5ea7f665dc 100644 --- a/cli/command/container/stats_helpers.go +++ b/cli/command/container/stats_helpers.go @@ -14,7 +14,7 @@ import ( ) type stats struct { - mu sync.Mutex + mu sync.RWMutex cs []*Stats }