77 "io"
88 "net/http"
99 "strings"
10+ "time"
11+
12+ "github.com/codefresh-io/terraform-provider-codefresh/codefresh/envutil"
1013)
1114
1215// Client token, host, htpp.Client
@@ -28,19 +31,41 @@ type RequestOptions struct {
2831 XAccessToken string
2932}
3033
31- // NewClient returns a new client configured to communicate on a server with the
32- // given hostname and to send an Authorization Header with the value of
33- // token
34- func NewClient (hostname string , hostnameV2 string , token string , tokenHeader string ) * Client {
34+ // HttpClient returns a client which can be configured to communicate on a server with custom timeout settings
35+ func NewHttpClient (hostname string , hostnameV2 string , token string , tokenHeader string ) * Client {
3536 if tokenHeader == "" {
3637 tokenHeader = "Authorization"
3738 }
39+
40+ // Configurable HTTP transport with proper connection pooling and timeouts to prevent "connection reset by peer" errors,
41+ // default values are equivalent to default &http.Client{} settings
42+ transport := & http.Transport {
43+ // Limit maximum idle connections per host to prevent connection exhaustion
44+ MaxIdleConnsPerHost : envutil .GetEnvAsInt ("CF_HTTP_MAX_IDLE_CONNECTIONS_PER_HOST" , 2 ),
45+ // Limit total idle connections
46+ MaxIdleConns : envutil .GetEnvAsInt ("CF_HTTP_MAX_IDLE_CONNECTIONS" , 100 ),
47+ // Close idle connections after specified seconds to prevent server-side timeouts
48+ IdleConnTimeout : time .Duration (envutil .GetEnvAsInt ("CF_HTTP_IDLE_CONNECTION_TIMEOUT" , 90 )) * time .Second ,
49+ // Timeout for TLS handshake in seconds
50+ TLSHandshakeTimeout : time .Duration (envutil .GetEnvAsInt ("CF_HTTP_TLS_HANDSHAKE_TIMEOUT" , 10 )) * time .Second ,
51+ // Timeout for expecting response headers in seconds, 0 - no limits
52+ ResponseHeaderTimeout : time .Duration (envutil .GetEnvAsInt ("CF_HTTP_RESPONSE_HEADER_TIMEOUT" , 0 )) * time .Second ,
53+ // Disable connection reuse for more stable connections
54+ DisableKeepAlives : envutil .GetEnvAsBool ("CF_HTTP_DISABLE_KEEPALIVES" , false ),
55+ }
56+
57+ httpClient := & http.Client {
58+ Transport : transport ,
59+ // Overall request timeout in seconds, 0 - no limits
60+ Timeout : time .Duration (envutil .GetEnvAsInt ("CF_HTTP_GLOBAL_TIMEOUT" , 0 )) * time .Second ,
61+ }
62+
3863 return & Client {
3964 Host : hostname ,
4065 HostV2 : hostnameV2 ,
4166 Token : token ,
4267 TokenHeader : tokenHeader ,
43- Client : & http. Client {} ,
68+ Client : httpClient ,
4469 featureFlags : map [string ]bool {},
4570 }
4671
0 commit comments