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"
|
2020-10-15 22:06:04 +00:00
|
|
|
"fmt"
|
2020-06-02 22:37:10 +00:00
|
|
|
"sync/atomic"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-10-15 22:06:04 +00:00
|
|
|
// subStateOpen is the default state of a subscription. An open subscription
|
|
|
|
// may return new events.
|
|
|
|
subStateOpen = 0
|
2020-06-19 20:34:50 +00:00
|
|
|
|
2020-10-15 22:06:04 +00:00
|
|
|
// subStateForceClosed indicates the subscription was forced closed by
|
|
|
|
// the EventPublisher, possibly as a result of a change to an ACL token, and
|
|
|
|
// will not return new events.
|
2020-06-19 20:34:50 +00:00
|
|
|
// The subscriber must issue a new Subscribe request.
|
2020-10-15 22:06:04 +00:00
|
|
|
subStateForceClosed = 1
|
|
|
|
|
|
|
|
// subStateUnsub indicates the subscription was closed by the caller, and
|
|
|
|
// will not return new events.
|
|
|
|
subStateUnsub = 2
|
2020-06-02 22:37:10 +00:00
|
|
|
)
|
|
|
|
|
2020-10-15 22:06:04 +00:00
|
|
|
// ErrSubForceClosed is a error signalling the subscription has been
|
2020-07-06 21:29:45 +00:00
|
|
|
// closed. The client should Unsubscribe, then re-Subscribe.
|
2020-10-15 22:06:04 +00:00
|
|
|
var ErrSubForceClosed = errors.New("subscription closed by server, client must reset state and 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 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-10-15 22:06:04 +00:00
|
|
|
// closed is a channel which is closed when the subscription is closed. It
|
|
|
|
// is used to exit the blocking select.
|
|
|
|
closed 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 {
|
2020-10-27 18:40:06 +00:00
|
|
|
// Topic to subscribe to
|
2020-06-05 23:36:31 +00:00
|
|
|
Topic Topic
|
2020-10-27 18:40:06 +00:00
|
|
|
// Key used to filter events in the topic. Only events matching the key will
|
|
|
|
// be returned by the subscription. A blank key will return all events. Key
|
|
|
|
// is generally the name of the resource.
|
|
|
|
Key string
|
|
|
|
// Namespace used to filter events in the topic. Only events matching the
|
|
|
|
// namespace will be returned by the subscription.
|
|
|
|
Namespace string
|
2021-08-17 21:49:26 +00:00
|
|
|
// Partition used to filter events in the topic. Only events matching the
|
|
|
|
// partition will be returned by the subscription.
|
|
|
|
Partition string // TODO(partitions): make this work
|
2020-10-27 18:40:06 +00:00
|
|
|
// Token that was used to authenticate the request. If any ACL policy
|
|
|
|
// changes impact the token the subscription will be forcefully closed.
|
2020-06-05 23:36:31 +00:00
|
|
|
Token string
|
2020-10-27 18:40:06 +00:00
|
|
|
// Index is the last index the client received. If non-zero the
|
|
|
|
// subscription will be resumed from this index. If the index is out-of-date
|
|
|
|
// a NewSnapshotToFollow event will be sent.
|
2020-06-05 23:36:31 +00:00
|
|
|
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-10-15 22:06:04 +00:00
|
|
|
closed: 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
// Next returns the next Event to deliver. It must only be called from a
|
2020-06-02 22:37:10 +00:00
|
|
|
// single goroutine concurrently as it mutates the Subscription.
|
2020-10-05 16:38:38 +00:00
|
|
|
func (s *Subscription) Next(ctx context.Context) (Event, error) {
|
2020-06-02 22:37:10 +00:00
|
|
|
for {
|
2020-10-15 22:06:04 +00:00
|
|
|
if err := s.requireStateOpen(); err != nil {
|
|
|
|
return Event{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
next, err := s.currentItem.Next(ctx, s.closed)
|
|
|
|
if err := s.requireStateOpen(); err != nil {
|
|
|
|
return Event{}, err
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-10-05 16:38:38 +00:00
|
|
|
return Event{}, err
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
|
|
|
s.currentItem = next
|
2020-10-05 16:38:38 +00:00
|
|
|
if len(next.Events) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2020-11-05 22:50:17 +00:00
|
|
|
event := newEventFromBatch(s.req, next.Events)
|
2021-08-17 21:49:26 +00:00
|
|
|
if !event.Payload.MatchesKey(s.req.Key, s.req.Namespace, s.req.Partition) {
|
2020-07-08 04:31:22 +00:00
|
|
|
continue
|
2020-06-02 22:37:10 +00:00
|
|
|
}
|
2020-10-05 16:38:38 +00:00
|
|
|
return event, nil
|
2020-07-08 04:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-15 22:06:04 +00:00
|
|
|
func (s *Subscription) requireStateOpen() error {
|
|
|
|
switch atomic.LoadUint32(&s.state) {
|
|
|
|
case subStateForceClosed:
|
|
|
|
return ErrSubForceClosed
|
|
|
|
case subStateUnsub:
|
|
|
|
return fmt.Errorf("subscription was closed by unsubscribe")
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-05 16:38:38 +00:00
|
|
|
func newEventFromBatch(req SubscribeRequest, events []Event) Event {
|
|
|
|
first := events[0]
|
|
|
|
if len(events) == 1 {
|
|
|
|
return first
|
2020-07-08 04:31:22 +00:00
|
|
|
}
|
2020-10-05 16:38:38 +00:00
|
|
|
return Event{
|
|
|
|
Topic: req.Topic,
|
|
|
|
Index: first.Index,
|
2020-11-06 18:00:33 +00:00
|
|
|
Payload: newPayloadEvents(events...),
|
2020-07-08 04:31:22 +00:00
|
|
|
}
|
2020-10-05 16:38:38 +00:00
|
|
|
}
|
2020-07-08 04:31:22 +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-10-15 22:06:04 +00:00
|
|
|
if atomic.CompareAndSwapUint32(&s.state, subStateOpen, subStateForceClosed) {
|
|
|
|
close(s.closed)
|
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() {
|
2020-10-15 22:06:04 +00:00
|
|
|
if atomic.CompareAndSwapUint32(&s.state, subStateOpen, subStateUnsub) {
|
|
|
|
close(s.closed)
|
|
|
|
}
|
2020-07-08 04:31:22 +00:00
|
|
|
s.unsub()
|
|
|
|
}
|