Files
smartmate/backend/middleware/idempotency.go
2026-05-04 20:38:49 +08:00

110 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package middleware
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"strings"
"time"
"github.com/LoveLosita/smartflow/backend/dao"
"github.com/LoveLosita/smartflow/backend/respond"
"github.com/gin-gonic/gin"
)
type IdempotencyValue struct {
Status int `json:"status"` // HTTP 状态码
Body string `json:"body"` // JSON 响应体
}
type responseRecorder struct {
gin.ResponseWriter
body *bytes.Buffer
}
func (r *responseRecorder) Write(b []byte) (int, error) {
r.body.Write(b) // 录制到缓冲区
return r.ResponseWriter.Write(b) // 正常发送给前端
}
func IdempotencyMiddleware(cache *dao.CacheDAO) gin.HandlerFunc {
return func(c *gin.Context) {
// 1. 获取 IKey
ikey := c.GetHeader("X-Idempotency-Key")
if ikey == "" {
c.JSON(http.StatusBadRequest, respond.MissingIdempotencyKey) // 400 错误,缺少 IKey
c.Abort()
return
}
userID := c.GetInt("user_id") // 假设 JWT 已存入
routeKey := idempotencyRouteKey(c)
redisKey := fmt.Sprintf("idempotency:%d:%s:%s:%s", userID, c.Request.Method, routeKey, ikey)
// 2. 查 Redis 缓存
cachedData, err := cache.GetRecord(c, redisKey)
if err != nil { // 💡 Fail-OpenRedis 挂了也别卡住用户,记个日志继续走
log.Printf("[Idempotency] Redis Get error: %v", err)
} else if cachedData != "" {
// 命中缓存,直接回放录像
var val IdempotencyValue
json.Unmarshal([]byte(cachedData), &val)
c.Data(val.Status, "application/json", []byte(val.Body))
c.Abort()
return
}
// 3. 分布式锁:防止微秒级的并发碰撞 (SetNX)
// 锁 10 秒,防止请求卡死导致 key 永久锁定
lockKey := redisKey + ":lock"
success, err := cache.AcquireLock(c, lockKey, 10*time.Second)
if err != nil { // 如果加锁报错,为了保险我们依然放行,让底层的数据库唯一索引去兜底
log.Printf("[Idempotency] Redis Lock error: %v", err)
} else if !success {
c.JSON(http.StatusConflict, respond.RequestIsProcessing)
c.Abort()
return
}
// 💡 只有在加锁成功时才需要 defer 删锁
if err == nil && success {
defer cache.ReleaseLock(c, lockKey)
}
// 4. 装饰 ResponseWriter 开始录制
recorder := &responseRecorder{
ResponseWriter: c.Writer,
body: bytes.NewBufferString(""),
}
c.Writer = recorder
// 5. 执行后续 Handler (你的 Service 逻辑)
c.Next()
// 6. 录制完成,存入 Redis (缓存 24 小时)
// 只有状态码 < 500 时才存入 Redis这样如果是服务器临时抽风用户重试依然有机会成功
if c.Writer.Status() < 500 {
respVal := IdempotencyValue{
Status: c.Writer.Status(),
Body: recorder.body.String(),
}
data, _ := json.Marshal(respVal)
if err := cache.SaveRecord(c, redisKey, string(data), 24*time.Hour); err != nil {
log.Printf("[Idempotency] Redis Save error: %v", err)
}
}
}
}
func idempotencyRouteKey(c *gin.Context) string {
// 1. 优先使用 Gin 匹配后的路由模板,避免 /posts/1 和 /posts/2 被当成两个幂等域。
// 2. 若当前上下文还拿不到模板,则退回请求路径,保证异常情况下仍不会跨接口串响应。
// 3. 路由 key 统一替换冒号,避免 Redis key 中混入过多分隔符影响人工排查。
route := strings.TrimSpace(c.FullPath())
if route == "" && c.Request != nil && c.Request.URL != nil {
route = strings.TrimSpace(c.Request.URL.Path)
}
return strings.ReplaceAll(route, ":", "_")
}