Documentation
¶
Overview ¶
Package multi is an experimental implementation of a of.FeatureProvider that supports evaluating multiple feature flag providers together.
Index ¶
- Constants
- Variables
- func BuildDefaultResult[R FlagTypes](strategy EvaluationStrategy, defaultValue R, err error) of.GenericResolutionDetail[R]
- func Evaluate[T FlagTypes](ctx context.Context, provider NamedProvider, flag string, defaultVal T, ...) of.GenericResolutionDetail[T]
- type AggregateError
- type Comparator
- type EvaluationStrategy
- type FlagTypes
- type NamedProvider
- type Option
- func WithCustomComparator(comparator Comparator) Option
- func WithCustomStrategy(s StrategyConstructor) Option
- func WithFallbackProvider(p of.FeatureProvider) Option
- func WithGlobalHooks(hooks ...of.Hook) Option
- func WithLogger(l *slog.Logger) Option
- func WithProvider(providerName string, provider of.FeatureProvider, hooks ...of.Hook) Option
- type Provider
- func (p *Provider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, ...) of.BoolResolutionDetail
- func (p *Provider) EvaluationStrategy() string
- func (p *Provider) EventChannel() <-chan of.Event
- func (p *Provider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, ...) of.FloatResolutionDetail
- func (p *Provider) Hooks() []of.Hook
- func (p *Provider) Init(evalCtx of.EvaluationContext) error
- func (p *Provider) InitWithContext(ctx context.Context, evalCtx of.EvaluationContext) error
- func (p *Provider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, ...) of.IntResolutionDetail
- func (p *Provider) Metadata() of.Metadata
- func (p *Provider) ObjectEvaluation(ctx context.Context, flag string, defaultValue any, ...) of.InterfaceResolutionDetail
- func (p *Provider) Providers() []NamedProvider
- func (p *Provider) Shutdown()
- func (p *Provider) ShutdownWithContext(ctx context.Context) error
- func (p *Provider) Status() of.State
- func (p *Provider) StringEvaluation(ctx context.Context, flag string, defaultValue string, ...) of.StringResolutionDetail
- func (p *Provider) Track(ctx context.Context, trackingEventName string, ...)
- type ProviderError
- type StrategyConstructor
- type StrategyFn
Constants ¶
const ( MetadataProviderName = "multiprovider-provider-name" MetadataProviderType = "multiprovider-provider-type" MetadataSuccessfulProviderName = "multiprovider-successful-provider-name" MetadataSuccessfulProviderNames = MetadataSuccessfulProviderName + "s" MetadataStrategyUsed = "multiprovider-strategy-used" MetadataFallbackUsed = "multiprovider-fallback-used" MetadataIsDefaultValue = "multiprovider-is-result-default-value" MetadataEvaluationError = "multiprovider-evaluation-error" MetadataComparisonDisagreeingProviders = "multiprovider-comparison-disagreeing-providers" )
Metadata Keys
const ( // ReasonAggregated - the resolved value was the agreement of all providers in the multi.Provider using the // [StrategyComparison] strategy ReasonAggregated of.Reason = "AGGREGATED" // ReasonAggregatedFallback ReasonAggregated - the resolved value was result of the fallback provider because the // providers in multi.Provider were not in agreement using the [StrategyComparison] strategy. ReasonAggregatedFallback of.Reason = "AGGREGATED_FALLBACK" )
Additional of.Reason options
Variables ¶
var ErrAggregationNotAllowed = errors.New(errAggregationNotAllowedText)
ErrAggregationNotAllowed is an error returned if of.FeatureProvider.ObjectEvaluation is called using the StrategyComparison strategy without a custom Comparator function configured when response objects are not comparable.
Functions ¶
func BuildDefaultResult ¶
func BuildDefaultResult[R FlagTypes](strategy EvaluationStrategy, defaultValue R, err error) of.GenericResolutionDetail[R]
BuildDefaultResult should be called when a StrategyFn is in a failure state and needs to return a default value. This method will build a resolution detail with the internal provided error set. This method is exported for those writing their own custom StrategyFn.
func Evaluate ¶
func Evaluate[T FlagTypes](ctx context.Context, provider NamedProvider, flag string, defaultVal T, flatCtx of.FlattenedContext) of.GenericResolutionDetail[T]
Evaluate is a generic method used to resolve a flag from a single NamedProvider without losing type information. This method is exported for those writing their own custom StrategyFn. Since any is an allowed FlagTypes this can be set to any type, but this should be done with care outside the specified primitive FlagTypes
Types ¶
type AggregateError ¶
type AggregateError []ProviderError
AggregateError is a map that contains up to one error per provider within the multiprovider.
func NewAggregateError ¶
func NewAggregateError(providerErrors []ProviderError) AggregateError
NewAggregateError creates a new AggregateError from a slice of ProviderError instances
func (AggregateError) Error ¶
func (ae AggregateError) Error() string
type Comparator ¶
Comparator is used to compare the results of of.FeatureProvider.ObjectEvaluation. This is required if returned results are not comparable.
type EvaluationStrategy ¶
type EvaluationStrategy = string
EvaluationStrategy Defines a strategy to use for resolving the result from multiple providers.
const ( // StrategyFirstMatch returns the result of the first [of.FeatureProvider] whose response is not [of.FlagNotFoundCode]. // This is executed sequentially, and not in parallel. Any returned errors from a provider other than flag not found // will result in a default response with a set error. StrategyFirstMatch EvaluationStrategy = "strategy-first-match" // StrategyFirstSuccess returns the result of the first [of.FeatureProvider] whose response that is not an error. // This is very similar to [StrategyFirstMatch], but does not raise errors. This is executed sequentially. StrategyFirstSuccess EvaluationStrategy = "strategy-first-success" // StrategyComparison returns a response of all [of.FeatureProvider] instances in agreement. All providers are // called in parallel and then the results of each non-error result are compared to each other. If all responses // agree, then that value will be returned. Otherwise, the value from the designated fallback [of.FeatureProvider] // instance's response will be returned. The fallback provider will be assigned to the first provider registered if // the [WithFallbackProvider] Option is not explicitly set. StrategyComparison EvaluationStrategy = "strategy-comparison" // StrategyCustom allows for using a custom [StrategyFn] implementation. If this is set you MUST use the WithCustomStrategy // Option to set it StrategyCustom EvaluationStrategy = "strategy-custom" )
EvaluationStrategy options
type NamedProvider ¶
type NamedProvider interface {
of.FeatureProvider
// Name returns the unique name assigned to the provider.
Name() string
}
NamedProvider extends of.FeatureProvider by adding a unique provider name.
type Option ¶
type Option func(*configuration)
Option function used for setting configuration via the options pattern
func WithCustomComparator ¶
func WithCustomComparator(comparator Comparator) Option
WithCustomComparator sets a custom Comparator to use when using StrategyComparison when of.FeatureProvider.ObjectEvaluation is performed. This is required if the returned objects are not comparable, otherwise an error occur..
func WithCustomStrategy ¶
func WithCustomStrategy(s StrategyConstructor) Option
WithCustomStrategy sets a custom strategy function by defining a "constructor" that acts as closure over a slice of NamedProvider instances with your returned custom strategy function. This must be used in conjunction with StrategyCustom
func WithFallbackProvider ¶
func WithFallbackProvider(p of.FeatureProvider) Option
WithFallbackProvider sets a fallback provider when using the StrategyComparison setting. The fallback provider is called when providers are not in agreement. If a fallback provider is not set and providers are not agreement, then the default result will be returned along with an error value.
func WithGlobalHooks ¶
WithGlobalHooks sets the global hooks for the provider. These are of.Hook instances that affect ALL of.FeatureProvider instances. To apply hooks to specific providers, attach them directly to that provider, or include them in the WithProvider Option if the provider does not support its own hook functionality.
func WithLogger ¶
WithLogger sets a logger to be used with slog for internal logging. By default, all logs are discarded unless this is set.
func WithProvider ¶
WithProvider registers a specific of.FeatureProvider instance under the given providerName. The providerName must be unique and correspond to the name used when creating the Provider. Optional of.Hook instances may also be provided, which will execute only for this specific provider. This Option can be used multiple times with unique provider names to register multiple providers. The order in which options are provided determines the order in which the providers are registered and evaluated.
type Provider ¶
type Provider struct {
// contains filtered or unexported fields
}
Provider is an implementation of of.FeatureProvider that can execute multiple providers using various strategies.
func NewProvider ¶
func NewProvider(evaluationStrategy EvaluationStrategy, options ...Option) (*Provider, error)
NewProvider returns a new multi.Provider that acts as a unified interface of multiple providers for interaction.
func (*Provider) BooleanEvaluation ¶
func (p *Provider) BooleanEvaluation(ctx context.Context, flag string, defaultValue bool, flatCtx of.FlattenedContext) of.BoolResolutionDetail
BooleanEvaluation evaluates the flag and returns a of.BoolResolutionDetail.
func (*Provider) EvaluationStrategy ¶
EvaluationStrategy The name of the currently set EvaluationStrategy.
func (*Provider) EventChannel ¶
EventChannel is the channel that all events are emitted on.
func (*Provider) FloatEvaluation ¶
func (p *Provider) FloatEvaluation(ctx context.Context, flag string, defaultValue float64, flatCtx of.FlattenedContext) of.FloatResolutionDetail
FloatEvaluation evaluates the flag and returns a of.FloatResolutionDetail.
func (*Provider) Hooks ¶
Hooks returns a collection of.Hook instances configured to the provider using WithGlobalHooks.
func (*Provider) Init ¶
func (p *Provider) Init(evalCtx of.EvaluationContext) error
Init will run the initialize method for all internal of.FeatureProvider instances and aggregate any errors.
func (*Provider) InitWithContext ¶
InitWithContext will run the initialize method for all internal of.FeatureProvider instances and aggregate any errors.
func (*Provider) IntEvaluation ¶
func (p *Provider) IntEvaluation(ctx context.Context, flag string, defaultValue int64, flatCtx of.FlattenedContext) of.IntResolutionDetail
IntEvaluation evaluates the flag and returns an of.IntResolutionDetail.
func (*Provider) Metadata ¶
Metadata provides the name "multiprovider" along with the names and types of each internal of.FeatureProvider.
func (*Provider) ObjectEvaluation ¶
func (p *Provider) ObjectEvaluation(ctx context.Context, flag string, defaultValue any, flatCtx of.FlattenedContext) of.InterfaceResolutionDetail
ObjectEvaluation evaluates the flag and returns an of.InterfaceResolutionDetail. For the purposes of evaluation within strategies, the type of the default value is used as the assumed type of the returned responses from each provider. This is especially important when using the StrategyComparison configuration as an internal error will occur if this is not a comparable type unless the WithCustomComparator Option is configured.
func (*Provider) Providers ¶
func (p *Provider) Providers() []NamedProvider
Providers returns slice of providers wrapped in NamedProvider structs.
func (*Provider) Shutdown ¶
func (p *Provider) Shutdown()
Shutdown Shuts down all internal of.FeatureProvider instances and internal event listeners
func (*Provider) ShutdownWithContext ¶
ShutdownWithContext shuts down all internal of.FeatureProvider instances and internal event listeners
func (*Provider) Status ¶
Status provides the current state of the multi.Provider.
func (*Provider) StringEvaluation ¶
func (p *Provider) StringEvaluation(ctx context.Context, flag string, defaultValue string, flatCtx of.FlattenedContext) of.StringResolutionDetail
StringEvaluation evaluates the flag and returns a of.StringResolutionDetail.
func (*Provider) Track ¶
func (p *Provider) Track(ctx context.Context, trackingEventName string, evaluationContext of.EvaluationContext, details of.TrackingEventDetails)
Track implements the of.Tracker interface by forwarding tracking calls to all internal providers that are in ready state and implement the of.Tracker interface.
type ProviderError ¶
type ProviderError struct {
// ProviderName is the name of the provider that returned the included error
ProviderName string
// contains filtered or unexported fields
}
ProviderError is an error wrapper that includes the provider name.
func (*ProviderError) Error ¶
func (e *ProviderError) Error() string
Error implements the error interface for ProviderError.
func (*ProviderError) Unwrap ¶
func (e *ProviderError) Unwrap() error
Unwrap allows access to the original error, if any.
type StrategyConstructor ¶
type StrategyConstructor func(providers []NamedProvider) StrategyFn[FlagTypes]
StrategyConstructor defines the signature for the function that will be called to retrieve the closure that acts as the custom strategy implementation. This function should return a StrategyFn
type StrategyFn ¶
type StrategyFn[T FlagTypes] func(ctx context.Context, flag string, defaultValue T, flatCtx of.FlattenedContext) of.GenericResolutionDetail[T]
StrategyFn defines the signature for a strategy function.