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 configuration of PingTimeout/ReadIdleTimeout #512

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 32 additions & 1 deletion config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ var (
// 5 minutes is typically above the maximum sane scrape interval. So we can
// use keepalive for all configurations.
idleConnTimeout: 5 * time.Minute,
// readIdleTimeout is the timeout after which a health check using ping
// frame will be carried out if no frame is received on the connection.
// Note that a ping response will is considered a received frame, so if
// there is no other traffic on the connection, the health check will
// be performed every readIdleTimeout interval.
// If zero, no health check is performed.
// Defaults to 1min.
readIdleTimeout: 1 * time.Minute,
// PingTimeout is the timeout after which the connection will be closed
// if a response to Ping is not received.
// Defaults to 15s.
pingTimeout: 15 * time.Second,
}
)

Expand Down Expand Up @@ -429,6 +441,8 @@ type httpClientOptions struct {
keepAlivesEnabled bool
http2Enabled bool
idleConnTimeout time.Duration
readIdleTimeout time.Duration
pingTimeout time.Duration
userAgent string
}

Expand Down Expand Up @@ -470,6 +484,22 @@ func WithUserAgent(ua string) HTTPClientOption {
}
}

// WithReadIdleTimeout allows setting the health check will
// be performed every HTTP2 readIdleTimeout interval .
func WithReadIdleTimeout(timeout time.Duration) HTTPClientOption {
return func(opts *httpClientOptions) {
opts.readIdleTimeout = timeout
}
}

// WithPingTimeout allows setting the timeout after which the connection
// will be closed if a response to Ping is not received.
func WithPingTimeout(timeout time.Duration) HTTPClientOption {
return func(opts *httpClientOptions) {
opts.pingTimeout = timeout
}
}

// NewClient returns a http.Client using the specified http.RoundTripper.
func newClient(rt http.RoundTripper) *http.Client {
return &http.Client{Transport: rt}
Expand Down Expand Up @@ -541,7 +571,8 @@ func NewRoundTripperFromConfig(cfg HTTPClientConfig, name string, optFuncs ...HT
if err != nil {
return nil, err
}
http2t.ReadIdleTimeout = time.Minute
http2t.ReadIdleTimeout = opts.readIdleTimeout
http2t.PingTimeout = opts.pingTimeout
}

// If a authorization_credentials is provided, create a round tripper that will set the
Expand Down
28 changes: 28 additions & 0 deletions config/http_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,34 @@ func TestCustomIdleConnTimeout(t *testing.T) {
}
}

func TestPingTimeoutAndReadIdleTimeout(t *testing.T) {
clientDone := make(chan struct{})
pingTimeout := time.Millisecond * 10
readIdleTimeout := time.Millisecond * 50

ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-clientDone
}))
ts.Start()
defer ts.Close()

cfg := HTTPClientConfig{}
rt, err := NewRoundTripperFromConfig(cfg, "test", WithPingTimeout(pingTimeout), WithReadIdleTimeout(readIdleTimeout))
if err != nil {
t.Fatalf("Can't create a round-tripper from this config: %+v", cfg)
}

ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond)
defer cancel()

defer close(clientDone)
req, _ := http.NewRequestWithContext(ctx, "GET", ts.URL, nil)
_, err = rt.RoundTrip(req)
if err == nil || !strings.Contains(err.Error(), "context deadline exceeded") {
t.Fatalf("expected to get error about \"deadline exceeded\", got %+v", err)
}
}

func TestMissingBearerAuthFile(t *testing.T) {
cfg := HTTPClientConfig{
BearerTokenFile: MissingBearerTokenFile,
Expand Down