Enhance reverse proxy transport configuration for improved performance. Updated reverse.go to set a direct transport for the reverse proxy, ensuring more efficient request handling. Modified gateway.go to utilize the direct transport with customized connection settings, optimizing idle connections and timeouts.
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
// (e.g. https://host/api/ + v1 + users → https://host/api/v1/users).
|
||||
func NewReverseProxy(target *url.URL, stripPrefix, pathPrefix string, setAuth string) *httputil.ReverseProxy {
|
||||
proxy := httputil.NewSingleHostReverseProxy(target)
|
||||
proxy.Transport = DirectTransport()
|
||||
orig := proxy.Director
|
||||
targetQuery := target.RawQuery
|
||||
proxy.Director = func(req *http.Request) {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package proxy
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// DirectTransport returns a transport that never uses HTTP_PROXY/HTTPS_PROXY.
|
||||
// Upstream Telemt hosts are usually private or loopback; env proxies break that and
|
||||
// httptest in CI when runners set HTTP_PROXY.
|
||||
func DirectTransport() *http.Transport {
|
||||
t := http.DefaultTransport.(*http.Transport).Clone()
|
||||
t.Proxy = func(*http.Request) (*url.URL, error) { return nil, nil }
|
||||
return t
|
||||
}
|
||||
@@ -28,15 +28,13 @@ type Gateway struct {
|
||||
|
||||
// NewGateway builds handlers and reverse proxies from parsed config.
|
||||
func NewGateway(p *config.Parsed, log *slog.Logger) (*Gateway, error) {
|
||||
t := &http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
MaxIdleConns: 64,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
TLSHandshakeTimeout: 10 * time.Second,
|
||||
ExpectContinueTimeout: 1 * time.Second,
|
||||
DialContext: (&net.Dialer{Timeout: 5 * time.Second, KeepAlive: 30 * time.Second}).DialContext,
|
||||
ResponseHeaderTimeout: 120 * time.Second,
|
||||
}
|
||||
t := proxy.DirectTransport()
|
||||
t.MaxIdleConns = 64
|
||||
t.IdleConnTimeout = 90 * time.Second
|
||||
t.TLSHandshakeTimeout = 10 * time.Second
|
||||
t.ExpectContinueTimeout = 1 * time.Second
|
||||
t.DialContext = (&net.Dialer{Timeout: 5 * time.Second, KeepAlive: 30 * time.Second}).DialContext
|
||||
t.ResponseHeaderTimeout = 120 * time.Second
|
||||
g := &Gateway{
|
||||
parsed: p,
|
||||
proxies: make(map[string]*httputil.ReverseProxy),
|
||||
|
||||
Reference in New Issue
Block a user