Use shared HTTP client for upstream requests

- Create package-level httpClient with 300s timeout\n- Reuse client instead of creating new one per request\n- Prevents resource exhaustion under load\n- Reduces connection overhead
This commit is contained in:
Franz Kafka 2026-04-15 09:02:17 +00:00
parent b906d49f9e
commit 1c3eccce10
2 changed files with 4 additions and 2 deletions

View file

@ -19,6 +19,9 @@ type Config struct {
var config *Config var config *Config
// httpClient is a shared HTTP client for all upstream requests
var httpClient = &http.Client{Timeout: 300 * time.Second}
// blockedHeaders are headers that should never be forwarded to upstream // blockedHeaders are headers that should never be forwarded to upstream
// for security/privacy reasons. These headers could leak internal URLs, // for security/privacy reasons. These headers could leak internal URLs,
// session information, or other sensitive data. // session information, or other sensitive data.
@ -218,8 +221,7 @@ func callUpstream(req *AnthropicRequest, apiKey, sessionID string) (*http.Respon
httpReq.Header.Set(k, v) httpReq.Header.Set(k, v)
} }
client := &http.Client{Timeout: 300 * time.Second} return httpClient.Do(httpReq)
return client.Do(httpReq)
} }
func writeError(w http.ResponseWriter, code int, message, errType, errCode string) { func writeError(w http.ResponseWriter, code int, message, errType, errCode string) {

BIN
proxx

Binary file not shown.