Merge branch 'main' into feature/distribution-system

This commit is contained in:
yml2213
2026-09-11 09:35:53 +08:00
14 changed files with 75 additions and 13 deletions
+11 -4
View File
@@ -1,6 +1,8 @@
package middleware
import (
"context"
"errors"
"net/http"
"strings"
"time"
@@ -72,6 +74,10 @@ func RequestLogger(logger *zap.Logger) gin.HandlerFunc {
}
switch {
case errors.Is(c.Request.Context().Err(), context.Canceled):
if logger.Core().Enabled(zap.DebugLevel) {
logger.Debug("HTTP 客户端中断连接", fields...)
}
case status >= 500:
logger.Error("HTTP 请求失败", fields...)
case status == http.StatusTooManyRequests:
@@ -113,7 +119,7 @@ func shouldSkipHTTPLog(path, route string, status int, latencyMs float64) bool {
return false
}
// SSE 的请求耗时就是连接存活时间,由 chathub 单独记录连接生命周期。
if isChatSSEPath(path, route) {
if isSSEPath(path, route) {
return true
}
if latencyMs >= slowRequestThresholdMs {
@@ -122,9 +128,10 @@ func shouldSkipHTTPLog(path, route string, status int, latencyMs float64) bool {
return true
}
func isChatSSEPath(path, route string) bool {
return path == "/api/chats/events" || path == "/api/admin/chats/events" ||
route == "/api/chats/events" || route == "/api/admin/chats/events"
func isSSEPath(path, route string) bool {
return path == "/api/auth/events" || route == "/api/auth/events" ||
path == "/api/chats/events" || route == "/api/chats/events" ||
path == "/api/admin/chats/events" || route == "/api/admin/chats/events"
}
func meaningfulAuthFailure(c *gin.Context) bool {
@@ -19,6 +19,7 @@ func TestShouldSkipHTTPLog(t *testing.T) {
{name: "轮询 500 不跳过", path: "/api/wallet/balance", status: 500, latencyMs: 1, wantSkip: false},
{name: "限流请求不跳过", path: "/api/auth/sms", status: 429, latencyMs: 1, wantSkip: false},
{name: "慢请求不跳过", path: "/api/orders", status: 200, latencyMs: 500, wantSkip: false},
{name: "用户全局通知 SSE 长连接跳过", path: "/api/auth/events", route: "/api/auth/events", status: 200, latencyMs: 60_000, wantSkip: true},
{name: "用户 SSE 长连接跳过", path: "/api/chats/events", route: "/api/chats/events", status: 200, latencyMs: 60_000, wantSkip: true},
{name: "后台 SSE 长连接跳过", path: "/api/admin/chats/events", route: "/api/admin/chats/events", status: 200, latencyMs: 600_000, wantSkip: true},
{name: "SSE 服务端错误不跳过", path: "/api/admin/chats/events", route: "/api/admin/chats/events", status: 500, latencyMs: 600_000, wantSkip: false},