2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
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
|
2022-05-23 12:59:13 +00:00
|
|
|
|
|
|
|
// subStateShutting down indicates the subscription was closed due to
|
|
|
|
// the server being shut down.
|
|
|
|
subStateShuttingDown = 3
|
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
|
|
|
|
2022-05-23 12:59:13 +00:00
|
|
|
// ErrShuttingDown is an error to signal that the subscription has
|
|
|
|
// been closed because the server is shutting down. The client should
|
|
|
|
// subscribe to a different server to get streaming event updates.
|
|
|
|
var ErrShuttingDown = errors.New("subscription closed by server, server is shutting down")
|
|
|
|
|
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
|
2022-04-05 14:26:14 +00:00
|
|
|
// receive. Topic, Subject, and Token are required.
|
2020-06-05 23:36:31 +00:00
|
|
|
type SubscribeRequest struct {
|
2022-04-05 14:26:14 +00:00
|
|
|
// Topic to subscribe to (e.g. service health).
|
2020-06-05 23:36:31 +00:00
|
|
|
Topic Topic
|
2022-04-05 14:26:14 +00:00
|
|
|
|
|
|
|
// Subject identifies the subset of Topic events the subscriber wishes to
|
|
|
|
// receive (e.g. events for a specific service). SubjectNone may be provided
|
|
|
|
// if all events on the given topic are "global" and not further partitioned
|
|
|
|
// by subject.
|
|
|
|
Subject Subject
|
|
|
|
|
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
|
2022-04-05 14:26:14 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-01-28 12:27:00 +00:00
|
|
|
func (req SubscribeRequest) topicSubject() topicSubject {
|
2022-04-05 14:26:14 +00:00
|
|
|
return topicSubject{
|
|
|
|
Topic: req.Topic.String(),
|
|
|
|
Subject: req.Subject.String(),
|
|
|
|
}
|
2022-01-28 12:27:00 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2022-01-28 12:27:00 +00:00
|
|
|
return newEventFromBatch(s.req, next.Events), 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
|
2022-05-23 12:59:13 +00:00
|
|
|
case subStateShuttingDown:
|
|
|
|
return ErrShuttingDown
|
2020-10-15 22:06:04 +00:00
|
|
|
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
|
|
|
|
2022-05-23 12:59:13 +00:00
|
|
|
// Close the subscription and indicate that the server is being shut down.
|
|
|
|
func (s *Subscription) shutDown() {
|
|
|
|
if atomic.CompareAndSwapUint32(&s.state, subStateOpen, subStateShuttingDown) {
|
|
|
|
close(s.closed)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|