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 (
|
2024-01-30 15:49:02 -05:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
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
|
|
|
)
|
|
|
|
|
2024-01-30 15:49:02 -05:00
|
|
|
// GetByteBuffer returns a byte buffer from the pool.
|
|
|
|
var GetByteBuffer = func() []byte {
|
|
|
|
return make([]byte, 0, 4096)
|
|
|
|
}
|
|
|
|
|
2019-06-27 01:41:12 -04:00
|
|
|
// Sub - subscriber entity.
|
2022-10-28 13:55:42 -04:00
|
|
|
type Sub[T Maskable] struct {
|
|
|
|
ch chan T
|
2022-07-05 17:45:49 -04:00
|
|
|
types Mask
|
2022-10-28 13:55:42 -04:00
|
|
|
filter func(entry T) bool
|
2019-06-27 01:41:12 -04:00
|
|
|
}
|
|
|
|
|
2019-06-08 18:54:41 -04:00
|
|
|
// PubSub holds publishers and subscribers
|
2022-10-28 13:55:42 -04:00
|
|
|
type PubSub[T Maskable, M Maskable] 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:
|
2022-10-28 13:55:42 -04:00
|
|
|
subs []*Sub[T]
|
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-10-28 13:55:42 -04:00
|
|
|
func (ps *PubSub[T, M]) Publish(item T) {
|
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-10-28 13:55:42 -04:00
|
|
|
func (ps *PubSub[T, M]) Subscribe(mask M, subCh chan T, doneCh <-chan struct{}, filter func(entry T) 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-10-28 13:55:42 -04:00
|
|
|
sub := &Sub[T]{ch: subCh, types: Mask(mask.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))
|
2022-10-28 13:55:42 -04:00
|
|
|
combined.Merge(Mask(mask.Mask()))
|
2022-07-05 17:45:49 -04:00
|
|
|
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 {
|
2024-01-30 15:49:02 -05:00
|
|
|
if s == sub {
|
|
|
|
ps.subs = append(ps.subs[:i], ps.subs[i+1:]...)
|
|
|
|
} else {
|
|
|
|
remainTypes.Merge(s.types)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
atomic.StoreUint64(&ps.types, uint64(remainTypes))
|
|
|
|
atomic.AddInt32(&ps.numSubscribers, -1)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubscribeJSON - Adds a subscriber to pubsub system and returns results with JSON encoding.
|
|
|
|
func (ps *PubSub[T, M]) SubscribeJSON(mask M, subCh chan<- []byte, doneCh <-chan struct{}, filter func(entry T) 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)
|
|
|
|
}
|
|
|
|
ps.Lock()
|
|
|
|
defer ps.Unlock()
|
|
|
|
subChT := make(chan T, 10000)
|
|
|
|
sub := &Sub[T]{ch: subChT, types: Mask(mask.Mask()), filter: filter}
|
|
|
|
ps.subs = append(ps.subs, sub)
|
|
|
|
|
|
|
|
// We hold a lock, so we are safe to update
|
|
|
|
combined := Mask(atomic.LoadUint64(&ps.types))
|
|
|
|
combined.Merge(Mask(mask.Mask()))
|
|
|
|
atomic.StoreUint64(&ps.types, uint64(combined))
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-doneCh:
|
|
|
|
case v, ok := <-subChT:
|
|
|
|
if !ok {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
buf.Reset()
|
|
|
|
err := enc.Encode(v)
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
subCh <- append(GetByteBuffer()[:0], buf.Bytes()...)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
ps.Lock()
|
|
|
|
defer ps.Unlock()
|
|
|
|
var remainTypes Mask
|
|
|
|
for i, s := range ps.subs {
|
2019-06-27 01:41:12 -04:00
|
|
|
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,
|
2022-10-28 13:55:42 -04:00
|
|
|
// The mask is checked against the active subscribed types,
|
|
|
|
// and 0 will be returned if nobody is subscribed for the type(s).
|
|
|
|
func (ps *PubSub[T, M]) NumSubscribers(mask M) int32 {
|
|
|
|
types := Mask(atomic.LoadUint64(&ps.types))
|
|
|
|
if !types.Overlaps(Mask(mask.Mask())) {
|
|
|
|
return 0
|
2022-07-05 17:45:49 -04:00
|
|
|
}
|
2021-01-04 12:40:30 -05:00
|
|
|
return atomic.LoadInt32(&ps.numSubscribers)
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|
|
|
|
|
2022-10-28 13:55:42 -04:00
|
|
|
// Subscribers returns the number of current subscribers for all types.
|
|
|
|
func (ps *PubSub[T, M]) Subscribers() int32 {
|
|
|
|
return atomic.LoadInt32(&ps.numSubscribers)
|
|
|
|
}
|
|
|
|
|
2022-06-05 17:29:12 -04:00
|
|
|
// New inits a PubSub system with a limit of maximum
|
|
|
|
// subscribers unless zero is specified
|
2022-10-28 13:55:42 -04:00
|
|
|
func New[T Maskable, M Maskable](maxSubscribers int32) *PubSub[T, M] {
|
|
|
|
return &PubSub[T, M]{maxSubscribers: maxSubscribers}
|
2019-06-08 18:54:41 -04:00
|
|
|
}
|