-
Notifications
You must be signed in to change notification settings - Fork 594
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(node-http-handler): call socket.setKeepAlive if enabled in http(s…
…)Agent (#4561) Co-authored-by: Trivikram Kamat <16024985+trivikr@users.noreply.github.com>
- Loading branch information
1 parent
0408b01
commit bd16ace
Showing
3 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/node-http-handler/src/set-socket-keep-alive.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { EventEmitter } from "events"; | ||
import { ClientRequest } from "http"; | ||
import { Socket } from "net"; | ||
|
||
import { setSocketKeepAlive } from "./set-socket-keep-alive"; | ||
|
||
describe("setSocketKeepAlive", () => { | ||
let request: ClientRequest; | ||
let socket: Socket; | ||
|
||
beforeEach(() => { | ||
request = new EventEmitter() as ClientRequest; | ||
socket = new Socket(); | ||
}); | ||
|
||
it("should set keepAlive to true", () => { | ||
setSocketKeepAlive(request, { keepAlive: true }); | ||
|
||
const setKeepAliveSpy = jest.spyOn(socket, "setKeepAlive"); | ||
request.emit("socket", socket); | ||
|
||
expect(setKeepAliveSpy).toHaveBeenCalled(); | ||
expect(setKeepAliveSpy).toHaveBeenCalledWith(true, 0); | ||
}); | ||
|
||
it("should set keepAlive to true with custom initialDelay", () => { | ||
const initialDelay = 5 * 1000; | ||
setSocketKeepAlive(request, { keepAlive: true, keepAliveMsecs: initialDelay }); | ||
|
||
const setKeepAliveSpy = jest.spyOn(socket, "setKeepAlive"); | ||
request.emit("socket", socket); | ||
|
||
expect(setKeepAliveSpy).toHaveBeenCalled(); | ||
expect(setKeepAliveSpy).toHaveBeenCalledWith(true, initialDelay); | ||
}); | ||
|
||
it("should not set keepAlive at all when keepAlive is false", () => { | ||
setSocketKeepAlive(request, { keepAlive: false }); | ||
|
||
const setKeepAliveSpy = jest.spyOn(socket, "setKeepAlive"); | ||
request.emit("socket", socket); | ||
|
||
expect(setKeepAliveSpy).not.toHaveBeenCalled(); | ||
}); | ||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ClientRequest } from "http"; | ||
|
||
export interface SocketKeepAliveOptions { | ||
keepAlive: boolean; | ||
keepAliveMsecs?: number; | ||
} | ||
|
||
export const setSocketKeepAlive = (request: ClientRequest, { keepAlive, keepAliveMsecs }: SocketKeepAliveOptions) => { | ||
if (keepAlive !== true) { | ||
return; | ||
} | ||
|
||
request.on("socket", (socket) => { | ||
socket.setKeepAlive(keepAlive, keepAliveMsecs || 0); | ||
}); | ||
}; |