Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(node-http-handler): call socket.setKeepAlive if enabled in http(s)Agent #4561

Merged
merged 19 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
e827350
fix: enable keep-alive in node-http-handler
yenfryherrerafeliz Mar 21, 2023
90ce070
refactor: refactor set-socket-keep-alive code
yenfryherrerafeliz Mar 22, 2023
c4a2e59
test: add unit test to set-socket-keep-alive
yenfryherrerafeliz Mar 22, 2023
b210645
refactor: refactor node-http-handler code
yenfryherrerafeliz Mar 22, 2023
323042b
Merge branch 'aws:main' into main
yenfryherrerafeliz May 1, 2023
7b0daae
refactor: set keepalive base on agent options
yenfryherrerafeliz May 1, 2023
825d9bf
Merge remote-tracking branch 'origin/main'
yenfryherrerafeliz May 2, 2023
b37289d
Merge branch 'aws:main' into main
yenfryherrerafeliz May 2, 2023
271d498
chore: refactor setkeepalive implementation
yenfryherrerafeliz May 4, 2023
23c4831
Merge branch 'aws:main' into main
yenfryherrerafeliz May 4, 2023
419b3e7
Merge remote-tracking branch 'refs/remotes/origin/main'
yenfryherrerafeliz May 4, 2023
270d9b2
fix: remove ts-ignore on httpAgent and add ts-expect-error instead
trivikr May 4, 2023
7ecb1d9
fix: remove optional chaining for socketKeepAlive
trivikr May 4, 2023
1019bac
fix: use object destructuring to remove variable socketKeepAlive
trivikr May 4, 2023
7507bfc
fix: remove import of SocketKeepAliveOptions
trivikr May 4, 2023
aec5546
test: remove set which passes undefined options
trivikr May 4, 2023
3eea808
fix: lint errors
trivikr May 4, 2023
2e4ac90
test: use net.Socket from node in unit tests
trivikr May 4, 2023
037ed1e
docs: add link to bug report in source code
trivikr May 4, 2023
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
11 changes: 11 additions & 0 deletions packages/node-http-handler/src/node-http-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getTransformedHeaders } from "./get-transformed-headers";
import { setConnectionTimeout } from "./set-connection-timeout";
import { setSocketTimeout } from "./set-socket-timeout";
import { writeRequestBody } from "./write-request-body";
import { setSocketKeepAlive, SocketKeepAliveOptions } from "./set-socket-keep-alive";
trivikr marked this conversation as resolved.
Show resolved Hide resolved

/**
* Represents the http options that can be passed to a node http client.
Expand Down Expand Up @@ -151,6 +152,16 @@ export class NodeHttpHandler implements HttpHandler {
};
}

const httpAgent = nodeHttpsOptions.agent;
trivikr marked this conversation as resolved.
Show resolved Hide resolved
if (typeof httpAgent === "object" && "keepAlive" in httpAgent) {
setSocketKeepAlive(req, {
// @ts-expect-error keepAlive is not public on httpAgent.
keepAlive: (httpAgent as hAgent).keepAlive,
// @ts-expect-error keepAliveMsecs is not public on httpAgent.
keepAliveMsecs: (httpAgent as hAgent).keepAliveMsecs,
});
}

writeRequestBody(req, request);
});
}
Expand Down
66 changes: 66 additions & 0 deletions packages/node-http-handler/src/set-socket-keep-alive.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { setSocketKeepAlive } from "./set-socket-keep-alive";
import { EventEmitter } from "events";
trivikr marked this conversation as resolved.
Show resolved Hide resolved

class MockSocket {
enableKeepAlive: boolean;
keepAliveInitialDelay: number;

setKeepAlive(enable, initialDelay) {
this.enableKeepAlive = enable;
this.keepAliveInitialDelay = initialDelay;
}

isKeepAliveEnabled() {
return this.enableKeepAlive;
}

getKeepALiveInitialDelay() {
return this.keepAliveInitialDelay;
}
}

describe("setSocketKeepAlive", () => {
let request: any;
let socket;

beforeEach(() => {
request = new EventEmitter();
socket = new MockSocket();
});

it("should set keepAlive to true", () => {
setSocketKeepAlive(request, {
keepAlive: true,
});
request.emit("socket", socket);

expect(socket.isKeepAliveEnabled()).toBeTruthy();
});

it("should set keepAlive to true with custom initialDelay", () => {
const initialDelay = 5 * 1000;
setSocketKeepAlive(request, {
keepAlive: true,
keepAliveMsecs: initialDelay,
});
request.emit("socket", socket);

expect(socket.isKeepAliveEnabled() && socket.getKeepALiveInitialDelay() === initialDelay).toBeTruthy();
});

it("should not set keepAlive at all when keepAlive is false", () => {
setSocketKeepAlive(request, {
keepAlive: false,
});
request.emit("socket", socket);

expect(socket.isKeepAliveEnabled()).toBeFalsy();
});

it("should not set keepAlive at all when socketKeepAlive is undefined", () => {
setSocketKeepAlive(request, undefined);
request.emit("socket", socket);

expect(socket.isKeepAliveEnabled()).toBeFalsy();
});
trivikr marked this conversation as resolved.
Show resolved Hide resolved
});
16 changes: 16 additions & 0 deletions packages/node-http-handler/src/set-socket-keep-alive.ts
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, socketKeepAlive: SocketKeepAliveOptions) => {
if (socketKeepAlive?.keepAlive !== true) {
trivikr marked this conversation as resolved.
Show resolved Hide resolved
return;
}

request.on("socket", (socket) => {
socket.setKeepAlive(socketKeepAlive.keepAlive, socketKeepAlive.keepAliveMsecs || 0);
});
};