@ -18,6 +18,7 @@ package pubsub
import (
import (
"sync"
"sync"
"sync/atomic"
)
)
// Sub - subscriber entity.
// Sub - subscriber entity.
@ -29,6 +30,7 @@ type Sub struct {
// PubSub holds publishers and subscribers
// PubSub holds publishers and subscribers
type PubSub struct {
type PubSub struct {
subs [ ] * Sub
subs [ ] * Sub
numSubscribers int32
sync . RWMutex
sync . RWMutex
}
}
@ -56,6 +58,7 @@ func (ps *PubSub) Subscribe(subCh chan interface{}, doneCh <-chan struct{}, filt
sub := & Sub { subCh , filter }
sub := & Sub { subCh , filter }
ps . subs = append ( ps . subs , sub )
ps . subs = append ( ps . subs , sub )
atomic . AddInt32 ( & ps . numSubscribers , 1 )
go func ( ) {
go func ( ) {
<- doneCh
<- doneCh
@ -68,19 +71,13 @@ func (ps *PubSub) Subscribe(subCh chan interface{}, doneCh <-chan struct{}, filt
ps . subs = append ( ps . subs [ : i ] , ps . subs [ i + 1 : ] ... )
ps . subs = append ( ps . subs [ : i ] , ps . subs [ i + 1 : ] ... )
}
}
}
}
atomic . AddInt32 ( & ps . numSubscribers , - 1 )
} ( )
} ( )
}
}
// HasSubscribers returns true if pubsub system has subscribers
func ( ps * PubSub ) HasSubscribers ( ) bool {
return ps . NumSubscribers ( ) > 0
}
// NumSubscribers returns the number of current subscribers
// NumSubscribers returns the number of current subscribers
func ( ps * PubSub ) NumSubscribers ( ) int {
func ( ps * PubSub ) NumSubscribers ( ) int32 {
ps . RLock ( )
return atomic . LoadInt32 ( & ps . numSubscribers )
defer ps . RUnlock ( )
return len ( ps . subs )
}
}
// New inits a PubSub system
// New inits a PubSub system