Skip to content

Commit

Permalink
Do not keep alive http connection (#3878)
Browse files Browse the repository at this point in the history
  • Loading branch information
penghuazhou authored Nov 28, 2022
1 parent ed779dd commit cfc294b
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ To learn more about active deprecations, we recommend checking [GitHub Discussio
- **General:** Add explicit seccompProfile type to securityContext config ([#3561](https://github.com/kedacore/keda/issues/3561))
- **General:** Add `Min` column to ScaledJob visualization ([#3689](https://github.com/kedacore/keda/issues/3689))
- **General:** Add support to use pod identities for authentication in Azure Key Vault ([#3813](https://github.com/kedacore/keda/issues/3813)
- **General:** Support disable keep http connection alive([#3874](https://github.com/kedacore/keda/issues/3874)
- **General:** Improve the function used to normalize metric names ([#3789](https://github.com/kedacore/keda/issues/3789)
- **Apache Kafka Scaler:** SASL/OAuthbearer Implementation ([#3681](https://github.com/kedacore/keda/issues/3681))
- **Azure AD Pod Identity Authentication:** Improve error messages to emphasize problems around the integration with aad-pod-identity itself ([#3610](https://github.com/kedacore/keda/issues/3610))
Expand Down
10 changes: 10 additions & 0 deletions pkg/util/env_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ func ResolveOsEnvDuration(envName string) (*time.Duration, error) {

return nil, nil
}

func ResolveOsEnvBool(envName string, defaultValue bool) (bool, error) {
valueStr, found := os.LookupEnv(envName)

if found && valueStr != "" {
return strconv.ParseBool(valueStr)
}

return defaultValue, nil
}
27 changes: 21 additions & 6 deletions pkg/util/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import (
"time"
)

var disableKeepAlives bool

func init() {
var err error
disableKeepAlives, err = ResolveOsEnvBool("KEDA_HTTP_DISABLE_KEEP_ALIVE", false)
if err != nil {
disableKeepAlives = false
}
}

// HTTPDoer is an interface that matches the Do method on
// (net/http).Client. It should be used in function signatures
// instead of raw *http.Clients wherever possible
Expand All @@ -37,13 +47,18 @@ func CreateHTTPClient(timeout time.Duration, unsafeSsl bool) *http.Client {
if timeout <= 0 {
timeout = 300 * time.Millisecond
}
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: unsafeSsl},
Proxy: http.ProxyFromEnvironment,
}
if disableKeepAlives {
// disable keep http connection alive
transport.DisableKeepAlives = true
transport.IdleConnTimeout = 100 * time.Second
}
httpClient := &http.Client{
Timeout: timeout,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: unsafeSsl},
Proxy: http.ProxyFromEnvironment,
},
Timeout: timeout,
Transport: transport,
}

return httpClient
}

0 comments on commit cfc294b

Please sign in to comment.