2019-06-08 18:54:41 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2019 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package pubsub
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2019-06-27 01:41:12 -04:00
|
|
|
// Sub - subscriber entity.
|
|
|
|
type Sub struct {
|
|
|
|
ch chan interface{}
|
|
|
|
filter func(entry interface{}) bool
|
|
|
|
}
|
|
|
|
|
2019-06-08 18:54:41 -04:00
|
|
|
// PubSub holds publishers and subscribers
|
|
|
|
type PubSub struct {
|
2019-06-27 01:41:12 -04:00
|
|
|
subs []*Sub
|
|
|
|
sync.RWMutex
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2019-06-27 01:41:12 -04:00
|
|
|
// Publish message to the subscribers.
|
|
|
|
// Note that publish is always nob-blocking send so that we don't block on slow receivers.
|
|
|
|
// Hence receivers should use buffered channel so as not to miss the published events.
|
|
|
|
func (ps *PubSub) Publish(item interface{}) {
|
|
|
|
ps.RLock()
|
|
|
|
defer ps.RUnlock()
|
|
|
|
|
|
|
|
for _, sub := range ps.subs {
|
2019-09-03 14:10:48 -04:00
|
|
|
if sub.filter == nil || sub.filter(item) {
|
2019-06-27 01:41:12 -04:00
|
|
|
select {
|
|
|
|
case sub.ch <- item:
|
|
|
|
default:
|
|
|
|
}
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe - Adds a subscriber to pubsub system
|
2020-04-16 13:56:18 -04:00
|
|
|
func (ps *PubSub) Subscribe(subCh chan interface{}, doneCh <-chan struct{}, filter func(entry interface{}) bool) {
|
2019-06-27 01:41:12 -04:00
|
|
|
ps.Lock()
|
|
|
|
defer ps.Unlock()
|
|
|
|
|
|
|
|
sub := &Sub{subCh, filter}
|
|
|
|
ps.subs = append(ps.subs, sub)
|
2019-06-08 18:54:41 -04:00
|
|
|
|
2019-06-27 01:41:12 -04:00
|
|
|
go func() {
|
|
|
|
<-doneCh
|
2019-06-08 18:54:41 -04:00
|
|
|
|
2019-06-27 01:41:12 -04:00
|
|
|
ps.Lock()
|
|
|
|
defer ps.Unlock()
|
|
|
|
|
|
|
|
for i, s := range ps.subs {
|
|
|
|
if s == sub {
|
|
|
|
ps.subs = append(ps.subs[:i], ps.subs[i+1:]...)
|
|
|
|
}
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
2019-06-27 01:41:12 -04:00
|
|
|
}()
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasSubscribers returns true if pubsub system has subscribers
|
|
|
|
func (ps *PubSub) HasSubscribers() bool {
|
2020-07-22 16:16:55 -04:00
|
|
|
return ps.NumSubscribers() > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumSubscribers returns the number of current subscribers
|
|
|
|
func (ps *PubSub) NumSubscribers() int {
|
2019-06-27 01:41:12 -04:00
|
|
|
ps.RLock()
|
|
|
|
defer ps.RUnlock()
|
2020-07-22 16:16:55 -04:00
|
|
|
return len(ps.subs)
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// New inits a PubSub system
|
|
|
|
func New() *PubSub {
|
2019-06-27 01:41:12 -04:00
|
|
|
return &PubSub{}
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|