Documentation
¶
Overview ¶
Package flags is a feature-flag + A/B-testing plugin for togo (Laravel Pennant / Flipper / django-waffle for Go).
Define flags with a master on/off, a percentage rollout, and targeting rules; evaluate them per subject with deterministic bucketing (a subject stays in the same bucket across calls). Flags with variants drive A/B experiments.
s, _ := flags.FromKernel(k)
s.Set(flags.Flag{Key: "new-checkout", Enabled: true, Rollout: 25})
if s.Enabled(ctx, "new-checkout", flags.Subject{ID: user.ID}) { ... }
Index ¶
- type Flag
- type Rule
- type Service
- func (s *Service) All() []Flag
- func (s *Service) Delete(key string)
- func (s *Service) Enabled(ctx context.Context, key string, subj Subject) bool
- func (s *Service) Get(key string) (Flag, bool)
- func (s *Service) Middleware(key string, subjectFn func(*http.Request) Subject) func(http.Handler) http.Handler
- func (s *Service) Results(key string) map[string]int64
- func (s *Service) Set(f Flag)
- func (s *Service) Variant(ctx context.Context, key string, subj Subject) string
- func (s *Service) WithStore(store Store) *Service
- type Store
- type Subject
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Flag ¶
type Flag struct {
Key string `json:"key"`
Enabled bool `json:"enabled"` // master on/off
Rollout int `json:"rollout"` // 0-100 percentage when enabled
Rules []Rule `json:"rules,omitempty"` // targeting (all must match)
Variants []string `json:"variants,omitempty"` // A/B variants (deterministic)
Description string `json:"description,omitempty"`
}
Flag is a feature-flag definition.
type Rule ¶
Rule is a targeting rule: the subject's Attribute must equal one of Values. A flag matches only when ALL its rules match.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service is the flags runtime stored on the kernel (k.Get("flags")).
func FromKernel ¶
FromKernel returns the flags Service registered on the kernel.
func (*Service) Enabled ¶
Enabled evaluates a flag for a subject. Deterministic: the same (key,subject) always yields the same bucket, so a partial rollout is stable per subject.
func (*Service) Middleware ¶
func (s *Service) Middleware(key string, subjectFn func(*http.Request) Subject) func(http.Handler) http.Handler
Middleware gates a handler behind a flag. subjectFn extracts the Subject from the request (e.g. the authenticated user); when the flag is off it responds 404. Use to dark-launch routes.
func (*Service) Results ¶
Results returns the recorded evaluation counts for a flag (result -> count), e.g. {"true": 120, "false": 380} or per-variant counts for A/B experiments.
func (*Service) Set ¶
Set defines or replaces a flag. An enabled flag with Rollout==0 is treated as 100% (fully on); set Rollout explicitly for a partial rollout.