Skip to content

Commit

Permalink
Remove publisher if no one is listening
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
  • Loading branch information
crosbymichael committed Jan 21, 2015
1 parent 76141a0 commit 217a2bd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
3 changes: 3 additions & 0 deletions daemon/stats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
4 changes: 2 additions & 2 deletions integration-cli/docker_api_containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
8 changes: 8 additions & 0 deletions pkg/pubsub/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 217a2bd

Please sign in to comment.