Files
a301_server/pkg/middleware/idempotency.go
tolelom b006fe77c2
All checks were successful
Server CI/CD / lint-and-build (push) Successful in 38s
Server CI/CD / deploy (push) Successful in 50s
fix: API 서버 코드 리뷰 버그 15건 수정 (CRITICAL 2, HIGH 2, MEDIUM 11)
CRITICAL:
- graceful shutdown 레이스 수정 — Listen을 goroutine으로 이동
- Register 레이스 컨디션 — sentinel error + MySQL duplicate key 처리

HIGH:
- 멱등성 키에 method+path 포함 — 엔드포인트 간 캐시 충돌 방지
- 입장 토큰 생성 실패 시 방/슬롯 롤백 추가

MEDIUM:
- RequestEntry 슬롯 없음 시 503 반환
- chain ExportWallet/GetWalletInfo/GrantReward 에러 처리 개선
- resolveUsername 에러 타입 구분 (duplicate key vs 기타)
- 공지사항 길이 검증 byte→rune (한국어 256자 허용)
- Level 검증 범위 MaxLevel(50)로 통일
- admin 자기 강등 방지
- CORS ExposeHeaders 추가
- MySQL DSN loc=Local→loc=UTC
- hashGameExeFromZip 100MB 초과 절단 감지

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-03-23 18:05:27 +09:00

120 lines
4.0 KiB
Go

package middleware
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"a301_server/pkg/apperror"
"github.com/gofiber/fiber/v2"
"github.com/redis/go-redis/v9"
)
const idempotencyTTL = 10 * time.Minute
const redisTimeout = 5 * time.Second
type cachedResponse struct {
StatusCode int `json:"s"`
Body json.RawMessage `json:"b"`
}
// IdempotencyRequired returns a middleware that rejects requests without an Idempotency-Key header,
// then delegates to idempotency cache/replay logic.
func IdempotencyRequired(rdb *redis.Client) fiber.Handler {
idempotency := Idempotency(rdb)
return func(c *fiber.Ctx) error {
if c.Get("Idempotency-Key") == "" {
return apperror.BadRequest("Idempotency-Key 헤더가 필요합니다")
}
return idempotency(c)
}
}
// Idempotency returns a middleware that checks the Idempotency-Key header to prevent duplicate transactions.
// If the same key is seen again within the TTL, the cached response is returned.
func Idempotency(rdb *redis.Client) fiber.Handler {
return func(c *fiber.Ctx) error {
key := c.Get("Idempotency-Key")
if key == "" {
return c.Next()
}
if len(key) > 256 {
return apperror.BadRequest("Idempotency-Key가 너무 깁니다")
}
// userID가 있으면 키에 포함하여 사용자 간 캐시 충돌 방지
redisKey := "idempotency:"
if uid, ok := c.Locals("userID").(uint); ok {
redisKey += fmt.Sprintf("u%d:", uid)
}
redisKey += c.Method() + ":" + c.Route().Path + ":" + key
ctx, cancel := context.WithTimeout(context.Background(), redisTimeout)
defer cancel()
// Atomically claim the key using SET NX (only succeeds if key doesn't exist)
set, err := rdb.SetNX(ctx, redisKey, "processing", idempotencyTTL).Result()
if err != nil {
// Redis error — reject to prevent duplicate transactions
log.Printf("ERROR: idempotency SetNX failed (key=%s): %v", key, err)
return apperror.New("internal_error", "서버 오류가 발생했습니다. 잠시 후 다시 시도해주세요", 503)
}
if !set {
// Key already exists — either processing or completed
getCtx, getCancel := context.WithTimeout(context.Background(), redisTimeout)
defer getCancel()
cached, err := rdb.Get(getCtx, redisKey).Bytes()
if err != nil {
return apperror.Conflict("요청이 처리 중입니다")
}
if string(cached) == "processing" {
return apperror.Conflict("요청이 처리 중입니다")
}
var cr cachedResponse
if json.Unmarshal(cached, &cr) == nil {
c.Set("Content-Type", "application/json")
c.Set("X-Idempotent-Replay", "true")
return c.Status(cr.StatusCode).Send(cr.Body)
}
return apperror.Conflict("요청이 처리 중입니다")
}
// We claimed the key — process the request
if err := c.Next(); err != nil {
// Processing failed — remove the key so it can be retried
delCtx, delCancel := context.WithTimeout(context.Background(), redisTimeout)
defer delCancel()
if delErr := rdb.Del(delCtx, redisKey).Err(); delErr != nil {
log.Printf("WARNING: idempotency cache delete failed (key=%s): %v", key, delErr)
}
return err
}
// Cache successful responses (2xx), otherwise remove the key for retry
status := c.Response().StatusCode()
if status >= 200 && status < 300 {
cr := cachedResponse{StatusCode: status, Body: c.Response().Body()}
if data, err := json.Marshal(cr); err == nil {
writeCtx, writeCancel := context.WithTimeout(context.Background(), redisTimeout)
defer writeCancel()
if err := rdb.Set(writeCtx, redisKey, data, idempotencyTTL).Err(); err != nil {
log.Printf("WARNING: idempotency cache write failed (key=%s): %v", key, err)
}
}
} else {
// Non-success — allow retry by removing the key
delCtx, delCancel := context.WithTimeout(context.Background(), redisTimeout)
defer delCancel()
if delErr := rdb.Del(delCtx, redisKey).Err(); delErr != nil {
log.Printf("WARNING: idempotency cache delete failed (key=%s): %v", key, delErr)
}
}
return nil
}
}