govd/util/http.go
2025-04-15 10:46:54 +02:00

40 lines
830 B
Go

package util
import (
"net"
"net/http"
"sync"
"time"
)
var (
httpSession *http.Client
httpSessionOnce sync.Once
)
func GetHTTPSession() *http.Client {
httpSessionOnce.Do(func() {
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}).DialContext,
ForceAttemptHTTP2: true,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 20,
MaxConnsPerHost: 20,
ResponseHeaderTimeout: 30 * time.Second,
DisableCompression: false,
}
httpSession = &http.Client{
Transport: transport,
Timeout: 60 * time.Second,
}
})
return httpSession
}