Add base http client and transport (#909)
This commit is contained in:
commit
ab4d9a5d4d
@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
### Added
|
### Added
|
||||||
|
|
||||||
- [#905](https://github.com/spegel-org/spegel/pull/905) Change mirror type to url and add byte range parameter.
|
- [#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
|
### Changed
|
||||||
|
|
||||||
|
@ -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 {
|
func Wait(ctx context.Context, probeEndpoint string, period time.Duration, threshold int) error {
|
||||||
log := logr.FromContextOrDiscard(ctx)
|
log := logr.FromContextOrDiscard(ctx)
|
||||||
resolver := &net.Resolver{}
|
resolver := &net.Resolver{}
|
||||||
client := &http.Client{}
|
httpClient := httpx.BaseClient()
|
||||||
|
|
||||||
addr, port, err := net.SplitHostPort(probeEndpoint)
|
addr, port, err := net.SplitHostPort(probeEndpoint)
|
||||||
if err != nil {
|
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))
|
log.Info("running probe request", "endpoints", len(ips))
|
||||||
err = probeIPs(ctx, client, ips, port)
|
err = probeIPs(ctx, httpClient, ips, port)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err, "cleanup probe request failed")
|
log.Error(err, "cleanup probe request failed")
|
||||||
thresholdCount = 0
|
thresholdCount = 0
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
package httpx
|
package httpx
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
HeaderContentType = "Content-Type"
|
HeaderContentType = "Content-Type"
|
||||||
HeaderContentLength = "Content-Length"
|
HeaderContentLength = "Content-Length"
|
||||||
@ -12,3 +18,26 @@ const (
|
|||||||
HeaderWWWAuthenticate = "WWW-Authenticate"
|
HeaderWWWAuthenticate = "WWW-Authenticate"
|
||||||
HeaderXForwardedFor = "X-Forwarded-For"
|
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
24
pkg/httpx/httpx_test.go
Normal 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()
|
||||||
|
}
|
@ -31,8 +31,10 @@ type Client struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewClient() *Client {
|
func NewClient() *Client {
|
||||||
|
hc := httpx.BaseClient()
|
||||||
|
hc.Timeout = 0
|
||||||
return &Client{
|
return &Client{
|
||||||
hc: &http.Client{},
|
hc: hc,
|
||||||
tc: sync.Map{},
|
tc: sync.Map{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user