Skip to content

Commit 3232d34

Browse files
committed
feat: 优化服务超时时间(临时处理)
1 parent 245fd96 commit 3232d34

File tree

3 files changed

+11
-38
lines changed

3 files changed

+11
-38
lines changed

cmd/gpt-load/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ func main() {
6161
server := &http.Server{
6262
Addr: fmt.Sprintf("%s:%d", serverConfig.Host, serverConfig.Port),
6363
Handler: router,
64-
ReadTimeout: 60 * time.Second, // Increased read timeout for large file uploads
65-
WriteTimeout: 300 * time.Second, // Increased write timeout for streaming responses
64+
ReadTimeout: 120 * time.Second, // Increased read timeout for large file uploads
65+
WriteTimeout: 1800 * time.Second, // Increased write timeout for streaming responses
6666
IdleTimeout: 120 * time.Second, // Increased idle timeout for connection reuse
6767
MaxHeaderBytes: 1 << 20, // 1MB header limit
6868
}

internal/middleware/middleware.go

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
package middleware
33

44
import (
5-
"context"
65
"fmt"
76
"strings"
87
"time"
@@ -215,32 +214,6 @@ func RateLimiter(config types.PerformanceConfig) gin.HandlerFunc {
215214
}
216215
}
217216

218-
// Timeout creates a timeout middleware
219-
func Timeout(timeout time.Duration) gin.HandlerFunc {
220-
return func(c *gin.Context) {
221-
acceptHeader := c.Request.Header.Get("Accept")
222-
if strings.Contains(acceptHeader, "text/event-stream") ||
223-
strings.Contains(acceptHeader, "application/x-ndjson") ||
224-
c.Request.Header.Get("X-Accel-Buffering") == "no" {
225-
c.Next()
226-
return
227-
}
228-
ctx, cancel := context.WithTimeout(c.Request.Context(), timeout)
229-
defer cancel()
230-
231-
c.Request = c.Request.WithContext(ctx)
232-
c.Next()
233-
234-
if ctx.Err() == context.DeadlineExceeded {
235-
c.JSON(408, gin.H{
236-
"error": "Request timeout",
237-
"code": errors.ErrProxyTimeout,
238-
})
239-
c.Abort()
240-
}
241-
}
242-
}
243-
244217
// ErrorHandler creates an error handling middleware
245218
func ErrorHandler() gin.HandlerFunc {
246219
return func(c *gin.Context) {

internal/proxy/server.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ func NewProxyServer(keyManager types.KeyManager, configManager types.ConfigManag
5656

5757
// Create high-performance HTTP client
5858
transport := &http.Transport{
59-
MaxIdleConns: 50,
60-
MaxIdleConnsPerHost: 10,
61-
MaxConnsPerHost: 0, // No limit to avoid connection pool bottleneck
62-
IdleConnTimeout: 90 * time.Second,
63-
TLSHandshakeTimeout: 10 * time.Second,
59+
MaxIdleConns: 100,
60+
MaxIdleConnsPerHost: 20,
61+
MaxConnsPerHost: 0,
62+
IdleConnTimeout: 120 * time.Second,
63+
TLSHandshakeTimeout: 30 * time.Second,
6464
ExpectContinueTimeout: 1 * time.Second,
6565
DisableCompression: !perfConfig.EnableGzip,
6666
ForceAttemptHTTP2: true,
@@ -70,17 +70,17 @@ func NewProxyServer(keyManager types.KeyManager, configManager types.ConfigManag
7070

7171
// Create dedicated transport for streaming, optimize TCP parameters
7272
streamTransport := &http.Transport{
73-
MaxIdleConns: 100,
74-
MaxIdleConnsPerHost: 20,
73+
MaxIdleConns: 200,
74+
MaxIdleConnsPerHost: 40,
7575
MaxConnsPerHost: 0,
7676
IdleConnTimeout: 300 * time.Second, // Keep streaming connections longer
77-
TLSHandshakeTimeout: 10 * time.Second,
77+
TLSHandshakeTimeout: 30 * time.Second,
7878
ExpectContinueTimeout: 1 * time.Second,
7979
DisableCompression: true, // Always disable compression for streaming
8080
ForceAttemptHTTP2: true,
8181
WriteBufferSize: 64 * 1024,
8282
ReadBufferSize: 64 * 1024,
83-
ResponseHeaderTimeout: 10 * time.Second,
83+
ResponseHeaderTimeout: 30 * time.Second,
8484
}
8585

8686
httpClient := &http.Client{

0 commit comments

Comments
 (0)