grayscale

package
v1.79.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package grayscale 提供灰度路由:按比例或按用户稳定分流新旧版本处理器。 场景:新接口灰度发版(5% → 20% → 50% → 100%)、A/B 测试。 支持灰度可观测:响应头标记命中版本(X-Gray-Version)与命中统计(Stats)。

Index

Constants

View Source
const DefaultHeaderName = "X-Gray-Version"

默认灰度标记响应头。

Variables

This section is empty.

Functions

This section is empty.

Types

type Stats added in v1.76.0

type Stats struct {
	Total   uint64  // 总请求数
	NewHits uint64  // 命中新版本数
	OldHits uint64  // 命中旧版本数
	Ratio   float64 // 实际新版本占比(0~1;无请求时为 0)
}

Stats 灰度统计快照。

type Strategy

type Strategy struct {
	// Ratio 命中新版本的比例(0~1);1 = 全量新版本,0 = 全量旧版本。
	Ratio float64
	// UserKey 提取用户标识的函数(nil = 按请求随机分流)。
	// 提供后同一用户始终命中同一版本(稳定灰度)。
	UserKey func(ctx iris.Context) string
	// HeaderName 响应头标记名("" 关闭标记;默认 X-Gray-Version)。
	HeaderName string
	// contains filtered or unexported fields
}

Strategy 灰度策略。

func New

func New(ratio float64, userKey ...func(ctx iris.Context) string) *Strategy

New 创建灰度策略。userKey 可选(传入则用户稳定分流)。

func (*Strategy) Hit

func (s *Strategy) Hit(ctx iris.Context) bool

Hit 判断当前请求是否命中新版本(不计入统计;Route 内部统计)。

func (*Strategy) Route

func (s *Strategy) Route(newHandler, oldHandler iris.Handler) iris.Handler

Route 返回分流处理器:命中走 newHandler,否则走 oldHandler。 响应头标记命中版本(X-Gray-Version: new|old,可 WithHeaderName 关闭),并计入统计。 用法:app.Get("/api/order", gray.Route(newOrderHandler, oldOrderHandler))

func (*Strategy) Stats added in v1.76.0

func (s *Strategy) Stats() Stats

Stats 返回灰度命中统计(原子读取)。

func (*Strategy) WithHeaderName added in v1.76.0

func (s *Strategy) WithHeaderName(name string) *Strategy

WithHeaderName 设置响应头标记名("" 关闭标记)。链式调用。

Jump to

Keyboard shortcuts

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