Add base http client and transport (#909)

This commit is contained in:
Philip Laine 2025-06-05 14:43:26 +02:00 committed by GitHub
commit ab4d9a5d4d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 59 additions and 3 deletions

View File

@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- [#905](https://github.com/spegel-org/spegel/pull/905) Change mirror type to url and add byte range parameter.
- [#909](https://github.com/spegel-org/spegel/pull/909) Add base http client and transport.
### Changed

View File

@ -63,7 +63,7 @@ func Run(ctx context.Context, addr, configPath string) error {
func Wait(ctx context.Context, probeEndpoint string, period time.Duration, threshold int) error {
log := logr.FromContextOrDiscard(ctx)
resolver := &net.Resolver{}
client := &http.Client{}
httpClient := httpx.BaseClient()
addr, port, err := net.SplitHostPort(probeEndpoint)
if err != nil {
@ -93,7 +93,7 @@ func Wait(ctx context.Context, probeEndpoint string, period time.Duration, thres
}
log.Info("running probe request", "endpoints", len(ips))
err = probeIPs(ctx, client, ips, port)
err = probeIPs(ctx, httpClient, ips, port)
if err != nil {
log.Error(err, "cleanup probe request failed")
thresholdCount = 0

View File

@ -1,5 +1,11 @@
package httpx
import (
"net"
"net/http"
"time"
)
const (
HeaderContentType = "Content-Type"
HeaderContentLength = "Content-Length"
@ -12,3 +18,26 @@ const (
HeaderWWWAuthenticate = "WWW-Authenticate"
HeaderXForwardedFor = "X-Forwarded-For"
)
// BaseClient returns a http client with reasonable defaults set.
func BaseClient() *http.Client {
return &http.Client{
Transport: BaseTransport(),
Timeout: 10 * time.Second,
}
}
// BaseTransport returns a http transport with reasonable defaults set.
func BaseTransport() *http.Transport {
return &http.Transport{
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,
}
}

24
pkg/httpx/httpx_test.go Normal file
View File

@ -0,0 +1,24 @@
package httpx
import (
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestBaseClient(t *testing.T) {
t.Parallel()
c := BaseClient()
require.Equal(t, 10*time.Second, c.Timeout)
_, ok := c.Transport.(*http.Transport)
require.True(t, ok)
}
func TestBaseTransport(t *testing.T) {
t.Parallel()
BaseTransport()
}

View File

@ -31,8 +31,10 @@ type Client struct {
}
func NewClient() *Client {
hc := httpx.BaseClient()
hc.Timeout = 0
return &Client{
hc: &http.Client{},
hc: hc,
tc: sync.Map{},
}
}