fix: Initialize transport object, set timeouts, and add IdleTimeout config

- Introduced idle connection timeout for HTTP client transport
- Implemented custom timeout for HTTP client when `RelayTimeout` is 0
- Set ImpatientHTTPClient timeout to 5 seconds
- Added new config variable `IdleTimeout` with default value of 30 seconds
- Utilized shared transport object for all HTTP clients in relay/util package
This commit is contained in:
Laisky.Cai
2024-03-08 12:58:05 +00:00
parent 758378e45a
commit 577ec90736
2 changed files with 13 additions and 3 deletions

View File

@@ -110,6 +110,7 @@ var BatchUpdateEnabled = false
var BatchUpdateInterval = helper.GetOrDefaultEnvInt("BATCH_UPDATE_INTERVAL", 5)
var RelayTimeout = helper.GetOrDefaultEnvInt("RELAY_TIMEOUT", 0) // unit is second
var IdleTimeout = helper.GetOrDefaultEnvInt("IDLE_TIMEOUT", 30) // unit is second
var GeminiSafetySetting = helper.GetOrDefaultEnvString("GEMINI_SAFETY_SETTING", "BLOCK_NONE")

View File

@@ -10,15 +10,24 @@ var HTTPClient *http.Client
var ImpatientHTTPClient *http.Client
func init() {
tp := &http.Transport{
IdleConnTimeout: time.Duration(config.IdleTimeout) * time.Second,
}
if config.RelayTimeout == 0 {
HTTPClient = &http.Client{}
HTTPClient = &http.Client{
Transport: tp,
}
} else {
HTTPClient = &http.Client{
Timeout: time.Duration(config.RelayTimeout) * time.Second,
Transport: tp,
Timeout: time.Duration(config.RelayTimeout) * time.Second,
}
}
ImpatientHTTPClient = &http.Client{
Timeout: 5 * time.Second,
Transport: tp,
Timeout: 5 * time.Second,
}
}