Ver Fonte

optimize pubsub.Publish function

Signed-off-by: Shijiang Wei <mountkin@gmail.com>
Shijiang Wei há 9 anos atrás
pai
commit
1e0f1ec525
1 ficheiros alterados com 5 adições e 1 exclusões
  1. 5 1
      pkg/pubsub/publisher.go

+ 5 - 1
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()