perf: add HTTP connection pooling across providers and disable proxy buffering on SSE streams
CI / Lint (push) Has been cancelled
CI / Test (push) Has been cancelled
CI / Build (push) Has been cancelled

This commit is contained in:
2026-07-21 17:40:18 +00:00
parent 0decc63e8c
commit 654f6ab6d1
9 changed files with 42 additions and 8 deletions
+33
View File
@@ -5,10 +5,15 @@ import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"regexp"
"strings"
"time"
"gophergate/internal/models"
"github.com/go-resty/resty/v2"
)
var keySanitizeRegex = regexp.MustCompile(`(?i)(key|api_key|secret)=[^&]+`)
@@ -18,6 +23,34 @@ func SanitizeURL(rawURL string) string {
return keySanitizeRegex.ReplaceAllString(rawURL, "$1=REDACTED")
}
// Shared HTTP transport configured with high connection pooling, TCP keep-alive,
// and HTTP/2 multiplexing to minimize latency when connecting to upstream LLM providers.
var sharedHTTPTransport = &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 200,
MaxIdleConnsPerHost: 50,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
}
// NewOptimizedRestyClient creates a resty client equipped with HTTP connection pooling.
func NewOptimizedRestyClient(timeout time.Duration) *resty.Client {
httpClient := &http.Client{
Transport: sharedHTTPTransport,
}
client := resty.NewWithClient(httpClient)
if timeout > 0 {
client.SetTimeout(timeout)
}
return client
}
func sanitizeFunctionName(name string) string {
var sb strings.Builder