From a82c51ad98717df9e80cf98147c4c7e3fe714046 Mon Sep 17 00:00:00 2001 From: Aurelien LABATE Date: Sat, 28 Dec 2024 02:53:06 +0100 Subject: [PATCH] Reintroduce timeout and keepalive for watch requests to match client-go This will send sends keep-alive probes to the server every 30 seconds. These features were present prior to the 1.0 refactor but were inadvertently removed. Fixes #2127 Previous relevant issues: - Initial issue: #559 - PR: #630 - Improvement: #632 - PR: #635 --- src/watch.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/watch.ts b/src/watch.ts index 64c386f046..d1410c5168 100644 --- a/src/watch.ts +++ b/src/watch.ts @@ -40,6 +40,7 @@ export class Watch { const controller = new AbortController(); requestInit.signal = controller.signal as AbortSignal; requestInit.method = 'GET'; + requestInit.timeout = 30000; let doneCalled: boolean = false; const doneCallOnce = (err: any) => { @@ -53,6 +54,15 @@ export class Watch { try { const response = await fetch(watchURL, requestInit); + // Enable socket keep-alive to prevent connection stalls + if (requestInit.agent && typeof requestInit.agent === 'object') { + for (const socket of Object.values(requestInit.agent.sockets).flat()) { + if (socket) { + socket.setKeepAlive(true, 30000); + } + } + } + if (response.status === 200) { response.body.on('error', doneCallOnce); response.body.on('close', () => doneCallOnce(null));