From 2c3627f184230cd3e5edd8fac901bd0aa5b92d8a Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Tue, 14 Jul 2015 09:10:14 -0700 Subject: [PATCH 1/2] Add docstring to pubsub.Publisher Signed-off-by: Alexander Morozov Upstream-commit: 7080f5d1cf2c9f86d89a84424394b2c61e925843 Component: engine --- components/engine/pkg/pubsub/publisher.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/components/engine/pkg/pubsub/publisher.go b/components/engine/pkg/pubsub/publisher.go index 534b74ad4d..6f3d5924db 100644 --- a/components/engine/pkg/pubsub/publisher.go +++ b/components/engine/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 From d0e6da97377894d82144b5da5aa8817448cf8f00 Mon Sep 17 00:00:00 2001 From: Alexander Morozov Date: Tue, 14 Jul 2015 09:14:51 -0700 Subject: [PATCH 2/2] Don't use time.After if there is no timeout Signed-off-by: Alexander Morozov Upstream-commit: bc6ad1608c37b56aa031760cc07e9a30ebe030d8 Component: engine --- components/engine/pkg/pubsub/publisher.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/components/engine/pkg/pubsub/publisher.go b/components/engine/pkg/pubsub/publisher.go index 6f3d5924db..ab457cfba9 100644 --- a/components/engine/pkg/pubsub/publisher.go +++ b/components/engine/pkg/pubsub/publisher.go @@ -58,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()