Documentation
¶
Overview ¶
Package flipswitch provides an OpenFeature provider for Flipswitch with real-time SSE support.
This package wraps the OFREP provider for flag evaluation and adds real-time updates via Server-Sent Events (SSE).
Example:
provider, err := flipswitch.NewProvider("your-api-key")
if err != nil {
log.Fatal(err)
}
defer provider.Shutdown()
openfeature.SetProvider(provider)
client := openfeature.NewClient("my-app")
darkMode, _ := client.BooleanValue(ctx, "dark-mode", false, nil)
Index ¶
- type ApiKeyRotatedEvent
- type CancelFunc
- type ConfigUpdatedEvent
- type ConnectionStatus
- type ConnectionStatusHandler
- type FlagChangeEvent
- type FlagChangeHandler
- type FlagEvaluation
- type FlagUpdatedEvent
- type FlipswitchOptions
- type FlipswitchProvider
- func (p *FlipswitchProvider) AddFlagChangeListener(handler FlagChangeHandler) CancelFunc
- func (p *FlipswitchProvider) AddFlagKeyChangeListener(flagKey string, handler FlagChangeHandler) CancelFunc
- func (p *FlipswitchProvider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, ...) openfeature.BoolResolutionDetail
- func (p *FlipswitchProvider) EvaluateAllFlags(evalCtx openfeature.FlattenedContext) []FlagEvaluation
- func (p *FlipswitchProvider) EvaluateFlag(flagKey string, evalCtx openfeature.FlattenedContext) *FlagEvaluation
- func (p *FlipswitchProvider) EventChannel() <-chan openfeature.Event
- func (p *FlipswitchProvider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, ...) openfeature.FloatResolutionDetail
- func (p *FlipswitchProvider) GetSseStatus() ConnectionStatus
- func (p *FlipswitchProvider) Hooks() []openfeature.Hook
- func (p *FlipswitchProvider) Init(evaluationContext openfeature.EvaluationContext) error
- func (p *FlipswitchProvider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, ...) openfeature.IntResolutionDetail
- func (p *FlipswitchProvider) IsPollingActive() bool
- func (p *FlipswitchProvider) Metadata() openfeature.Metadata
- func (p *FlipswitchProvider) ObjectEvaluation(ctx context.Context, flag string, defaultValue interface{}, ...) openfeature.InterfaceResolutionDetail
- func (p *FlipswitchProvider) ReconnectSse()
- func (p *FlipswitchProvider) RemoveFlagChangeListener(handler FlagChangeHandler)deprecated
- func (p *FlipswitchProvider) Shutdown()
- func (p *FlipswitchProvider) StringEvaluation(ctx context.Context, flag string, defaultValue string, ...) openfeature.StringResolutionDetail
- type Option
- type SseClient
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ApiKeyRotatedEvent ¶ added in v0.1.2
type ApiKeyRotatedEvent struct {
// ValidUntil is the ISO timestamp when the current key expires.
// Empty if the rotation was aborted.
ValidUntil string `json:"validUntil"`
// Timestamp is the ISO timestamp of when the event occurred.
Timestamp string `json:"timestamp"`
}
ApiKeyRotatedEvent represents an API key rotation event received via SSE. If ValidUntil is empty, it indicates the rotation was aborted.
type CancelFunc ¶ added in v0.3.0
type CancelFunc func()
CancelFunc is returned by listener registration methods. Calling it removes the corresponding listener.
type ConfigUpdatedEvent ¶ added in v0.1.1
type ConfigUpdatedEvent struct {
// Timestamp is the ISO timestamp of when the change occurred.
Timestamp string `json:"timestamp"`
}
ConfigUpdatedEvent represents a configuration update event received via SSE.
type ConnectionStatus ¶
type ConnectionStatus string
ConnectionStatus represents the SSE connection status.
const ( // StatusConnecting indicates the client is connecting. StatusConnecting ConnectionStatus = "connecting" // StatusConnected indicates the client is connected. StatusConnected ConnectionStatus = "connected" // StatusDisconnected indicates the client is disconnected. StatusDisconnected ConnectionStatus = "disconnected" // StatusError indicates there was a connection error. StatusError ConnectionStatus = "error" )
type ConnectionStatusHandler ¶
type ConnectionStatusHandler func(status ConnectionStatus)
ConnectionStatusHandler is called when the SSE connection status changes.
type FlagChangeEvent ¶
type FlagChangeEvent struct {
// FlagKey is the key of the flag that changed, or empty for bulk invalidation.
FlagKey string `json:"flagKey,omitempty"`
// Timestamp is the ISO timestamp of when the change occurred.
Timestamp string `json:"timestamp"`
}
FlagChangeEvent represents a flag change event received via SSE (legacy/internal format).
func (*FlagChangeEvent) GetTimestampAsTime ¶
func (e *FlagChangeEvent) GetTimestampAsTime() (time.Time, error)
GetTimestampAsTime returns the timestamp as a time.Time object.
type FlagChangeHandler ¶
type FlagChangeHandler func(event FlagChangeEvent)
FlagChangeHandler is called when a flag changes.
type FlagEvaluation ¶
type FlagEvaluation struct {
// Key is the flag key.
Key string
// Value is the evaluated value.
Value interface{}
// ValueType is the type of the value (boolean, string, number, etc.).
ValueType string
// Reason is the reason for this evaluation result.
Reason string
// Variant is the variant that matched, if applicable.
Variant string
}
FlagEvaluation represents the result of evaluating a single flag.
func (*FlagEvaluation) AsBoolean ¶
func (e *FlagEvaluation) AsBoolean() bool
AsBoolean returns the value as a boolean.
func (*FlagEvaluation) AsFloat ¶
func (e *FlagEvaluation) AsFloat() float64
AsFloat returns the value as a float64.
func (*FlagEvaluation) AsInt ¶
func (e *FlagEvaluation) AsInt() int
AsInt returns the value as an integer.
func (*FlagEvaluation) AsString ¶
func (e *FlagEvaluation) AsString() string
AsString returns the value as a string.
func (*FlagEvaluation) GetValueAsString ¶
func (e *FlagEvaluation) GetValueAsString() string
GetValueAsString returns the value formatted for display.
type FlagUpdatedEvent ¶ added in v0.1.1
type FlagUpdatedEvent struct {
// FlagKey is the key of the flag that changed.
FlagKey string `json:"flagKey"`
// Timestamp is the ISO timestamp of when the change occurred.
Timestamp string `json:"timestamp"`
}
FlagUpdatedEvent represents a single flag update event received via SSE.
type FlipswitchOptions ¶
type FlipswitchOptions struct {
// APIKey is the environment API key (required).
APIKey string
// BaseURL is the Flipswitch server URL.
// Default: "https://api.flipswitch.io"
BaseURL string
// EnableRealtime enables SSE for real-time flag updates.
// Default: true
EnableRealtime bool
}
FlipswitchOptions contains configuration options for the Flipswitch provider.
type FlipswitchProvider ¶
type FlipswitchProvider struct {
// contains filtered or unexported fields
}
FlipswitchProvider is an OpenFeature provider for Flipswitch with real-time SSE support.
func NewProvider ¶
func NewProvider(apiKey string, opts ...Option) (*FlipswitchProvider, error)
NewProvider creates a new FlipswitchProvider with the given API key. Returns an error if the API key is empty.
func (*FlipswitchProvider) AddFlagChangeListener ¶
func (p *FlipswitchProvider) AddFlagChangeListener(handler FlagChangeHandler) CancelFunc
AddFlagChangeListener adds a listener for all flag change events. Returns a CancelFunc that removes the listener when called.
func (*FlipswitchProvider) AddFlagKeyChangeListener ¶ added in v0.3.0
func (p *FlipswitchProvider) AddFlagKeyChangeListener(flagKey string, handler FlagChangeHandler) CancelFunc
AddFlagKeyChangeListener adds a listener for changes to a specific flag key. The listener fires on targeted changes matching the key AND on bulk invalidations (events with empty FlagKey). Returns a CancelFunc that removes the listener when called.
func (*FlipswitchProvider) BooleanEvaluation ¶
func (p *FlipswitchProvider) BooleanEvaluation( ctx context.Context, flag string, defaultValue bool, evalCtx openfeature.FlattenedContext, ) openfeature.BoolResolutionDetail
BooleanEvaluation evaluates a boolean flag.
func (*FlipswitchProvider) EvaluateAllFlags ¶
func (p *FlipswitchProvider) EvaluateAllFlags(evalCtx openfeature.FlattenedContext) []FlagEvaluation
EvaluateAllFlags evaluates all flags for the given context. Returns a list of all flag evaluations with their keys, values, types, and reasons.
Note: This method makes direct HTTP calls since OFREP providers don't expose the bulk evaluation API.
func (*FlipswitchProvider) EvaluateFlag ¶
func (p *FlipswitchProvider) EvaluateFlag(flagKey string, evalCtx openfeature.FlattenedContext) *FlagEvaluation
EvaluateFlag evaluates a single flag and returns its evaluation result. Returns nil if the flag doesn't exist.
Note: This method makes direct HTTP calls for demo purposes. For standard flag evaluation, use the OpenFeature client methods.
func (*FlipswitchProvider) EventChannel ¶ added in v0.3.0
func (p *FlipswitchProvider) EventChannel() <-chan openfeature.Event
EventChannel returns the channel for OpenFeature provider events. Implements the openfeature.EventHandler interface.
func (*FlipswitchProvider) FloatEvaluation ¶
func (p *FlipswitchProvider) FloatEvaluation( ctx context.Context, flag string, defaultValue float64, evalCtx openfeature.FlattenedContext, ) openfeature.FloatResolutionDetail
FloatEvaluation evaluates a float flag.
func (*FlipswitchProvider) GetSseStatus ¶
func (p *FlipswitchProvider) GetSseStatus() ConnectionStatus
GetSseStatus returns the current SSE connection status.
func (*FlipswitchProvider) Hooks ¶
func (p *FlipswitchProvider) Hooks() []openfeature.Hook
Hooks returns any hooks the provider implements.
func (*FlipswitchProvider) Init ¶
func (p *FlipswitchProvider) Init(evaluationContext openfeature.EvaluationContext) error
Init initializes the provider. Validates the API key and starts SSE connection if real-time is enabled.
func (*FlipswitchProvider) IntEvaluation ¶
func (p *FlipswitchProvider) IntEvaluation( ctx context.Context, flag string, defaultValue int64, evalCtx openfeature.FlattenedContext, ) openfeature.IntResolutionDetail
IntEvaluation evaluates an integer flag.
func (*FlipswitchProvider) IsPollingActive ¶ added in v0.1.2
func (p *FlipswitchProvider) IsPollingActive() bool
IsPollingActive returns whether polling fallback is active.
func (*FlipswitchProvider) Metadata ¶
func (p *FlipswitchProvider) Metadata() openfeature.Metadata
Metadata returns the provider metadata.
func (*FlipswitchProvider) ObjectEvaluation ¶
func (p *FlipswitchProvider) ObjectEvaluation( ctx context.Context, flag string, defaultValue interface{}, evalCtx openfeature.FlattenedContext, ) openfeature.InterfaceResolutionDetail
ObjectEvaluation evaluates an object flag.
func (*FlipswitchProvider) ReconnectSse ¶
func (p *FlipswitchProvider) ReconnectSse()
ReconnectSse forces a reconnection of the SSE client.
func (*FlipswitchProvider) RemoveFlagChangeListener
deprecated
func (p *FlipswitchProvider) RemoveFlagChangeListener(handler FlagChangeHandler)
RemoveFlagChangeListener is deprecated. Use the CancelFunc returned by AddFlagChangeListener or AddFlagKeyChangeListener instead.
Deprecated: Function pointer comparison is unreliable in Go.
func (*FlipswitchProvider) Shutdown ¶
func (p *FlipswitchProvider) Shutdown()
Shutdown shuts down the provider and closes all connections.
func (*FlipswitchProvider) StringEvaluation ¶
func (p *FlipswitchProvider) StringEvaluation( ctx context.Context, flag string, defaultValue string, evalCtx openfeature.FlattenedContext, ) openfeature.StringResolutionDetail
StringEvaluation evaluates a string flag.
type Option ¶
type Option func(*FlipswitchProvider)
Option is a functional option for configuring the provider.
func WithBaseURL ¶
WithBaseURL sets the Flipswitch server base URL.
func WithHTTPClient ¶
WithHTTPClient sets a custom HTTP client.
func WithMaxSseRetries ¶ added in v0.1.2
WithMaxSseRetries sets the maximum SSE retry attempts before falling back to polling.
func WithPollingFallback ¶ added in v0.1.2
WithPollingFallback enables or disables polling fallback when SSE fails.
func WithPollingInterval ¶ added in v0.1.2
WithPollingInterval sets the polling interval for fallback mode.
func WithRealtime ¶
WithRealtime enables or disables real-time SSE updates.
type SseClient ¶
type SseClient struct {
// contains filtered or unexported fields
}
SseClient handles SSE connections for real-time flag change notifications.
func NewSseClient ¶
func NewSseClient( baseURL string, apiKey string, telemetryHeaders map[string]string, onFlagChange FlagChangeHandler, onStatusChange ConnectionStatusHandler, ) *SseClient
NewSseClient creates a new SSE client.
func (*SseClient) Close ¶
func (c *SseClient) Close()
Close closes the SSE connection and stops reconnection attempts.
func (*SseClient) Connect ¶
func (c *SseClient) Connect()
Connect starts the SSE connection in a background goroutine.
func (*SseClient) GetStatus ¶
func (c *SseClient) GetStatus() ConnectionStatus
GetStatus returns the current connection status.