feat: 优化服务超时时间(临时处理)

This commit is contained in:
tbphp
2025-06-13 10:20:13 +08:00
parent 245fd96790
commit 3232d34534
3 changed files with 11 additions and 38 deletions

View File

@@ -2,7 +2,6 @@
package middleware
import (
"context"
"fmt"
"strings"
"time"
@@ -215,32 +214,6 @@ func RateLimiter(config types.PerformanceConfig) gin.HandlerFunc {
}
}
// Timeout creates a timeout middleware
func Timeout(timeout time.Duration) gin.HandlerFunc {
return func(c *gin.Context) {
acceptHeader := c.Request.Header.Get("Accept")
if strings.Contains(acceptHeader, "text/event-stream") ||
strings.Contains(acceptHeader, "application/x-ndjson") ||
c.Request.Header.Get("X-Accel-Buffering") == "no" {
c.Next()
return
}
ctx, cancel := context.WithTimeout(c.Request.Context(), timeout)
defer cancel()
c.Request = c.Request.WithContext(ctx)
c.Next()
if ctx.Err() == context.DeadlineExceeded {
c.JSON(408, gin.H{
"error": "Request timeout",
"code": errors.ErrProxyTimeout,
})
c.Abort()
}
}
}
// ErrorHandler creates an error handling middleware
func ErrorHandler() gin.HandlerFunc {
return func(c *gin.Context) {

View File

@@ -56,11 +56,11 @@ func NewProxyServer(keyManager types.KeyManager, configManager types.ConfigManag
// Create high-performance HTTP client
transport := &http.Transport{
MaxIdleConns: 50,
MaxIdleConnsPerHost: 10,
MaxConnsPerHost: 0, // No limit to avoid connection pool bottleneck
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
MaxConnsPerHost: 0,
IdleConnTimeout: 120 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableCompression: !perfConfig.EnableGzip,
ForceAttemptHTTP2: true,
@@ -70,17 +70,17 @@ func NewProxyServer(keyManager types.KeyManager, configManager types.ConfigManag
// Create dedicated transport for streaming, optimize TCP parameters
streamTransport := &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
MaxIdleConns: 200,
MaxIdleConnsPerHost: 40,
MaxConnsPerHost: 0,
IdleConnTimeout: 300 * time.Second, // Keep streaming connections longer
TLSHandshakeTimeout: 10 * time.Second,
TLSHandshakeTimeout: 30 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
DisableCompression: true, // Always disable compression for streaming
ForceAttemptHTTP2: true,
WriteBufferSize: 64 * 1024,
ReadBufferSize: 64 * 1024,
ResponseHeaderTimeout: 10 * time.Second,
ResponseHeaderTimeout: 30 * time.Second,
}
httpClient := &http.Client{