perf: add HTTP connection pooling across providers and disable proxy buffering on SSE streams
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user