2020-06-02 22:37:10 +00:00
|
|
|
package stream
|
|
|
|
|
|
|
|
import (
|
2020-06-19 20:34:50 +00:00
|
|
|
"context"
|
2020-06-02 22:37:10 +00:00
|
|
|
"errors"
|
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-06-19 20:34:50 +00:00
|
|
|
// subscriptionStateOpen is the default state of a subscription. An open
|
|
|
|
// subscription may receive new events.
|
|
|
|
subscriptionStateOpen uint32 = 0
|
|
|
|
|
|
|
|
// subscriptionStateClosed indicates that the subscription was closed, possibly
|
|
|
|
// as a result of a change to an ACL token, and will not receive new events.
|
|
|
|
// The subscriber must issue a new Subscribe request.
|
|
|
|
subscriptionStateClosed uint32 = 1
|
2020-06-02 22:37:10 +00:00
|
|
|
)
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
// ErrSubscriptionClosed is a error signalling the subscription has been
|
|
|
|
// closed. The client should Unsubscribe, then re-Subscribe.
|
2020-07-08 04:31:22 +00:00
|
|
|
var ErrSubscriptionClosed = errors.New("subscription closed by server, client should resubscribe")
|
2020-06-02 22:37:10 +00:00
|
|
|
|
2020-07-08 04:31:22 +00:00
|
|
|
// Subscription provides events on a Topic. Events may be filtered by Key.
|
|
|
|
// Events are returned by Next(), and may start with a Snapshot of events.
|
2020-06-02 22:37:10 +00:00
|
|
|
type Subscription struct {
|
|
|
|
// state is accessed atomically 0 means open, 1 means closed with reload
|
|
|
|
state uint32
|
|
|
|
|
|
|
|
// req is the requests that we are responding to
|
2020-10-01 17:51:55 +00:00
|
|
|
req SubscribeRequest
|
2020-06-02 22:37:10 +00:00
|
|
|
|
|
|
|
// currentItem stores the current snapshot or topic buffer item we are on. It
|
|
|
|
// is mutated by calls to Next.
|
2020-07-06 21:29:45 +00:00
|
|
|
currentItem *bufferItem
|
2020-06-02 22:37:10 +00:00
|
|
|
|
2020-07-08 04:31:22 +00:00
|
|
|
// forceClosed is closed when forceClose is called. It is used by
|
|
|
|
// EventPublisher to cancel Next().
|
|
|
|
forceClosed chan struct{}
|
2020-06-02 22:37:10 +00:00
|
|
|
|
2020-07-08 04:31:22 +00:00
|
|
|
// unsub is a function set by EventPublisher that is called to free resources
|
|
|
|
// when the subscription is no longer needed.
|
|
|
|
// It must be safe to call the function from multiple goroutines and the function
|
|
|
|
// must be idempotent.
|
|
|
|
unsub func()
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:50 +00:00
|
|
|
// SubscribeRequest identifies the types of events the subscriber would like to
|
|
|
|
// receiver. Topic and Token are required.
|
2020-06-05 23:36:31 +00:00
|
|
|
type SubscribeRequest struct {
|
|
|
|
Topic Topic
|
|
|
|
Key string
|
|
|
|
Token string
|
|
|
|
Index uint64
|
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
// newSubscription return a new subscription. The caller is responsible for
|
2020-06-19 20:34:50 +00:00
|
|
|
// calling Unsubscribe when it is done with the subscription, to free resources.
|
2020-10-01 17:51:55 +00:00
|
|
|
func newSubscription(req SubscribeRequest, item *bufferItem, unsub func()) *Subscription {
|
2020-06-02 22:37:10 +00:00
|
|
|
return &Subscription{
|
2020-07-08 04:31:22 +00:00
|
|
|
forceClosed: make(chan struct{}),
|
2020-06-02 22:37:10 +00:00
|
|
|
req: req,
|
|
|
|
currentItem: item,
|
2020-07-08 04:31:22 +00:00
|
|
|
unsub: unsub,
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next returns the next set of events to deliver. It must only be called from a
|
|
|
|
// single goroutine concurrently as it mutates the Subscription.
|
2020-07-08 04:31:22 +00:00
|
|
|
func (s *Subscription) Next(ctx context.Context) ([]Event, error) {
|
2020-06-19 20:34:50 +00:00
|
|
|
if atomic.LoadUint32(&s.state) == subscriptionStateClosed {
|
2020-07-06 21:29:45 +00:00
|
|
|
return nil, ErrSubscriptionClosed
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2020-07-08 04:31:22 +00:00
|
|
|
next, err := s.currentItem.Next(ctx, s.forceClosed)
|
|
|
|
switch {
|
|
|
|
case err != nil && atomic.LoadUint32(&s.state) == subscriptionStateClosed:
|
|
|
|
return nil, ErrSubscriptionClosed
|
|
|
|
case err != nil:
|
2020-06-02 22:37:10 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.currentItem = next
|
|
|
|
|
2020-07-15 21:03:39 +00:00
|
|
|
events := filter(s.req.Key, next.Events)
|
2020-07-08 04:31:22 +00:00
|
|
|
if len(events) == 0 {
|
|
|
|
continue
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
2020-07-08 04:31:22 +00:00
|
|
|
return events, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 21:03:39 +00:00
|
|
|
// filter events to only those that match the key exactly.
|
|
|
|
func filter(key string, events []Event) []Event {
|
|
|
|
if key == "" || len(events) == 0 {
|
2020-07-08 04:31:22 +00:00
|
|
|
return events
|
|
|
|
}
|
2020-06-02 22:37:10 +00:00
|
|
|
|
2020-07-15 21:03:39 +00:00
|
|
|
var count int
|
2020-07-08 04:31:22 +00:00
|
|
|
for _, e := range events {
|
2020-07-15 21:03:39 +00:00
|
|
|
if key == e.Key {
|
|
|
|
count++
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
2020-07-08 04:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only allocate a new slice if some events need to be filtered out
|
2020-07-15 21:03:39 +00:00
|
|
|
switch count {
|
|
|
|
case 0:
|
|
|
|
return nil
|
|
|
|
case len(events):
|
2020-07-08 04:31:22 +00:00
|
|
|
return events
|
|
|
|
}
|
2020-06-02 22:37:10 +00:00
|
|
|
|
2020-07-15 21:03:39 +00:00
|
|
|
result := make([]Event, 0, count)
|
2020-07-08 04:31:22 +00:00
|
|
|
for _, e := range events {
|
2020-07-15 21:03:39 +00:00
|
|
|
if key == e.Key {
|
|
|
|
result = append(result, e)
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-15 21:03:39 +00:00
|
|
|
return result
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:50 +00:00
|
|
|
// Close the subscription. Subscribers will receive an error when they call Next,
|
|
|
|
// and will need to perform a new Subscribe request.
|
2020-06-02 22:37:10 +00:00
|
|
|
// It is safe to call from any goroutine.
|
2020-07-08 04:31:22 +00:00
|
|
|
func (s *Subscription) forceClose() {
|
2020-06-19 20:34:50 +00:00
|
|
|
swapped := atomic.CompareAndSwapUint32(&s.state, subscriptionStateOpen, subscriptionStateClosed)
|
2020-06-02 22:37:10 +00:00
|
|
|
if swapped {
|
2020-07-08 04:31:22 +00:00
|
|
|
close(s.forceClosed)
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-08 04:31:22 +00:00
|
|
|
|
|
|
|
// Unsubscribe the subscription, freeing resources.
|
|
|
|
func (s *Subscription) Unsubscribe() {
|
|
|
|
s.unsub()
|
|
|
|
}
|