style(backend): 清理幂等写入路径的注释

注释只保留一行级约束。
长段理由移入设计文档。
验证:go test -count=1 ./... 全绿。
This commit is contained in:
2026-10-31 00:03:53 +08:00
parent c0ab69118a
commit c210e75996
13 changed files with 50 additions and 239 deletions

View File

@@ -10,20 +10,13 @@ import (
)
const (
// MaxIntentKeyLen 与 comments.idempotency_key VARCHAR(64) 保持一致。
// MaxIntentKeyLen 与 comments.idempotency_key 的列宽一致。
MaxIntentKeyLen = 64
// minIntentKeyLen 防止客户端传 "1"、"a" 这类极易碰撞的键。
minIntentKeyLen = 8
// intentKeyAlphabet 刻意排除 ':':通知去重键用 ':' 作分隔符
// ("comment:<K>:12"),若 K 含 ':' 则键的边界不可解析。
// intentKeyAlphabet 不含 ':',它是通知去重键的分隔符。
intentKeyAlphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
)
// NewIntentKey 生成 32 字符的随机意图键(16 字节 hex)。
//
// 为什么不用 uuid 库:项目当前没有该依赖,crypto/rand + hex 已足够;
// rabbitmq 包里的 newEventID 是同一模式,但它在包内私有,跨包复用
// 会把 video 拖进 rabbitmq → config 的依赖链。
func NewIntentKey() (string, error) {
b := make([]byte, 16)
if _, err := rand.Read(b); err != nil {
@@ -32,13 +25,6 @@ func NewIntentKey() (string, error) {
return hex.EncodeToString(b), nil
}
// ResolveIntentKey 把客户端传来的 request_id 解析成意图键。
//
// nil 或空白 → 生成新键(旧客户端,每次请求都是新意图,行为与今天一致);
// 合法 → 原样返回(客户端重试携带同一个值 → 命中同一次意图);
// 非法 → 返回错误,【不静默截断/不静默替换】。
//
// 静默替换会让"客户端以为去重了、服务端其实没有",比重试产生重复更难排查。
func ResolveIntentKey(raw *string) (string, error) {
if raw == nil || strings.TrimSpace(*raw) == "" {
return NewIntentKey()
@@ -50,9 +36,7 @@ func ResolveIntentKey(raw *string) (string, error) {
return key, nil
}
// validateIntentKey 的错误一律包 apierror.ErrValidation:request_id 写错
// 是客户端的错,必须落 400。裸 error 会走 apierror.ClassifyHTTPStatus 的
// default 分支变成 500,把一次参数错误显示成服务端故障。
// validateIntentKey 的错误必须包 apierror.ErrValidation,否则会落 500。
func validateIntentKey(key string) error {
if len(key) < minIntentKeyLen || len(key) > MaxIntentKeyLen {
return fmt.Errorf("%w: request_id must be %d-%d characters",
@@ -67,10 +51,7 @@ func validateIntentKey(key string) error {
return nil
}
// NotificationDedupeKey 构造通知表的去重键。
//
// intentKey 为空时返回空串:空串不写入唯一索引列(该列可空),
// 因此"没有意图键的事件"退化为不去重,而不是全部挤在同一个键上。
// NotificationDedupeKey 返回空串表示"这条通知不参与去重"。
func NotificationDedupeKey(notifType, intentKey string, recipientID uint) string {
if intentKey == "" {
return ""