diff --git a/pubsub/publisher.go b/pubsub/publisher.go index 8529ffa..22be5b7 100644 --- a/pubsub/publisher.go +++ b/pubsub/publisher.go @@ -54,18 +54,24 @@ func (p *Publisher) SubscribeTopic(topic topicFunc) chan interface{} { // Evict removes the specified subscriber from receiving any more messages. func (p *Publisher) Evict(sub chan interface{}) { p.m.Lock() - delete(p.subscribers, sub) - close(sub) + if _, ok := p.subscribers[sub]; ok { + delete(p.subscribers, sub) + close(sub) + } p.m.Unlock() } // Publish sends the data in v to all subscribers currently registered with the publisher. func (p *Publisher) Publish(v interface{}) { p.m.RLock() + if len(p.subscribers) == 0 { + p.m.RUnlock() + return + } + wg := new(sync.WaitGroup) for sub, topic := range p.subscribers { wg.Add(1) - go p.sendTopic(sub, topic, v, wg) } wg.Wait()