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 ¶
Types ¶
type Flags ¶
type Flags struct {
// contains filtered or unexported fields
}
Flags is the facade over a Provider.
func (*Flags) Enabled ¶
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 ¶
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 ¶
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.
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.