diff --git a/daemon/stats_collector.go b/daemon/stats_collector.go index fe0a1f763b..50ae6baf54 100644 --- a/daemon/stats_collector.go +++ b/daemon/stats_collector.go @@ -68,6 +68,9 @@ func (s *statsCollector) unsubscribe(c *Container, ch chan interface{}) { publisher := s.publishers[c] if publisher != nil { publisher.Evict(ch) + if publisher.Len() == 0 { + delete(s.publishers, c) + } } s.m.Unlock() } diff --git a/integration-cli/docker_api_containers_test.go b/integration-cli/docker_api_containers_test.go index 43ae8edde5..34cc82aff0 100644 --- a/integration-cli/docker_api_containers_test.go +++ b/integration-cli/docker_api_containers_test.go @@ -274,10 +274,10 @@ func TestGetContainerStats(t *testing.T) { t.Fatalf("GET containers/stats sockRequest failed: %v", err) } + dec := json.NewDecoder(bytes.NewBuffer(body)) var s *stats.Stats - if err := json.Unmarshal(body, &s); err != nil { + if err := dec.Decode(&s); err != nil { t.Fatal(err) } - logDone("container REST API - check GET containers/stats") } diff --git a/pkg/pubsub/publisher.go b/pkg/pubsub/publisher.go index 98d035687d..f017262ae3 100644 --- a/pkg/pubsub/publisher.go +++ b/pkg/pubsub/publisher.go @@ -26,6 +26,14 @@ type Publisher struct { subscribers map[subscriber]struct{} } +// Len returns the number of subscribers for the publisher +func (p *Publisher) Len() int { + p.m.RLock() + i := len(p.subscribers) + p.m.RUnlock() + return i +} + // Subscribe adds a new subscriber to the publisher returning the channel. func (p *Publisher) Subscribe() chan interface{} { ch := make(chan interface{}, p.buffer)