Skip to content

Commit

Permalink
Reintroduce timeout and keepalive for watch requests to match client-go
Browse files Browse the repository at this point in the history
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 kubernetes-client#2127

Previous relevant issues:
- Initial issue: kubernetes-client#559
  - PR: kubernetes-client#630
- Improvement: kubernetes-client#632
  - PR: kubernetes-client#635
  • Loading branch information
Alabate authored and Aurelien LABATE committed Dec 28, 2024
1 parent 27ba0e1 commit 4113110
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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));
Expand Down

0 comments on commit 4113110

Please sign in to comment.