transport

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2026 License: BSD-3-Clause Imports: 15 Imported by: 0

Documentation

Overview

Package transport defines the Transport interface and its implementations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AutoTransport

type AutoTransport struct {
	// contains filtered or unexported fields
}

AutoTransport attempts to connect via WebSocket first; on failure it falls back to the HTTP long-polling transport. Once a transport is selected it is used for the lifetime of the AutoTransport.

func NewAutoTransport

func NewAutoTransport(wsEndpoint, httpEndpoint, apiKey string, logger *slog.Logger) *AutoTransport

NewAutoTransport creates an AutoTransport that prefers WebSocket and falls back to HTTP long-polling.

func (*AutoTransport) Close

func (t *AutoTransport) Close() error

Close closes the active transport.

func (*AutoTransport) Connect

func (t *AutoTransport) Connect(ctx context.Context, evalCtx map[string]any) error

Connect attempts WebSocket first with the given evaluation context. If the WebSocket dial fails it transparently switches to HTTP long-polling. Blocks until the chosen transport is ready or ctx is cancelled.

func (*AutoTransport) FetchFlags

func (t *AutoTransport) FetchFlags(ctx context.Context, evalCtx map[string]any) (map[string]any, error)

FetchFlags delegates to the active transport.

func (*AutoTransport) OnFlagsUpdated

func (t *AutoTransport) OnFlagsUpdated(fn func(flags map[string]any))

OnFlagsUpdated registers the callback passed to whichever transport is eventually selected. Must be called before Connect.

type HTTPTransport

type HTTPTransport struct {
	// contains filtered or unexported fields
}

HTTPTransport polls the Flagmint backend periodically over HTTPS (long-polling). Safe for concurrent use after Connect() returns.

func NewHTTPTransport

func NewHTTPTransport(endpoint, apiKey string, logger *slog.Logger) *HTTPTransport

NewHTTPTransport creates a new HTTPTransport with default poll interval (20 minutes).

func NewHTTPTransportWithOptions

func NewHTTPTransportWithOptions(endpoint, apiKey string, logger *slog.Logger, opts HTTPTransportOptions) *HTTPTransport

NewHTTPTransportWithOptions creates a new HTTPTransport with explicit options.

func (*HTTPTransport) Close

func (t *HTTPTransport) Close() error

Close stops the poll loop and waits for all goroutines to exit. Safe to call multiple times.

func (*HTTPTransport) Connect

func (t *HTTPTransport) Connect(ctx context.Context, evalCtx map[string]any) error

Connect makes an initial flag fetch attempt and starts the background poll loop. The evalCtx parameter provides the initial evaluation context to use for polling. If the initial fetch fails (e.g. server unreachable), Connect logs a warning and starts the poll loop anyway; retries will happen at the configured interval. Connect blocks until the poll loop is started or ctx is cancelled.

func (*HTTPTransport) FetchFlags

func (t *HTTPTransport) FetchFlags(ctx context.Context, evalCtx map[string]any) (map[string]any, error)

FetchFlags sends the evaluation context to the evaluate endpoint and returns the flag set.

func (*HTTPTransport) OnFlagsUpdated

func (t *HTTPTransport) OnFlagsUpdated(fn func(flags map[string]any))

OnFlagsUpdated registers the callback invoked when the server pushes flag updates. Must be called before Connect.

type HTTPTransportOptions

type HTTPTransportOptions struct {
	// PollInterval controls how often the transport polls the evaluate endpoint.
	// Defaults to 20 minutes when zero.
	PollInterval time.Duration
	// HTTPClient overrides the default HTTP client. Useful for testing.
	HTTPClient *http.Client
}

HTTPTransportOptions configures optional parameters for HTTPTransport.

type Transport

type Transport interface {
	// Connect establishes the connection with optional initial evaluation context.
	// The context is sent in the x-flagmint-context header if provided.
	// Blocks until the transport is ready to send/receive, or ctx is cancelled.
	Connect(ctx context.Context, evalCtx map[string]any) error

	// FetchFlags sends the evaluation context to the server and returns the
	// evaluated flag set. evalCtx is a map representation of the evaluation
	// context. For WebSocket this sends a context message; for HTTP this makes
	// a POST request.
	FetchFlags(ctx context.Context, evalCtx map[string]any) (map[string]any, error)

	// OnFlagsUpdated registers a callback invoked when the server pushes flag
	// updates (WebSocket broadcasts or poll results with changes).
	// Must be called before Connect(). Only one callback is supported;
	// subsequent calls replace the previous callback.
	OnFlagsUpdated(fn func(flags map[string]any))

	// Close shuts down the transport and releases resources.
	// Blocks until all internal goroutines have exited.
	// Safe to call multiple times.
	Close() error
}

Transport abstracts the communication layer between the SDK and Flagmint servers. Implementations must be safe for concurrent use after Connect() returns.

type WebSocketTransport

type WebSocketTransport struct {
	// contains filtered or unexported fields
}

WebSocketTransport connects to the Flagmint backend over a persistent WebSocket connection and streams flag updates in real time. Safe for concurrent use after Connect() returns.

func NewWebSocketTransport

func NewWebSocketTransport(endpoint, apiKey string, logger *slog.Logger) *WebSocketTransport

NewWebSocketTransport creates a new WebSocketTransport.

func (*WebSocketTransport) Close

func (t *WebSocketTransport) Close() error

Close shuts down the WebSocket connection and waits for all goroutines to exit. Safe to call multiple times.

func (*WebSocketTransport) Connect

func (t *WebSocketTransport) Connect(ctx context.Context, evalCtx map[string]any) error

Connect establishes the WebSocket connection with optional initial context and starts the background read loop. If evalCtx is provided and non-empty, it is sent in the x-flagmint-context header. It blocks until the handshake completes or ctx is cancelled.

func (*WebSocketTransport) FetchFlags

func (t *WebSocketTransport) FetchFlags(ctx context.Context, evalCtx map[string]any) (map[string]any, error)

FetchFlags sends the current evaluation context to the server (via header if changed) and waits for the flag response. It uses the context to impose a deadline.

func (*WebSocketTransport) OnFlagsUpdated

func (t *WebSocketTransport) OnFlagsUpdated(fn func(flags map[string]any))

OnFlagsUpdated registers the callback invoked when the server pushes flag updates. Must be called before Connect.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL