build: 升级go版本到1.26并添加限流中间件

1.  调整go.mod将Go版本升级至1.26
2.  新增速率限制和并发限制中间件,将其加入路由中间件链
3.  重构TTS处理逻辑,将限流逻辑迁移至中间件统一处理
4.  优化CORS中间件代码,移除冗余日志和格式调整
This commit is contained in:
sun
2026-06-22 18:13:35 +08:00
parent b92d3dbc00
commit 977e9ccadb
5 changed files with 48 additions and 35 deletions
+1 -17
View File
@@ -39,23 +39,7 @@ func OpenaiTTSHandler(w http.ResponseWriter, r *http.Request) {
return
}
select {
case middleware.ConcurrencySem <- struct{}{}:
defer func() { <-middleware.ConcurrencySem }()
default:
log.Printf("警告: 已达到最大并发请求数限制,拒绝请求 - 客户端IP: %s", middleware.GetClientIP(r))
middleware.SendJSONError(w, http.StatusServiceUnavailable, "Server is busy, maximum concurrent requests reached. Please try again later.", "concurrency_limit_error", "max_concurrent_requests")
return
}
clientIP := middleware.GetClientIP(r)
if !middleware.GlobalRateLimiter.Allow(clientIP) {
log.Printf("警告: 已超过IP速率限制,拒绝请求 - 客户端IP: %s", clientIP)
middleware.SendJSONError(w, http.StatusTooManyRequests, "Rate limit exceeded. Please try again later.", "rate_limit_error", "rate_limit_exceeded")
return
}
r.Body = http.MaxBytesReader(w, r.Body, common.MaxRequestBodySize)
r.Body = http.MaxBytesReader(w, r.Body, common.MaxRequestBodySize)
body, err := io.ReadAll(r.Body)
if err != nil {
if strings.Contains(err.Error(), "request body too large") {