From 54e8beec9b33fcca4263516b6f03ef96c29132a3 Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Fri, 24 Jun 2016 01:49:45 +0900 Subject: [PATCH] Fix high cpu usage caused by docker stats. Signed-off-by: Tianyi Wang --- api/client/container/stats.go | 10 ++++++++++ api/client/container/stats_helpers.go | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/api/client/container/stats.go b/api/client/container/stats.go index 46861514cd..b2a45a984c 100644 --- a/api/client/container/stats.go +++ b/api/client/container/stats.go @@ -192,13 +192,23 @@ func runStats(dockerCli *client.DockerCli, opts *statsOptions) error { for range time.Tick(500 * time.Millisecond) { printHeader() + toRemove := []string{} cStats.mu.Lock() for _, s := range cStats.cs { if err := s.Display(w); err != nil && !opts.noStream { logrus.Debugf("stats: got error for %s: %v", s.Name, err) + if err == io.EOF { + toRemove = append(toRemove, s.Name) + } } } cStats.mu.Unlock() + for _, name := range toRemove { + cStats.remove(name) + } + if len(cStats.cs) == 0 && !showAll { + return nil + } w.Flush() if opts.noStream { break diff --git a/api/client/container/stats_helpers.go b/api/client/container/stats_helpers.go index 6083669644..336ad14760 100644 --- a/api/client/container/stats_helpers.go +++ b/api/client/container/stats_helpers.go @@ -97,6 +97,10 @@ func (s *containerStats) Collect(ctx context.Context, cli client.APIClient, stre if err := dec.Decode(&v); err != nil { dec = json.NewDecoder(io.MultiReader(dec.Buffered(), responseBody)) u <- err + if err == io.EOF { + break + } + time.Sleep(100 * time.Millisecond) continue }