Files
lijiaoqiao/gateway/internal/router/strategy/rollout.go
Your Name 89104bd0db feat(P1/P2): 完成TDD开发及P1/P2设计文档
## 设计文档
- multi_role_permission_design: 多角色权限设计 (CONDITIONAL GO)
- audit_log_enhancement_design: 审计日志增强 (CONDITIONAL GO)
- routing_strategy_template_design: 路由策略模板 (CONDITIONAL GO)
- sso_saml_technical_research: SSO/SAML调研 (CONDITIONAL GO)
- compliance_capability_package_design: 合规能力包设计 (CONDITIONAL GO)

## TDD开发成果
- IAM模块: supply-api/internal/iam/ (111个测试)
- 审计日志模块: supply-api/internal/audit/ (40+测试)
- 路由策略模块: gateway/internal/router/ (33+测试)
- 合规能力包: gateway/internal/compliance/ + scripts/ci/compliance/

## 规范文档
- parallel_agent_output_quality_standards: 并行Agent产出质量规范
- project_experience_summary: 项目经验总结 (v2)
- 2026-04-02-p1-p2-tdd-execution-plan: TDD执行计划

## 评审报告
- 5个CONDITIONAL GO设计文档评审报告
- fix_verification_report: 修复验证报告
- full_verification_report: 全面质量验证报告
- tdd_module_quality_verification: TDD模块质量验证
- tdd_execution_summary: TDD执行总结

依据: Superpowers执行框架 + TDD规范
2026-04-02 23:35:53 +08:00

79 lines
1.6 KiB
Go

package strategy
import (
"fmt"
"hash/fnv"
"sync"
)
// RolloutStrategy 灰度发布策略
type RolloutStrategy struct {
percentage int // 当前灰度百分比 (0-100)
bucketKey string // 分桶key
mu sync.RWMutex
}
// NewRolloutStrategy 创建灰度发布策略
func NewRolloutStrategy(percentage int, bucketKey string) *RolloutStrategy {
return &RolloutStrategy{
percentage: percentage,
bucketKey: bucketKey,
}
}
// SetPercentage 设置灰度百分比
func (r *RolloutStrategy) SetPercentage(percentage int) {
r.mu.Lock()
defer r.mu.Unlock()
if percentage < 0 {
percentage = 0
}
if percentage > 100 {
percentage = 100
}
r.percentage = percentage
}
// GetPercentage 获取当前灰度百分比
func (r *RolloutStrategy) GetPercentage() int {
r.mu.RLock()
defer r.mu.RUnlock()
return r.percentage
}
// ShouldApply 判断请求是否应该在灰度范围内
func (r *RolloutStrategy) ShouldApply(req *RoutingRequest) bool {
r.mu.RLock()
defer r.mu.RUnlock()
if r.percentage >= 100 {
return true
}
if r.percentage <= 0 {
return false
}
// 一致性哈希分桶
bucket := r.hashString(fmt.Sprintf("%s:%s", r.bucketKey, req.UserID)) % 100
return bucket < r.percentage
}
// hashString 计算字符串哈希值 (用于一致性分桶)
func (r *RolloutStrategy) hashString(s string) int {
h := fnv.New32a()
h.Write([]byte(s))
return int(h.Sum32())
}
// IncrementPercentage 增加灰度百分比
func (r *RolloutStrategy) IncrementPercentage(delta int) {
r.mu.Lock()
defer r.mu.Unlock()
r.percentage += delta
if r.percentage > 100 {
r.percentage = 100
}
}