Skip to content

Commit 5aa4fca

Browse files
feat(go): add default http client with timeout
1 parent 228fd76 commit 5aa4fca

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type Client struct {
4545
// DefaultClientOptions read from the environment (GITPOD_API_KEY,
4646
// GITPOD_BASE_URL). This should be used to initialize new clients.
4747
func DefaultClientOptions() []option.RequestOption {
48-
defaults := []option.RequestOption{option.WithEnvironmentProduction()}
48+
defaults := []option.RequestOption{option.WithHTTPClient(defaultHTTPClient()), option.WithEnvironmentProduction()}
4949
if o, ok := os.LookupEnv("GITPOD_BASE_URL"); ok {
5050
defaults = append(defaults, option.WithBaseURL(o))
5151
}

default_http_client.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
package gitpod
4+
5+
import (
6+
"net/http"
7+
"time"
8+
)
9+
10+
// defaultResponseHeaderTimeout bounds the time between a fully written request
11+
// and the server's response headers. It does not apply to the response body,
12+
// so long-running streams are unaffected. Without this, a server that accepts
13+
// the connection but never responds would hang the request indefinitely.
14+
const defaultResponseHeaderTimeout = 10 * time.Minute
15+
16+
// defaultHTTPClient returns an [*http.Client] used when the caller does not
17+
// supply one via [option.WithHTTPClient]. It clones [http.DefaultTransport]
18+
// and adds a [http.Transport.ResponseHeaderTimeout] so stuck connections
19+
// fail fast instead of compounding across retries.
20+
func defaultHTTPClient() *http.Client {
21+
transport := http.DefaultTransport.(*http.Transport).Clone()
22+
transport.ResponseHeaderTimeout = defaultResponseHeaderTimeout
23+
return &http.Client{Transport: transport}
24+
}

0 commit comments

Comments
 (0)