Documentation
¶
Overview ¶
Package feature provides feature flag management with pluggable backends. Flags can be checked globally or per-user, with memory and database drivers.
Index ¶
- func CreateTableSQL() string
- func RequireFeature(mgr *Manager, name string) router.MiddlewareFunc
- func RequireFeatureForUser(mgr *Manager, name string, userIDFunc func(ctx *router.Context) string) router.MiddlewareFunc
- type Database
- func (d *Database) Active(ctx context.Context, name string) (bool, error)
- func (d *Database) ActiveFor(ctx context.Context, name string, userID string) (bool, error)
- func (d *Database) All(ctx context.Context) ([]Flag, error)
- func (d *Database) Delete(ctx context.Context, name string) error
- func (d *Database) Set(ctx context.Context, name string, active bool) error
- func (d *Database) SetForUser(ctx context.Context, name string, userID string, active bool) error
- type Flag
- type Manager
- type Memory
- func (m *Memory) Active(_ context.Context, name string) (bool, error)
- func (m *Memory) ActiveFor(ctx context.Context, name string, userID string) (bool, error)
- func (m *Memory) All(_ context.Context) ([]Flag, error)
- func (m *Memory) Delete(_ context.Context, name string) error
- func (m *Memory) Set(_ context.Context, name string, active bool) error
- func (m *Memory) SetForUser(_ context.Context, name string, userID string, active bool) error
- type Querier
- type Reader
- type Store
- type Writer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateTableSQL ¶
func CreateTableSQL() string
CreateTableSQL returns the SQL to create the feature_flags and feature_flag_users tables.
func RequireFeature ¶
func RequireFeature(mgr *Manager, name string) router.MiddlewareFunc
RequireFeature returns middleware that responds with 404 if the named flag is not active. This hides routes behind feature flags without changing handler code.
func RequireFeatureForUser ¶
func RequireFeatureForUser(mgr *Manager, name string, userIDFunc func(ctx *router.Context) string) router.MiddlewareFunc
RequireFeatureForUser returns middleware that responds with 404 if the named flag is not active for the given user. The userIDFunc extracts the user ID from the request context.
Types ¶
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
Database is a database-backed feature flag store. It stores flags in a "feature_flags" table and per-user overrides in a "feature_flag_users" table. Create these tables with the provided migration SQL before using this driver.
func NewDatabase ¶
NewDatabase creates a database-backed feature flag store.
func (*Database) ActiveFor ¶
ActiveFor returns whether a flag is enabled for a specific user. Falls back to the global flag state if no per-user override exists.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager provides a convenient API over a Reader for checking feature flags. It silently treats errors as inactive to simplify handler code.
func NewManager ¶
NewManager creates a feature flag manager backed by the given reader.
func (*Manager) Active ¶
Active returns true if the named flag is globally enabled. Returns false on any error.
type Memory ¶
type Memory struct {
// contains filtered or unexported fields
}
Memory is an in-memory feature flag store. Thread-safe.
func NewMemory ¶
NewMemory creates an in-memory feature flag store with initial flag values. Pass nil for an empty store.
func (*Memory) ActiveFor ¶
ActiveFor returns whether a flag is enabled for a specific user. Falls back to the global flag state if no per-user override exists.
type Querier ¶
type Querier interface {
Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error)
QueryRow(ctx context.Context, sql string, args ...any) pgx.Row
Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error)
}
Querier abstracts database query execution for the feature flag store. Satisfied by both *database.DB and *database.TX without importing the database package, avoiding circular dependencies.
type Reader ¶
type Reader interface {
// Active returns whether a flag is globally enabled.
Active(ctx context.Context, name string) (bool, error)
// ActiveFor returns whether a flag is enabled for a specific user.
// Falls back to the global flag state if no per-user override exists.
ActiveFor(ctx context.Context, name string, userID string) (bool, error)
}
Reader checks feature flag state. This is the interface consumed by Manager and middleware — keep it small for easy faking in tests.
type Writer ¶
type Writer interface {
// Set enables or disables a flag globally.
Set(ctx context.Context, name string, active bool) error
// SetForUser enables or disables a flag for a specific user.
SetForUser(ctx context.Context, name string, userID string, active bool) error
// Delete removes a flag entirely.
Delete(ctx context.Context, name string) error
// All returns all registered flags.
All(ctx context.Context) ([]Flag, error)
}
Writer mutates feature flag state.