Documentation
¶
Overview ¶
Package streams defines the Relay core abstractions for implementing streaming endpoints, and provides some standard implementations for those endpoints.
Index ¶
- func MakeEventsForApplyDelta(changes []subsystems.Change, selector subsystems.Selector) []eventsource.Event
- func MakeEventsForSetBasis(changes []subsystems.Change, selector subsystems.Selector) []eventsource.Event
- func MakeEventsForUpToDate(selector subsystems.Selector) []eventsource.Event
- func MakePingEvent() eventsource.Event
- func MakeServerSideDeleteEvent(kind ldstoretypes.DataKind, key string, version int) eventsource.Event
- func MakeServerSideFlagsOnlyDeleteEvent(key string, version int) eventsource.Event
- func MakeServerSideFlagsOnlyPatchEvent(key string, item ldstoretypes.ItemDescriptor) eventsource.Event
- func MakeServerSideFlagsOnlyPutEvent(allData []ldstoretypes.Collection) eventsource.Event
- func MakeServerSidePatchEvent(kind ldstoretypes.DataKind, key string, item ldstoretypes.ItemDescriptor) eventsource.Event
- func MakeServerSidePutEvent(allData []ldstoretypes.Collection) eventsource.Event
- type EnvStoreQueries
- type EnvStreamProvider
- type EnvStreamUpdates
- type EnvStreams
- type InitObserver
- type Option
- type StreamProvider
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MakeEventsForApplyDelta ¶
func MakeEventsForApplyDelta(changes []subsystems.Change, selector subsystems.Selector) []eventsource.Event
func MakeEventsForSetBasis ¶
func MakeEventsForSetBasis(changes []subsystems.Change, selector subsystems.Selector) []eventsource.Event
func MakeEventsForUpToDate ¶
func MakeEventsForUpToDate(selector subsystems.Selector) []eventsource.Event
func MakePingEvent ¶
func MakePingEvent() eventsource.Event
MakePingEvent creates a "ping" event for client-side SDKs.
func MakeServerSideDeleteEvent ¶
func MakeServerSideDeleteEvent(kind ldstoretypes.DataKind, key string, version int) eventsource.Event
MakeServerSideDeleteEvent creates a "delete" event for server-side SDKs.
func MakeServerSideFlagsOnlyDeleteEvent ¶
func MakeServerSideFlagsOnlyDeleteEvent(key string, version int) eventsource.Event
MakeServerSideFlagsOnlyDeleteEvent creates a "delete" event for old server-side SDKs that use the flags-only stream.
func MakeServerSideFlagsOnlyPatchEvent ¶
func MakeServerSideFlagsOnlyPatchEvent(key string, item ldstoretypes.ItemDescriptor) eventsource.Event
MakeServerSideFlagsOnlyPatchEvent creates a "patch" event for old server-side SDKs that use the flags-only stream.
func MakeServerSideFlagsOnlyPutEvent ¶
func MakeServerSideFlagsOnlyPutEvent(allData []ldstoretypes.Collection) eventsource.Event
MakeServerSideFlagsOnlyPutEvent creates a "put" event for old server-side SDKs that use the flags-only stream.
func MakeServerSidePatchEvent ¶
func MakeServerSidePatchEvent( kind ldstoretypes.DataKind, key string, item ldstoretypes.ItemDescriptor, ) eventsource.Event
MakeServerSidePatchEvent creates a "patch" event for server-side SDKs.
func MakeServerSidePutEvent ¶
func MakeServerSidePutEvent(allData []ldstoretypes.Collection) eventsource.Event
MakeServerSidePutEvent creates a "put" event for server-side SDKs.
Types ¶
type EnvStoreQueries ¶
type EnvStoreQueries interface {
IsInitialized() bool
Snapshot() (map[ldstoretypes.DataKind][]ldstoretypes.KeyedItemDescriptor, subsystems.Selector, error)
}
EnvStoreQueries is a subset of DataStore methods that are used by EnvStreams to query existing data from the store, for generating "put" events.
type EnvStreamProvider ¶
type EnvStreamProvider interface {
EnvStreamUpdates // Apply
// SendHeartbeat sends keep-alive data on the stream.
SendHeartbeat()
// Close releases all resources for this EnvStreamProvider and closes all connections to it.
Close()
}
EnvStreamProvider is an abstraction of publishing events to a stream for a specific environment. Implementations of this interface are created by StreamProvider.Register().
type EnvStreamUpdates ¶
type EnvStreamUpdates interface {
// Apply accepts a changeSet containing data store modifications (such as flag or segment updates)
// and forwards it to all active streams for broadcasting to connected SDK clients. The changeSet
// represents atomic changes to be applied to the data store and will be serialized appropriately
// for each stream type (server-side vs client-side SDKs).
Apply(changeSet subsystems.ChangeSet)
// InvalidateClientSideState signals an update that could affect client-side evaluations, but does
// not change any server-side data item. Updating big segment data is such an event, since the
// segment in the regular server-side SDK data does not change but evaluating the segment could
// now produce a different result. Nothing is broadcast to the server-side SDKs (since they have
// their own mechanisms for detecting big segment updates), but all connected client-side SDKs will
// receive a "ping" event to refresh their state.
InvalidateClientSideState()
}
EnvStreamUpdates is an interface representing the kinds of updates we can publish to streams. Other components that publish updates to EnvStreams should use this interface rather than the implementation type, both to clarify that they don't need other EnvStreams functionality and to simplify testing.
type EnvStreams ¶
type EnvStreams struct {
// contains filtered or unexported fields
}
EnvStreams encapsulates streaming behavior for a specific environment.
EnvStreams itself does not know anything about what kind of streams are available; those are determined by the StreamProvider instances that are passed in the constructor, and the credentials that are registered with AddCredential. For each combination of a credential and a StreamProvider that can handle that credential, a stream is available, and data updates that are sent with the EnvStreamUpdates methods will be rebroadcast to all of those streams, in a format that is determined by each StreamProvider.
func NewEnvStreams ¶
func NewEnvStreams( streamProviders []StreamProvider, storeQueries EnvStoreQueries, heartbeatInterval time.Duration, logger *slog.Logger, ) *EnvStreams
NewEnvStreams creates an instance of EnvStreams.
func (*EnvStreams) AddCredential ¶
func (es *EnvStreams) AddCredential(credential credential.SDKCredential)
AddCredential adds an environment keyed off the credential, and creates a corresponding EnvStreamProvider.
func (*EnvStreams) Apply ¶
func (es *EnvStreams) Apply(changeSet subsystems.ChangeSet)
func (*EnvStreams) Close ¶
func (es *EnvStreams) Close() error
Close shuts down all currently active streams for this environment and releases its resources.
func (*EnvStreams) InvalidateClientSideState ¶
func (es *EnvStreams) InvalidateClientSideState()
InvalidateClientSideState sends all appropriate stream updates for when client-side state should be refreshed.
func (*EnvStreams) RemoveCredential ¶
func (es *EnvStreams) RemoveCredential(credential credential.SDKCredential)
RemoveCredential shuts down the EnvStreamProvider, if any, specified by the credential.
type InitObserver ¶
type InitObserver interface {
// RecordDelivery records one gated delivery. The outcome is completed,
// connection_ended, or read_error.
RecordDelivery(protocol, outcome string, capEngaged bool)
// RecordUpToDate records one up-to-date reply. afterWait is true when the client's
// basis became current while it waited in the queue.
RecordUpToDate(afterWait bool)
// RecordShed records one shed stream replay.
RecordShed()
// AddDeadlineSetErrors adds the count of failed write-deadline calls a delivery saw.
AddDeadlineSetErrors(n int64)
}
InitObserver receives the initialization-delivery measurements the stream provider makes: deliveries with their outcomes, sheds, up-to-date replies, and failed write-deadline calls. The metrics package implements it. A nil observer is permitted; the provider then records nothing.
type Option ¶
type Option func(*providerOptions)
Option customizes a StreamProvider created by NewStreamProvider.
func WithInitLimiter ¶
func WithInitLimiter(limiter *concurrency.Limiter, sendTimeout time.Duration) Option
WithInitLimiter limits how many stream replays may send a FULL basis at the same time. The replays draw from the shared initialization-delivery budget, the same limiter the polls use. A replay that is already up-to-date, and the deltas, do not draw from it. Only the server-side stream provider obeys this option; the other stream kinds ignore it. A limiter that is nil or disabled applies no limit. sendTimeout frees a slot when an initialization payload makes no progress toward a stalled client in that time.
func WithInitObserver ¶
func WithInitObserver(o InitObserver) Option
WithInitObserver sends the initialization-delivery measurements to the given observer. Only the server-side stream provider records them.
func WithLogger ¶
WithLogger sends the connection-write errors of the SSE server to a logger. This makes a connection that the init limiter cut (its write deadline expired) visible instead of silently lost. Only the server-side stream provider uses the logger. Without it, those errors go nowhere.
type StreamProvider ¶
type StreamProvider interface {
// HandlerV1 returns an HTTP request handler for the given scoped SDK credential.
// It can return nil if it does not support this type of credential.
//
// This handler will service requests using the old FDv1 protocol.
HandlerV1(credential credential.SDKCredential) http.HandlerFunc
// HandlerV2 returns an HTTP request handler for the given scoped SDK credential.
// It can return nil if it does not support this type of credential.
//
// This handler will service requests using the new FDv2 protocol.
HandlerV2(credential credential.SDKCredential) http.HandlerFunc
// RegisterV1 tells the StreamProvider about an environment that it should support, and returns an
// implementation of EnvStreamProvider for pushing updates related to that environment. It can
// return nil if it does not support this type of credential.
//
// This method is used for the old FDv1 protocol.
RegisterV1(credential credential.SDKCredential, store EnvStoreQueries, logger *slog.Logger) EnvStreamProvider
// RegisterV2 tells the StreamProvider about an environment that it should support, and returns an
// implementation of EnvStreamProvider for pushing updates related to that environment. It can
// return nil if it does not support this type of credential.
//
// This method is used for the FDv2 protocol, the data-system protocol v9 adds.
RegisterV2(credential credential.SDKCredential, store EnvStoreQueries, logger *slog.Logger) EnvStreamProvider
// Close tells the StreamProvider to release all of its resources and close all connections.
Close()
}
StreamProvider is an abstraction of a specific kind of SSE event stream, such as the server-side SDK "/all" stream. The streams package provides default implementations of this interface for the streams that are supported by the standard Relay Proxy.
Each StreamProvider expects a specific kind of SDKCredential, e.g. config.SDKKey for the server-side streams. If the wrong kind of credential is passed, it should behave as it would for an unrecognized key. It is important that there can be more than one StreamProvider for a given credential.
func NewStreamProvider ¶
func NewStreamProvider(kind basictypes.StreamKind, maxConnTime, pingStreamJitterTime time.Duration, opts ...Option) StreamProvider
NewStreamProvider creates a StreamProvider implementation for the specified kind of stream endpoint. opts adjust the provider, for example WithInitLimiter for the server-side stream. The stream kinds that do not deliver a full basis ignore them.