Skip to content
This repository has been archived by the owner on Jan 17, 2025. It is now read-only.

adding client config #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ var ProxyOrdinal = getProxyOrdinal(ProxyName)

var kubeClient *kubernetes.Clientset

var httpClient http.Client

// Info of StatefulSet
var proxies struct {
Count int64
Expand Down Expand Up @@ -71,6 +73,12 @@ var config struct {
ProxyTimeout int64
IdleTimeout int64
DebugLevel int64
//HTTP client transport implementation variables
MaxIdleConns int
MaxIdleConnsPerHost int
MaxConnsPerHost int
IdleConnTimeout int64
TLSHandshakeTimeout int64

// HTTP config comes from readiness probe
HTTP struct {
Expand Down Expand Up @@ -303,7 +311,6 @@ func doAsyncProxyRequest(w http.ResponseWriter, proxyRequest *http.Request, inse
}()

// Do the request
var httpClient http.Client
if insecureSkipVerify {
httpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
Expand Down Expand Up @@ -375,6 +382,7 @@ func doAsyncProxyRequest(w http.ResponseWriter, proxyRequest *http.Request, inse

// Starts the HTTP server
func startServer() {
setUpClient()
http.HandleFunc(config.HTTP.Path, httpHandler)

debugPrint(1, "[+] Listening on port %v (path \"%v\")", config.HTTP.Port, config.HTTP.Path)
Expand Down Expand Up @@ -622,6 +630,31 @@ func updateProxyConfig(annotations map[string]string) error {
return err
}

newMaxIdleConns, err := getOptionalConfigValue(annotations, "maxIdleConns", 2000)
if err != nil {
return err
}

newMaxIdleConnsPerHost, err := getOptionalConfigValue(annotations, "maxIdleConnsPerHost", 2000)
if err != nil {
return err
}

newMaxConnsPerHost, err := getOptionalConfigValue(annotations, "maxConnsPerHost", 700)
if err != nil {
return err
}

newIdleConnTimeout, err := getOptionalConfigValue(annotations, "idleConnTimeout", 90)
if err != nil {
return err
}

newTLSHandshakeTimeout, err := getOptionalConfigValue(annotations, "tlsHandshakeTimeout", 10)
if err != nil {
return err
}

// Begin shared lock for idle shutdown
state.IdleShutdown.RLock()
defer state.IdleShutdown.RUnlock()
Expand All @@ -639,6 +672,11 @@ func updateProxyConfig(annotations map[string]string) error {
config.ProxyTimeout = int64(newProxyTimeout)
config.IdleTimeout = int64(newIdleTimeout)
config.DebugLevel = int64(newDebugLevel)
config.MaxIdleConns = int(newMaxIdleConns)
config.MaxIdleConnsPerHost = int(newMaxIdleConnsPerHost)
config.MaxConnsPerHost = int(newMaxConnsPerHost)
config.IdleConnTimeout = int64(newIdleConnTimeout)
config.TLSHandshakeTimeout = int64(newTLSHandshakeTimeout)

// If we are the last proxy, ensure the min/max number of proxies
if ProxyOrdinal+1 == proxies.Count {
Expand Down Expand Up @@ -742,11 +780,21 @@ func printStats() {
}()
}

func setUpClient() {
netHTTPTransport := &http.Transport{
MaxIdleConns: config.MaxIdleConns,
MaxIdleConnsPerHost: config.MaxIdleConnsPerHost,
MaxConnsPerHost: config.MaxConnsPerHost,
IdleConnTimeout: time.Duration(config.IdleConnTimeout),
TLSHandshakeTimeout: time.Duration(config.TLSHandshakeTimeout),
}

httpClient.Transport = netHTTPTransport
}

func main() {
startWatcher()
setupIdleShutdown()

printStats()

startServer()
}