Ver código fonte

Merge pull request #14629 from LK4D4/pubsub_timer_off

pkg/pubsub: Don't use time.After if there is no timeout
Brian Goff 10 anos atrás
pai
commit
e5fa2a740d
1 arquivos alterados com 10 adições e 1 exclusões
  1. 10 1
      pkg/pubsub/publisher.go

+ 10 - 1
pkg/pubsub/publisher.go

@@ -19,6 +19,8 @@ func NewPublisher(publishTimeout time.Duration, buffer int) *Publisher {
 
 type subscriber chan interface{}
 
+// Publisher is basic pub/sub structure. Allows to send events and subscribe
+// to them. Can be safely used from multiple goroutines.
 type Publisher struct {
 	m           sync.RWMutex
 	buffer      int
@@ -56,9 +58,16 @@ func (p *Publisher) Publish(v interface{}) {
 	p.m.RLock()
 	for sub := range p.subscribers {
 		// send under a select as to not block if the receiver is unavailable
+		if p.timeout > 0 {
+			select {
+			case sub <- v:
+			case <-time.After(p.timeout):
+			}
+			continue
+		}
 		select {
 		case sub <- v:
-		case <-time.After(p.timeout):
+		default:
 		}
 	}
 	p.m.RUnlock()