2020-07-06 21:29:45 +00:00
|
|
|
/*
|
|
|
|
Package stream provides a publish/subscribe system for events produced by changes
|
|
|
|
to the state store.
|
|
|
|
*/
|
2020-06-05 23:36:31 +00:00
|
|
|
package stream
|
|
|
|
|
2020-07-07 00:04:24 +00:00
|
|
|
import "fmt"
|
2020-06-05 23:36:31 +00:00
|
|
|
|
2020-07-07 00:04:24 +00:00
|
|
|
// Topic is an identifier that partitions events. A subscription will only receive
|
|
|
|
// events which match the Topic.
|
|
|
|
type Topic fmt.Stringer
|
2020-06-05 23:36:31 +00:00
|
|
|
|
2020-07-07 00:04:24 +00:00
|
|
|
// Event is a structure with identifiers and a payload. Events are Published to
|
|
|
|
// EventPublisher and returned to Subscribers.
|
2020-06-05 23:36:31 +00:00
|
|
|
type Event struct {
|
|
|
|
Topic Topic
|
|
|
|
Key string
|
|
|
|
Index uint64
|
|
|
|
Payload interface{}
|
|
|
|
}
|
|
|
|
|
2020-07-08 18:45:18 +00:00
|
|
|
// IsEndOfSnapshot returns true if this is a framing event that indicates the
|
|
|
|
// snapshot has completed. Future events from Subscription.Next will be
|
|
|
|
// change events.
|
2020-06-15 22:49:00 +00:00
|
|
|
func (e Event) IsEndOfSnapshot() bool {
|
2020-06-05 23:36:31 +00:00
|
|
|
return e.Payload == endOfSnapshot{}
|
|
|
|
}
|
|
|
|
|
2020-07-08 18:45:18 +00:00
|
|
|
// IsEndOfEmptySnapshot returns true if this is a framing event that indicates
|
|
|
|
// there is no snapshot. Future events from Subscription.Next will be
|
|
|
|
// change events.
|
|
|
|
func (e Event) IsEndOfEmptySnapshot() bool {
|
|
|
|
return e.Payload == endOfEmptySnapshot{}
|
2020-06-15 22:49:00 +00:00
|
|
|
}
|
|
|
|
|
2020-06-05 23:36:31 +00:00
|
|
|
type endOfSnapshot struct{}
|
2020-06-15 22:49:00 +00:00
|
|
|
|
2020-07-08 18:45:18 +00:00
|
|
|
type endOfEmptySnapshot struct{}
|
2020-07-06 18:34:58 +00:00
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
type closeSubscriptionPayload struct {
|
|
|
|
tokensSecretIDs []string
|
2020-07-06 18:34:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-06 21:29:45 +00:00
|
|
|
// NewCloseSubscriptionEvent returns a special Event that is handled by the
|
|
|
|
// stream package, and is never sent to subscribers. EventProcessor handles
|
|
|
|
// these events, and closes any subscriptions which were created using a token
|
|
|
|
// which matches any of the tokenSecretIDs.
|
2020-07-08 18:45:18 +00:00
|
|
|
//
|
|
|
|
// tokenSecretIDs may contain duplicate IDs.
|
2020-07-06 21:29:45 +00:00
|
|
|
func NewCloseSubscriptionEvent(tokenSecretIDs []string) Event {
|
|
|
|
return Event{Payload: closeSubscriptionPayload{tokensSecretIDs: tokenSecretIDs}}
|
2020-07-06 18:34:58 +00:00
|
|
|
}
|