Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom HTTP client. #170

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions http/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package http

import (
"errors"
"net/http"
"time"

"github.com/attestantio/go-eth2-client/metrics"
Expand All @@ -34,6 +35,7 @@ type parameters struct {
hooks *Hooks
reducedMemoryUsage bool
customSpecSupport bool
client *http.Client
}

// Parameter is the interface for service parameters.
Expand Down Expand Up @@ -134,6 +136,14 @@ func WithCustomSpecSupport(customSpecSupport bool) Parameter {
})
}

// WithHTTPClient provides a custom HTTP client for communication with the HTTP server.
// If not supplied then a standard HTTP client is used.
func WithHTTPClient(client *http.Client) Parameter {
return parameterFunc(func(p *parameters) {
p.client = client
})
}

// parseAndCheckParameters parses and checks parameters to ensure that mandatory parameters are present and correct.
func parseAndCheckParameters(params ...Parameter) (*parameters, error) {
parameters := parameters{
Expand Down
27 changes: 15 additions & 12 deletions http/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,21 @@ func New(ctx context.Context, params ...Parameter) (client.Service, error) {
}
}

httpClient := &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: parameters.timeout,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 64,
MaxConnsPerHost: 64,
MaxIdleConnsPerHost: 64,
IdleConnTimeout: 600 * time.Second,
},
httpClient := parameters.client
if httpClient == nil {
httpClient = &http.Client{
Transport: &http.Transport{
DialContext: (&net.Dialer{
Timeout: parameters.timeout,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 64,
MaxConnsPerHost: 64,
MaxIdleConnsPerHost: 64,
IdleConnTimeout: 600 * time.Second,
},
}
}

base, address, err := parseAddress(parameters.address)
Expand Down
Loading