Skip to content
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ This changelog keeps track of work items that have been completed and are ready

### Improvements

- **General**: TODO ([#TODO](https://github.com/kedacore/http-add-on/issues/TODO))
- **Interceptor**: Add `KEDA_HTTP_DISABLE_KEEP_ALIVES` to disable keep-alive connections from interceptor to backend services ([#1571](https://github.com/kedacore/http-add-on/pull/1571))

### Fixes

Expand Down
2 changes: 2 additions & 0 deletions interceptor/config/timeouts.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Timeouts struct {
WorkloadReplicas time.Duration `env:"KEDA_CONDITION_WAIT_TIMEOUT" envDefault:"20s"`
// ForceHTTP2 toggles whether to try to force HTTP2 for all requests
ForceHTTP2 bool `env:"KEDA_HTTP_FORCE_HTTP2" envDefault:"false"`
// DisableKeepAlives disables HTTP keep-alives for requests from interceptor to backend services.
DisableKeepAlives bool `env:"KEDA_HTTP_DISABLE_KEEP_ALIVES" envDefault:"false"`
// MaxIdleConns is the max number of idle connections to keep in the
// interceptor's internal connection pool across all backend services.
// Increase this if you proxy to many unique backend services.
Expand Down
1 change: 1 addition & 0 deletions interceptor/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func BuildProxyHandler(cfg *ProxyHandlerConfig) http.Handler {
Proxy: http.ProxyFromEnvironment,
DialContext: dialFunc,
ForceAttemptHTTP2: cfg.Timeouts.ForceHTTP2,
DisableKeepAlives: cfg.Timeouts.DisableKeepAlives,
MaxIdleConns: cfg.Timeouts.MaxIdleConns,
MaxIdleConnsPerHost: cfg.Timeouts.MaxIdleConnsPerHost,
IdleConnTimeout: cfg.Timeouts.IdleConnTimeout,
Expand Down
47 changes: 45 additions & 2 deletions interceptor/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,47 @@ func TestProxyHandler_BackendReceivesCorrectRequest(t *testing.T) {
}
}

func TestProxyHandler_DisableKeepAlives(t *testing.T) {
var backendRequestedClose bool
h := newProxyTestHarness(t, harnessConfig{
disableKeepAlives: true,
backendHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
backendRequestedClose = r.Close
w.WriteHeader(http.StatusOK)
}),
})

resp := h.doRequest(t, http.MethodGet, "/", testHost)
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK)
}
if !backendRequestedClose {
t.Error("expected backend request to set Connection: close when keep-alives are disabled")
}
}

func TestProxyHandler_DefaultKeepAlivesEnabled(t *testing.T) {
var backendRequestedClose bool
h := newProxyTestHarness(t, harnessConfig{
backendHandler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
backendRequestedClose = r.Close
w.WriteHeader(http.StatusOK)
}),
})

resp := h.doRequest(t, http.MethodGet, "/", testHost)
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Fatalf("status = %d, want %d", resp.StatusCode, http.StatusOK)
}
if backendRequestedClose {
t.Error("expected backend request to keep connections open when keep-alives are enabled")
}
}

func TestProxyHandler_UnknownHostReturnsError(t *testing.T) {
h := newProxyTestHarness(t, harnessConfig{})

Expand Down Expand Up @@ -283,6 +324,7 @@ type harnessConfig struct {
simulateColdStart bool
tlsEnabled bool
tracingEnabled bool
disableKeepAlives bool
useBlockingQueue bool
}

Expand Down Expand Up @@ -369,8 +411,9 @@ func newProxyTestHarness(t *testing.T, cfg harnessConfig) *proxyTestHarness {
RoutingTable: routingTable,
Reader: fake.NewClientBuilder().WithScheme(kedacache.NewScheme()).Build(),
Timeouts: config.Timeouts{
WorkloadReplicas: 5 * time.Second,
ResponseHeader: 5 * time.Second,
WorkloadReplicas: 5 * time.Second,
ResponseHeader: 5 * time.Second,
DisableKeepAlives: cfg.disableKeepAlives,
},
Serving: config.Serving{EnableColdStartHeader: cfg.enableColdStartHeader},
TLSConfig: tlsCfg,
Expand Down
Loading