featureflag

package
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package featureflag evaluates runtime feature flags (boolean, percentage rollout, and per-user/group targeting) through a pluggable provider, with built-in Static, Rules, and Redis providers and a Fiber helper.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Enabled

func Enabled(c *fiber.Ctx, flag string) bool

Enabled reports whether flag is on for the current request, using the Flags and Eval that Middleware stored. It returns false if the middleware is not installed.

Types

type Eval

type Eval struct {
	UserID string
	Groups []string
	Attrs  map[string]string
}

Eval carries the attributes a provider targets on when deciding a flag.

type Flags

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

Flags is the facade over a Provider.

func New

func New(p Provider) *Flags

New returns a Flags backed by the given provider.

func (*Flags) Enabled

func (f *Flags) Enabled(ctx context.Context, flag string, e Eval) bool

Enabled reports whether flag is on for the evaluation e.

Example

Evaluate a flag with a percentage rollout and a user allowlist.

package main

import (
	"context"
	"fmt"

	"github.com/rahmadafandi/fibr/featureflag"
)

func main() {
	f := featureflag.New(featureflag.Rules(map[string]featureflag.Rule{
		"new_checkout": {Percentage: 50, Users: []string{"vip"}},
	}))
	ctx := context.Background()

	// An allowlisted user is always in.
	fmt.Println("vip:", f.Enabled(ctx, "new_checkout", featureflag.Eval{UserID: "vip"}))
	// An unknown flag is off.
	fmt.Println("unknown:", f.Enabled(ctx, "nope", featureflag.Eval{UserID: "vip"}))
}
Output:
vip: true
unknown: false

func (*Flags) Middleware

func (f *Flags) Middleware(extract func(*fiber.Ctx) Eval) fiber.Handler

Middleware stores the Flags and a per-request Eval (derived by extract) in the request locals, so handlers can call Enabled(c, flag). extract maps the request (auth, headers, ...) to an Eval; a nil extract uses an anonymous Eval.

type Provider

type Provider interface {
	Enabled(ctx context.Context, flag string, e Eval) bool
}

Provider decides whether a flag is enabled for a given evaluation.

func Redis

func Redis(client redis.UniversalClient, prefix string) Provider

Redis returns a provider that reads each flag from key prefix+flag. The value is "true"/"false" or a JSON Rule. A missing key or any error is off. This allows toggling flags live without a redeploy.

func Rules

func Rules(m map[string]Rule) Provider

Rules returns a provider backed by per-flag Rules. Unknown flags are off.

func Static

func Static(m map[string]bool) Provider

Static returns a provider backed by a fixed on/off map. Unknown flags are off.

type Rule

type Rule struct {
	Enabled    bool     `json:"enabled"`    // fallback when no targeting matches
	Percentage int      `json:"percentage"` // 0-100 rollout, by stable hash(flag+UserID)
	Users      []string `json:"users"`      // allowlist (forces on)
	Groups     []string `json:"groups"`     // allowlist (forces on)
}

Rule targets a single flag.

Jump to

Keyboard shortcuts

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