From a96214b8c65d5a0dd502a6ba0b50aed9a55cce68 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Tue, 20 Jan 2015 11:37:50 -0800 Subject: [PATCH] Remove publisher if no one is listening Signed-off-by: Michael Crosby --- pubsub/publisher.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pubsub/publisher.go b/pubsub/publisher.go index 98d0356..f017262 100644 --- a/pubsub/publisher.go +++ b/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)