feat: 优化超时配置
This commit is contained in:
@@ -30,8 +30,8 @@ type Constants struct {
|
||||
var DefaultConstants = Constants{
|
||||
MinPort: 1,
|
||||
MaxPort: 65535,
|
||||
MinTimeout: 1000,
|
||||
DefaultTimeout: 30000,
|
||||
MinTimeout: 1,
|
||||
DefaultTimeout: 30,
|
||||
DefaultMaxSockets: 50,
|
||||
DefaultMaxFreeSockets: 10,
|
||||
}
|
||||
@@ -62,8 +62,12 @@ func NewManager() (types.ConfigManager, error) {
|
||||
|
||||
config := &Config{
|
||||
Server: types.ServerConfig{
|
||||
Port: parseInteger(os.Getenv("PORT"), 3000),
|
||||
Host: getEnvOrDefault("HOST", "0.0.0.0"),
|
||||
Port: parseInteger(os.Getenv("PORT"), 3000),
|
||||
Host: getEnvOrDefault("HOST", "0.0.0.0"),
|
||||
ReadTimeout: parseInteger(os.Getenv("SERVER_READ_TIMEOUT"), 120),
|
||||
WriteTimeout: parseInteger(os.Getenv("SERVER_WRITE_TIMEOUT"), 1800),
|
||||
IdleTimeout: parseInteger(os.Getenv("SERVER_IDLE_TIMEOUT"), 120),
|
||||
GracefulShutdownTimeout: parseInteger(os.Getenv("SERVER_GRACEFUL_SHUTDOWN_TIMEOUT"), 60),
|
||||
},
|
||||
Keys: types.KeysConfig{
|
||||
FilePath: getEnvOrDefault("KEYS_FILE", "keys.txt"),
|
||||
@@ -72,8 +76,10 @@ func NewManager() (types.ConfigManager, error) {
|
||||
MaxRetries: parseInteger(os.Getenv("MAX_RETRIES"), 3),
|
||||
},
|
||||
OpenAI: types.OpenAIConfig{
|
||||
BaseURLs: parseArray(os.Getenv("OPENAI_BASE_URL"), []string{"https://api.openai.com"}),
|
||||
Timeout: parseInteger(os.Getenv("REQUEST_TIMEOUT"), DefaultConstants.DefaultTimeout),
|
||||
BaseURLs: parseArray(os.Getenv("OPENAI_BASE_URL"), []string{"https://api.openai.com"}),
|
||||
RequestTimeout: parseInteger(os.Getenv("REQUEST_TIMEOUT"), DefaultConstants.DefaultTimeout),
|
||||
ResponseTimeout: parseInteger(os.Getenv("RESPONSE_TIMEOUT"), 30),
|
||||
IdleConnTimeout: parseInteger(os.Getenv("IDLE_CONN_TIMEOUT"), 120),
|
||||
},
|
||||
Auth: types.AuthConfig{
|
||||
Key: os.Getenv("AUTH_KEY"),
|
||||
@@ -88,7 +94,6 @@ func NewManager() (types.ConfigManager, error) {
|
||||
},
|
||||
Performance: types.PerformanceConfig{
|
||||
MaxConcurrentRequests: parseInteger(os.Getenv("MAX_CONCURRENT_REQUESTS"), 100),
|
||||
RequestTimeout: parseInteger(os.Getenv("REQUEST_TIMEOUT"), DefaultConstants.DefaultTimeout),
|
||||
EnableGzip: parseBoolean(os.Getenv("ENABLE_GZIP"), true),
|
||||
},
|
||||
Log: types.LogConfig{
|
||||
@@ -173,8 +178,8 @@ func (m *Manager) Validate() error {
|
||||
}
|
||||
|
||||
// Validate timeout
|
||||
if m.config.OpenAI.Timeout < DefaultConstants.MinTimeout {
|
||||
validationErrors = append(validationErrors, fmt.Sprintf("request timeout cannot be less than %dms", DefaultConstants.MinTimeout))
|
||||
if m.config.OpenAI.RequestTimeout < DefaultConstants.MinTimeout {
|
||||
validationErrors = append(validationErrors, fmt.Sprintf("request timeout cannot be less than %ds", DefaultConstants.MinTimeout))
|
||||
}
|
||||
|
||||
// Validate upstream URL format
|
||||
@@ -212,7 +217,9 @@ func (m *Manager) DisplayConfig() {
|
||||
logrus.Infof(" Blacklist threshold: %d errors", m.config.Keys.BlacklistThreshold)
|
||||
logrus.Infof(" Max retries: %d", m.config.Keys.MaxRetries)
|
||||
logrus.Infof(" Upstream URLs: %s", strings.Join(m.config.OpenAI.BaseURLs, ", "))
|
||||
logrus.Infof(" Request timeout: %dms", m.config.OpenAI.Timeout)
|
||||
logrus.Infof(" Request timeout: %ds", m.config.OpenAI.RequestTimeout)
|
||||
logrus.Infof(" Response timeout: %ds", m.config.OpenAI.ResponseTimeout)
|
||||
logrus.Infof(" Idle connection timeout: %ds", m.config.OpenAI.IdleConnTimeout)
|
||||
|
||||
authStatus := "disabled"
|
||||
if m.config.Auth.Enabled {
|
||||
|
@@ -176,8 +176,10 @@ func (h *Handler) GetConfig(c *gin.Context) {
|
||||
"max_retries": keysConfig.MaxRetries,
|
||||
},
|
||||
"openai": gin.H{
|
||||
"base_url": openaiConfig.BaseURL,
|
||||
"timeout": openaiConfig.Timeout,
|
||||
"base_url": openaiConfig.BaseURL,
|
||||
"request_timeout": openaiConfig.RequestTimeout,
|
||||
"response_timeout": openaiConfig.ResponseTimeout,
|
||||
"idle_conn_timeout": openaiConfig.IdleConnTimeout,
|
||||
},
|
||||
"auth": gin.H{
|
||||
"enabled": authConfig.Enabled,
|
||||
@@ -192,9 +194,17 @@ func (h *Handler) GetConfig(c *gin.Context) {
|
||||
},
|
||||
"performance": gin.H{
|
||||
"max_concurrent_requests": perfConfig.MaxConcurrentRequests,
|
||||
"request_timeout": perfConfig.RequestTimeout,
|
||||
"enable_gzip": perfConfig.EnableGzip,
|
||||
},
|
||||
"timeout_config": gin.H{
|
||||
"request_timeout_s": openaiConfig.RequestTimeout,
|
||||
"response_timeout_s": openaiConfig.ResponseTimeout,
|
||||
"idle_conn_timeout_s": openaiConfig.IdleConnTimeout,
|
||||
"server_read_timeout_s": serverConfig.ReadTimeout,
|
||||
"server_write_timeout_s": serverConfig.WriteTimeout,
|
||||
"server_idle_timeout_s": serverConfig.IdleTimeout,
|
||||
"graceful_shutdown_timeout_s": serverConfig.GracefulShutdownTimeout,
|
||||
},
|
||||
"log": gin.H{
|
||||
"level": logConfig.Level,
|
||||
"format": logConfig.Format,
|
||||
|
@@ -53,14 +53,13 @@ func NewProxyServer(keyManager types.KeyManager, configManager types.ConfigManag
|
||||
openaiConfig := configManager.GetOpenAIConfig()
|
||||
perfConfig := configManager.GetPerformanceConfig()
|
||||
|
||||
|
||||
// Create high-performance HTTP client
|
||||
transport := &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 20,
|
||||
MaxConnsPerHost: 0,
|
||||
IdleConnTimeout: 120 * time.Second,
|
||||
TLSHandshakeTimeout: 30 * time.Second,
|
||||
IdleConnTimeout: time.Duration(openaiConfig.IdleConnTimeout) * time.Second,
|
||||
TLSHandshakeTimeout: time.Duration(openaiConfig.ResponseTimeout) * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
DisableCompression: !perfConfig.EnableGzip,
|
||||
ForceAttemptHTTP2: true,
|
||||
@@ -73,19 +72,19 @@ func NewProxyServer(keyManager types.KeyManager, configManager types.ConfigManag
|
||||
MaxIdleConns: 200,
|
||||
MaxIdleConnsPerHost: 40,
|
||||
MaxConnsPerHost: 0,
|
||||
IdleConnTimeout: 300 * time.Second, // Keep streaming connections longer
|
||||
TLSHandshakeTimeout: 30 * time.Second,
|
||||
IdleConnTimeout: time.Duration(openaiConfig.IdleConnTimeout) * time.Second,
|
||||
TLSHandshakeTimeout: time.Duration(openaiConfig.ResponseTimeout) * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
DisableCompression: true, // Always disable compression for streaming
|
||||
ForceAttemptHTTP2: true,
|
||||
WriteBufferSize: 64 * 1024,
|
||||
ReadBufferSize: 64 * 1024,
|
||||
ResponseHeaderTimeout: 30 * time.Second,
|
||||
ResponseHeaderTimeout: time.Duration(openaiConfig.ResponseTimeout) * time.Second,
|
||||
}
|
||||
|
||||
httpClient := &http.Client{
|
||||
Transport: transport,
|
||||
Timeout: time.Duration(openaiConfig.Timeout) * time.Millisecond,
|
||||
Timeout: time.Duration(openaiConfig.RequestTimeout) * time.Second,
|
||||
}
|
||||
|
||||
// Streaming client without overall timeout
|
||||
@@ -229,7 +228,7 @@ func (ps *ProxyServer) executeRequestWithRetry(c *gin.Context, startTime time.Ti
|
||||
ctx, cancel = context.WithCancel(c.Request.Context())
|
||||
} else {
|
||||
// Non-streaming requests use configured timeout from the already fetched config
|
||||
timeout := time.Duration(openaiConfig.Timeout) * time.Millisecond
|
||||
timeout := time.Duration(openaiConfig.RequestTimeout) * time.Second
|
||||
ctx, cancel = context.WithTimeout(c.Request.Context(), timeout)
|
||||
}
|
||||
defer cancel()
|
||||
|
Reference in New Issue
Block a user