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 {
|
2022-07-05 17:45:49 -04:00
|
|
|
ch chan Maskable
|
|
|
|
types Mask
|
|
|
|
filter func(entry Maskable) bool
|
2019-06-27 01:41:12 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 18:54:41 -04:00
|
|
|
// PubSub holds publishers and subscribers
|
|
|
|
type PubSub struct {
|
2022-07-05 17:45:49 -04:00
|
|
|
// atomics, keep at top:
|
|
|
|
types uint64
|
2021-01-04 12:40:30 -05:00
|
|
|
numSubscribers int32
|
2022-06-05 17:29:12 -04:00
|
|
|
maxSubscribers int32
|
2022-07-05 17:45:49 -04:00
|
|
|
|
|
|
|
// not atomics:
|
|
|
|
subs []*Sub
|
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.
|
2022-07-05 17:45:49 -04:00
|
|
|
func (ps *PubSub) Publish(item Maskable) {
|
2019-06-27 01:41:12 -04:00
|
|
|
ps.RLock()
|
|
|
|
defer ps.RUnlock()
|
|
|
|
for _, sub := range ps.subs {
|
2022-07-05 17:45:49 -04:00
|
|
|
if sub.types.Contains(Mask(item.Mask())) && (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-07-05 17:45:49 -04:00
|
|
|
func (ps *PubSub) Subscribe(mask Mask, subCh chan Maskable, doneCh <-chan struct{}, filter func(entry Maskable) bool) error {
|
2022-06-05 17:29:12 -04:00
|
|
|
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()
|
|
|
|
|
2022-07-05 17:45:49 -04:00
|
|
|
sub := &Sub{ch: subCh, types: mask, filter: filter}
|
2019-06-27 01:41:12 -04:00
|
|
|
ps.subs = append(ps.subs, sub)
|
2019-06-08 18:54:41 -04:00
|
|
|
|
2022-07-05 17:45:49 -04:00
|
|
|
// We hold a lock, so we are safe to update
|
|
|
|
combined := Mask(atomic.LoadUint64(&ps.types))
|
|
|
|
combined.Merge(mask)
|
|
|
|
atomic.StoreUint64(&ps.types, uint64(combined))
|
|
|
|
|
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()
|
2022-07-05 17:45:49 -04:00
|
|
|
var remainTypes Mask
|
2019-06-27 01:41:12 -04:00
|
|
|
for i, s := range ps.subs {
|
|
|
|
if s == sub {
|
|
|
|
ps.subs = append(ps.subs[:i], ps.subs[i+1:]...)
|
2022-07-05 17:45:49 -04:00
|
|
|
} else {
|
|
|
|
remainTypes.Merge(s.types)
|
2019-06-27 01:41:12 -04:00
|
|
|
}
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
2022-07-05 17:45:49 -04:00
|
|
|
atomic.StoreUint64(&ps.types, uint64(remainTypes))
|
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
|
|
|
}
|
|
|
|
|
2022-07-05 17:45:49 -04:00
|
|
|
// NumSubscribers returns the number of current subscribers,
|
|
|
|
// If t is non-nil, the type is checked against the active subscribed types,
|
|
|
|
// and 0 will be returned if nobody is subscribed for the type,
|
|
|
|
// otherwise the *total* number of subscribers is returned.
|
|
|
|
func (ps *PubSub) NumSubscribers(m Maskable) int32 {
|
|
|
|
if m != nil {
|
|
|
|
types := Mask(atomic.LoadUint64(&ps.types))
|
|
|
|
if !types.Overlaps(Mask(m.Mask())) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
2021-01-04 12:40:30 -05:00
|
|
|
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
|
|
|
}
|