From 370a21760c67446120ea5a839f863664010c2f39 Mon Sep 17 00:00:00 2001 From: "3371392206@qq.com" <3371392206@qq.com> Date: Sun, 16 Aug 2026 18:50:33 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E9=99=90=E6=B5=81=E5=92=8C=E5=B9=B6?= =?UTF-8?q?=E5=8F=91=E4=B8=AD=E9=97=B4=E4=BB=B6=E8=B7=B3=E8=BF=87=E9=9D=9E?= =?UTF-8?q?=20/v1/=20=E8=B7=AF=E7=94=B1,=E9=81=BF=E5=85=8D=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E8=B7=AF=E5=BE=84=E6=B1=A1=E6=9F=93=E8=AE=A1=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- middleware/ratelimit_instrumented.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/middleware/ratelimit_instrumented.go b/middleware/ratelimit_instrumented.go index ec988ec..a9e05ef 100644 --- a/middleware/ratelimit_instrumented.go +++ b/middleware/ratelimit_instrumented.go @@ -9,6 +9,7 @@ package middleware import ( "log" "net/http" + "strings" "github.com/volcano-tts/tts-api/metrics" ) @@ -16,6 +17,11 @@ import ( // RateLimitWithMetrics 是 middleware.RateLimit 的可埋点版本。 func RateLimitWithMetrics(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // 仅对 /v1/ 下的业务请求限流,/health /metrics /dashboard 等监控路径不限流 + if !strings.HasPrefix(r.URL.Path, "/v1/") { + next.ServeHTTP(w, r) + return + } clientIP := GetClientIP(r) if !GlobalRateLimiter.Allow(clientIP) { log.Printf("警告: 已超过IP速率限制,拒绝请求 - 客户端IP: %s", clientIP) @@ -29,6 +35,11 @@ func RateLimitWithMetrics(next http.Handler) http.Handler { // ConcurrencyLimitWithMetrics 是 middleware.ConcurrencyLimit 的可埋点版本。 func ConcurrencyLimitWithMetrics(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // 仅对 /v1/ 下的业务请求统计并发和加锁,监控路径不占用并发槽位 + if !strings.HasPrefix(r.URL.Path, "/v1/") { + next.ServeHTTP(w, r) + return + } select { case ConcurrencySem <- struct{}{}: metrics.ConcurrencyActive.Inc(nil)