gatus/client/client.go

53 lines
1.1 KiB
Go
Raw Normal View History

package client
import (
"crypto/tls"
"net"
"net/http"
"time"
)
var (
2020-10-23 22:29:20 +02:00
secureHTTPClient *http.Client
insecureHTTPClient *http.Client
)
2020-10-23 22:29:20 +02:00
// GetHTTPClient returns the shared HTTP client
func GetHTTPClient(insecure bool) *http.Client {
if insecure {
2020-10-23 22:29:20 +02:00
if insecureHTTPClient == nil {
insecureHTTPClient = &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
}
2020-10-23 22:29:20 +02:00
return insecureHTTPClient
}
if secureHTTPClient == nil {
secureHTTPClient = &http.Client{
Timeout: time.Second * 10,
Transport: &http.Transport{
MaxIdleConns: 100,
MaxIdleConnsPerHost: 20,
},
}
}
2020-10-23 22:29:20 +02:00
return secureHTTPClient
}
2020-10-23 22:29:20 +02:00
// CanCreateConnectionToTCPService checks whether a connection can be established with a TCP service
func CanCreateConnectionToTCPService(address string) bool {
conn, err := net.DialTimeout("tcp", address, 5*time.Second)
if err != nil {
return false
}
_ = conn.Close()
return true
}