From 1e0f1ec52543bc83099fb91cd34dca4d38100e6f Mon Sep 17 00:00:00 2001 From: Shijiang Wei Date: Fri, 29 Jan 2016 13:08:20 +0800 Subject: [PATCH] optimize pubsub.Publish function Signed-off-by: Shijiang Wei --- pkg/pubsub/publisher.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/pubsub/publisher.go b/pkg/pubsub/publisher.go index d48d4323482c2..22be5b757b7a8 100644 --- a/pkg/pubsub/publisher.go +++ b/pkg/pubsub/publisher.go @@ -64,10 +64,14 @@ func (p *Publisher) Evict(sub chan interface{}) { // 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()