flags

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package flags provides config-driven feature flags with hot-reload and percentage rollouts, requiring no external service.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Instance name
	Name string `koanf:"-"`

	// Flags holds the raw flag definitions: scalars for plain values, or
	// {value, rollout} objects for percentage rollouts. Prefer snake_case
	// flag names; hyphens cannot be overridden via environment variables.
	Flags map[string]any `koanf:"flags"`
}

Config represents configuration for the feature flags Module

func NewConfig

func NewConfig(options ...Option) Config

NewConfig returns configuration with provided options based on defaults.

func NewDefaultConfig

func NewDefaultConfig() Config

NewDefaultConfig returns default configuration

func (*Config) LoadFromKoanf

func (c *Config) LoadFromKoanf(k *koanf.Koanf, path string) error

LoadFromKoanf loads configuration from koanf instance at the given path.

type Flags

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

Flags exposes lock-free reads over the current flag snapshot. The snapshot is swapped wholesale on config hot-reload, so reads never observe a half-applied reload.

func (*Flags) Bool

func (f *Flags) Bool(ctx context.Context, name string, def bool) bool

Bool returns the flag's boolean value, or def if missing or mistyped. Rollout percentages are ignored; use Flags.BoolFor for rollout-aware reads.

func (*Flags) BoolFor

func (f *Flags) BoolFor(ctx context.Context, name, key string, def bool) bool

BoolFor returns the flag's boolean value gated by its rollout percentage: a stable hash of the flag name and key decides whether key is in the enabled bucket. Without a rollout it behaves like Flags.Bool.

func (*Flags) Duration

func (f *Flags) Duration(ctx context.Context, name string, def time.Duration) time.Duration

Duration returns the flag's duration value, or def if missing or mistyped.

func (*Flags) Float

func (f *Flags) Float(ctx context.Context, name string, def float64) float64

Float returns the flag's float value, or def if missing or mistyped.

func (*Flags) Int

func (f *Flags) Int(ctx context.Context, name string, def int) int

Int returns the flag's integer value, or def if missing or mistyped.

func (*Flags) String

func (f *Flags) String(ctx context.Context, name, def string) string

String returns the flag's string value, or def if missing or mistyped.

type Module

type Module struct {
	lakta.NamedBase
	// contains filtered or unexported fields
}

Module provides config-driven Flags via DI, hot-reloaded on config change.

func NewModule

func NewModule(options ...Option) *Module

NewModule creates a new feature flags module.

func (*Module) ConfigPath

func (m *Module) ConfigPath() string

ConfigPath returns the koanf path for this module's configuration.

func (*Module) Dependencies

func (m *Module) Dependencies() ([]reflect.Type, []reflect.Type)

Dependencies declares the optional types this module needs from DI before Init.

func (*Module) Init

func (m *Module) Init(ctx context.Context) error

Init parses the flag definitions and provides Flags to the injector.

func (*Module) LoadConfig

func (m *Module) LoadConfig(k *koanf.Koanf) error

LoadConfig loads configuration from koanf.

func (*Module) OnReload

func (m *Module) OnReload(k *koanf.Koanf)

OnReload re-parses flag definitions from the reloaded config, keeping the previous snapshot if the new one fails to parse. The OnReload contract provides no context, so use the default logger.

func (*Module) Provides

func (m *Module) Provides() []reflect.Type

Provides returns the types this module registers in DI.

func (*Module) Shutdown

func (m *Module) Shutdown(_ context.Context) error

Shutdown is a no-op for the flags module.

type Option

type Option func(m *Config)

Option configures the Module.

func WithName

func WithName(name string) Option

WithName sets the instance name for this module.

Jump to

Keyboard shortcuts

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