2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2019-06-08 18:54:41 -04:00
|
|
|
|
|
|
|
package pubsub
|
|
|
|
|
|
|
|
import (
|
2022-06-05 17:29:12 -04:00
|
|
|
"fmt"
|
2019-06-08 18:54:41 -04:00
|
|
|
"sync"
|
2021-01-04 12:40:30 -05:00
|
|
|
"sync/atomic"
|
2019-06-08 18:54:41 -04:00
|
|
|
)
|
|
|
|
|
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 {
|
2021-01-04 12:40:30 -05:00
|
|
|
subs []*Sub
|
|
|
|
numSubscribers int32
|
2022-06-05 17:29:12 -04:00
|
|
|
maxSubscribers int32
|
2019-06-27 01:41:12 -04:00
|
|
|
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
|
2022-06-05 17:29:12 -04:00
|
|
|
func (ps *PubSub) Subscribe(subCh chan interface{}, doneCh <-chan struct{}, filter func(entry interface{}) bool) error {
|
|
|
|
totalSubs := atomic.AddInt32(&ps.numSubscribers, 1)
|
|
|
|
if ps.maxSubscribers > 0 && totalSubs > ps.maxSubscribers {
|
|
|
|
atomic.AddInt32(&ps.numSubscribers, -1)
|
|
|
|
return fmt.Errorf("the limit of `%d` subscribers is reached", ps.maxSubscribers)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-01-04 12:40:30 -05:00
|
|
|
atomic.AddInt32(&ps.numSubscribers, -1)
|
2019-06-27 01:41:12 -04:00
|
|
|
}()
|
2022-06-05 17:29:12 -04:00
|
|
|
|
|
|
|
return nil
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2020-07-22 16:16:55 -04:00
|
|
|
// NumSubscribers returns the number of current subscribers
|
2021-01-04 12:40:30 -05:00
|
|
|
func (ps *PubSub) NumSubscribers() int32 {
|
|
|
|
return atomic.LoadInt32(&ps.numSubscribers)
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2022-06-05 17:29:12 -04:00
|
|
|
// New inits a PubSub system with a limit of maximum
|
|
|
|
// subscribers unless zero is specified
|
|
|
|
func New(maxSubscribers int32) *PubSub {
|
|
|
|
return &PubSub{maxSubscribers: maxSubscribers}
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|