Skip to content

Commit a045972

Browse files
committed
ba-proxy-agent: close idle connections to mitigate memory leaks
The ba-proxy-agent currently experiences increasing memory consumption, leading to daily or weekly restarts. Previous mitigation attempts were insufficient, and the issue has been isolated to high memory usage on the agent connection side. This CL mitigates the issue by enforcing a timeout on idle connections. Since the root cause remains elusive, forcefully closing unused connections prevents memory accumulation from persistent links. Implementation details: * Introduced `lastActivityTime` property to agent connections, which updates upon usage. * Added a background routine to monitor connection activity. * Configured the routine to explicitly close connections that remain idle for more than 30 seconds.
1 parent 8c4f79b commit a045972

2 files changed

Lines changed: 5 additions & 2 deletions

File tree

agent/agent.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ var (
7373
injectBanner = flag.String("inject-banner", "", "HTML snippet to inject in served webpages")
7474
bannerHeight = flag.String("banner-height", "40px", "Height of the injected banner. This is ignored if no banner is set.")
7575
shimWebsockets = flag.Bool("shim-websockets", false, "Whether or not to replace websockets with a shim")
76+
websocketShimTimeout = flag.Duration("websocket-shim-timeout", 60*time.Minute, "Timeout for websocket shim connections to expire due to inactivity.")
7677
shimPath = flag.String("shim-path", "", "Path under which to handle websocket shim requests")
7778
healthCheckPath = flag.String("health-check-path", "/", "Path on backend host to issue health checks against. Defaults to the root.")
7879
healthCheckFreq = flag.Int("health-check-interval-seconds", 0, "Wait time in seconds between health checks. Set to zero to disable health checks. Checks disabled by default.")
@@ -126,7 +127,8 @@ func hostProxy(ctx context.Context, host, shimPath string, injectShimCode, force
126127
// restricted to a path prefix not equal to "/" will fail for websocket open requests. Passing in the
127128
// sessionHandler twice allows the websocket handler to ensure that cookies are applied based on the
128129
// correct, restored path.
129-
h, err = websockets.Proxy(ctx, h, host, shimPath, *rewriteWebsocketHost, *enableWebsocketsInjection, sessionLRU.SessionHandler, metricHandler)
130+
h, err = websockets.Proxy(ctx, h, host, shimPath, *rewriteWebsocketHost, *enableWebsocketsInjection, sessionLRU.SessionHandler,
131+
metricHandler, *websocketShimTimeout)
130132
if injectShimCode {
131133
shimFunc, err := websockets.ShimBody(shimPath)
132134
if err != nil {

agent/websockets/websockets_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"strings"
3131
"sync"
3232
"testing"
33+
"time"
3334

3435
"github.com/google/go-cmp/cmp"
3536
"github.com/google/go-cmp/cmp/cmpopts"
@@ -239,7 +240,7 @@ func TestShimHandlers(t *testing.T) {
239240
openWrapper := func(h http.Handler, metricHandler *metrics.MetricHandler) http.Handler {
240241
return h
241242
}
242-
p, err := Proxy(context.Background(), h, serverURL.Host, testShimPath, false, false, openWrapper, nil)
243+
p, err := Proxy(context.Background(), h, serverURL.Host, testShimPath, false, false, openWrapper, nil, 60*time.Second)
243244
if err != nil {
244245
t.Fatalf("Failure creating the websocket shim proxy: %+v", err)
245246
}

0 commit comments

Comments
 (0)