featureflag

package
v0.8.6 Latest Latest
Warning

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

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package featureflag 提供本地评估的特性开关与 A/B 实验: 布尔开关、百分比灰度、按属性定向、多变体实验,均基于确定性哈希分桶, 同一标识(如 userID)多次评估结果稳定,无需远程调用。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Attributes

type Attributes map[string]any

Attributes 是用于定向匹配的标识属性(如 {"country":"CN","plan":"pro"})。

type Engine

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

Engine 持有开关与实验配置,提供线程安全的评估。

func New

func New() *Engine

New 创建一个空 Engine。

func (*Engine) IsEnabled

func (e *Engine) IsEnabled(key, id string, attrs Attributes) bool

IsEnabled 评估某标识 id 是否命中开关 key。attrs 可为 nil。 评估顺序:定向规则(首个命中)→ 总开关 → 百分比灰度(按 id 稳定分桶)。

func (*Engine) SetExperiment

func (e *Engine) SetExperiment(x Experiment)

SetExperiment 注册/更新一个实验。

func (*Engine) SetFlag

func (e *Engine) SetFlag(f Flag)

SetFlag 注册/更新一个开关。

func (*Engine) Variant

func (e *Engine) Variant(key, id string) (string, bool)

Variant 评估某标识 id 在实验 key 中的变体。 返回 (变体名, true);未参与实验(超出 coverage)或实验不存在时返回 ("", false), 调用方应回退到对照/默认行为。

type Experiment

type Experiment struct {
	Key      string
	Variants []string  // 变体名,如 ["control","treatment"]
	Weights  []float64 // 各变体权重,缺省或非法时等分;和应约等于 1
	Coverage float64   // 0~1 参与实验的比例,0 表示用默认 1
}

Experiment 是一个 A/B(/n) 实验定义。

type Flag

type Flag struct {
	Key     string
	Enabled bool    // 总开关;false 时一律关闭(除非被 Rule 命中强制开)
	Rollout float64 // 0~1 灰度比例,Enabled 且未命中规则时按此比例放量;0 表示用默认 1
	Rules   []Rule  // 定向规则,按顺序首个命中生效
}

Flag 是一个特性开关定义。

type Rule

type Rule struct {
	When  map[string]any // 属性等值匹配,全部满足才算命中
	Force bool           // 命中后强制开/关
}

Rule 是一条定向规则:When 全部命中时,强制结果为 Force。

Jump to

Keyboard shortcuts

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