#104: Add support for HTTP_CLIENT_TIMEOUT_IN_SECONDS (undocumented)

This commit is contained in:
TwinProduction 2021-04-30 22:58:14 -04:00
parent 8b3b2f70bf
commit 857ad584e7

View File

@ -4,6 +4,8 @@ import (
"crypto/tls" "crypto/tls"
"net" "net"
"net/http" "net/http"
"os"
"strconv"
"time" "time"
"github.com/go-ping/ping" "github.com/go-ping/ping"
@ -16,14 +18,27 @@ var (
// pingTimeout is the timeout for the Ping function // pingTimeout is the timeout for the Ping function
// This is mainly exposed for testing purposes // This is mainly exposed for testing purposes
pingTimeout = 5 * time.Second pingTimeout = 5 * time.Second
// httpTimeout is the timeout for secureHTTPClient and insecureHTTPClient
httpTimeout = 10 * time.Second
) )
func init() {
// XXX: This is an undocumented feature. See https://github.com/TwinProduction/gatus/issues/104.
httpTimeoutInSecondsFromEnvironmentVariable := os.Getenv("HTTP_CLIENT_TIMEOUT_IN_SECONDS")
if len(httpTimeoutInSecondsFromEnvironmentVariable) > 0 {
if httpTimeoutInSeconds, err := strconv.Atoi(httpTimeoutInSecondsFromEnvironmentVariable); err == nil {
httpTimeout = time.Duration(httpTimeoutInSeconds) * time.Second
}
}
}
// GetHTTPClient returns the shared HTTP client // GetHTTPClient returns the shared HTTP client
func GetHTTPClient(insecure bool) *http.Client { func GetHTTPClient(insecure bool) *http.Client {
if insecure { if insecure {
if insecureHTTPClient == nil { if insecureHTTPClient == nil {
insecureHTTPClient = &http.Client{ insecureHTTPClient = &http.Client{
Timeout: 10 * time.Second, Timeout: httpTimeout,
Transport: &http.Transport{ Transport: &http.Transport{
MaxIdleConns: 100, MaxIdleConns: 100,
MaxIdleConnsPerHost: 20, MaxIdleConnsPerHost: 20,
@ -38,7 +53,7 @@ func GetHTTPClient(insecure bool) *http.Client {
} }
if secureHTTPClient == nil { if secureHTTPClient == nil {
secureHTTPClient = &http.Client{ secureHTTPClient = &http.Client{
Timeout: 10 * time.Second, Timeout: httpTimeout,
Transport: &http.Transport{ Transport: &http.Transport{
MaxIdleConns: 100, MaxIdleConns: 100,
MaxIdleConnsPerHost: 20, MaxIdleConnsPerHost: 20,