refactor: deprecate int32 (#7025)
* refactor: deprecate int32 * fix(db): reject legacy user quota schemas at startup * fix(quota): enforce wallet bounds and saturating billing conversions * fix(rate-limit): keep count*duration from wrapping int64 * fix: error message
This commit is contained in:
@@ -3,6 +3,7 @@ package middleware
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"math"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
@@ -103,7 +104,7 @@ func redisRateLimitHandler(duration int64, totalMaxCount, successMaxCount int) g
|
||||
allowed, err = tb.Allow(
|
||||
ctx,
|
||||
totalKey,
|
||||
limiter.WithCapacity(int64(totalMaxCount)*duration),
|
||||
limiter.WithCapacity(rateLimitCapacity(totalMaxCount, duration)),
|
||||
limiter.WithRate(int64(totalMaxCount)),
|
||||
limiter.WithRequested(duration),
|
||||
)
|
||||
@@ -174,7 +175,7 @@ func ModelRequestRateLimit() func(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 计算限流参数
|
||||
duration := int64(setting.ModelRequestRateLimitDurationMinutes * 60)
|
||||
duration := rateLimitDurationSeconds(setting.ModelRequestRateLimitDurationMinutes)
|
||||
totalMaxCount := setting.ModelRequestRateLimitCount
|
||||
successMaxCount := setting.ModelRequestRateLimitSuccessCount
|
||||
|
||||
@@ -199,3 +200,25 @@ func ModelRequestRateLimit() func(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func rateLimitDurationSeconds(durationMinutes int) int64 {
|
||||
if durationMinutes <= 0 {
|
||||
return 0
|
||||
}
|
||||
minutes := int64(durationMinutes)
|
||||
if minutes > math.MaxInt64/60 {
|
||||
return math.MaxInt64
|
||||
}
|
||||
return minutes * 60
|
||||
}
|
||||
|
||||
func rateLimitCapacity(count int, durationSeconds int64) int64 {
|
||||
if count <= 0 || durationSeconds <= 0 {
|
||||
return 0
|
||||
}
|
||||
c := int64(count)
|
||||
if c > math.MaxInt64/durationSeconds {
|
||||
return math.MaxInt64
|
||||
}
|
||||
return c * durationSeconds
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user