-
Notifications
You must be signed in to change notification settings - Fork 785
/
http_utils.go
66 lines (58 loc) · 2.33 KB
/
http_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package util
import (
"github.com/jenkins-x/jx/pkg/log"
"net"
"net/http"
"os"
"strconv"
"time"
)
// defaults mirror the default http.Transport values
var jxDefaultTransport http.RoundTripper = &http.Transport{
DialContext: (&net.Dialer{
Timeout: time.Duration(getIntFromEnv("HTTP_DIALER_TIMEOUT", 30)) * time.Second,
KeepAlive: time.Duration(getIntFromEnv("HTTP_DIALER_KEEP_ALIVE", 30)) * time.Second,
DualStack: getBoolFromEnv("HTTP_USE_DUAL_STACK", true),
}).DialContext,
MaxIdleConns: getIntFromEnv("HTTP_MAX_IDLE_CONNS", 100),
IdleConnTimeout: time.Duration(getIntFromEnv("HTTP_IDLE_CONN_TIMEOUT", 90)) * time.Second,
TLSHandshakeTimeout: time.Duration(getIntFromEnv("HTTP_TLS_HANDSHAKE_TIMEOUT", 10)) * time.Second,
ExpectContinueTimeout: time.Duration(getIntFromEnv("HTTP_EXPECT_CONTINUE_TIMEOUT", 1)) * time.Second,
Proxy: http.ProxyFromEnvironment,
}
var defaultClient = http.Client{Transport: jxDefaultTransport, Timeout: time.Duration(getIntFromEnv("DEFAULT_HTTP_REQUEST_TIMEOUT", 30)) * time.Second}
// GetClient returns a Client reference with our default configuration
func GetClient() *http.Client {
return &defaultClient
}
// GetClientWithTimeout returns a client with JX default transport and user specified timeout
func GetClientWithTimeout(duration time.Duration) (*http.Client) {
client := http.Client{}
client.Transport = jxDefaultTransport
client.Timeout = duration
return &client
}
// GetCustomClient returns a client with user specified transport and timeout (in seconds)
func GetCustomClient(transport http.RoundTripper, timeout int) *http.Client {
return &(http.Client{Transport: transport, Timeout: time.Duration(timeout) * time.Second})
}
func getIntFromEnv(key string, fallback int) int {
if value, ok := os.LookupEnv(key); ok {
intValue, err := strconv.Atoi(value)
if err == nil {
return intValue
}
log.Warnf("Unable to convert env var %s with value %s to integer, using default value of %d instead", key, value, fallback)
}
return fallback
}
func getBoolFromEnv(key string, fallback bool) bool {
if value, ok := os.LookupEnv(key); ok {
boolValue, err := strconv.ParseBool(value)
if err == nil {
return boolValue
}
log.Warnf("Unable to convert env var %s with value %s to boolean, using default value of %t instead", key, value, fallback)
}
return fallback
}