Skip to content

Commit 7d90703

Browse files
feat(go): add default http client with timeout
1 parent 5ed8075 commit 7d90703

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
@@ -28,7 +28,7 @@ type Client struct {
2828
// DefaultClientOptions read from the environment (OPENLAYER_API_KEY,
2929
// OPENLAYER_BASE_URL). This should be used to initialize new clients.
3030
func DefaultClientOptions() []option.RequestOption {
31-
defaults := []option.RequestOption{option.WithEnvironmentProduction()}
31+
defaults := []option.RequestOption{option.WithHTTPClient(defaultHTTPClient()), option.WithEnvironmentProduction()}
3232
if o, ok := os.LookupEnv("OPENLAYER_BASE_URL"); ok {
3333
defaults = append(defaults, option.WithBaseURL(o))
3434
}

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 openlayer
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)