Skip to content

Commit 200ed78

Browse files
trivikrsrchase
authored andcommitted
feat: node-http-handler set default keep-alive to true (#307)
1 parent a82ba22 commit 200ed78

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

packages/node-http-handler/src/node-http-handler.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,18 @@ import { setSocketTimeout } from "./set-socket-timeout";
1515
import { writeRequestBody } from "./write-request-body";
1616

1717
export class NodeHttpHandler implements HttpHandler<Readable, NodeHttpOptions> {
18-
constructor(private readonly httpOptions: NodeHttpOptions = {}) {}
18+
private readonly httpAgent: http.Agent;
19+
private readonly httpsAgent: https.Agent;
20+
21+
constructor(private readonly httpOptions: NodeHttpOptions = {}) {
22+
const { keepAlive } = httpOptions;
23+
this.httpAgent = new http.Agent({ keepAlive });
24+
this.httpsAgent = new https.Agent({ keepAlive });
25+
}
1926

2027
destroy(): void {
21-
// pass for now, but this may destroy the underlying agent in the future
22-
// if we decide to enable keep-alive by default.
28+
this.httpAgent.destroy();
29+
this.httpsAgent.destroy();
2330
}
2431

2532
handle(
@@ -43,7 +50,8 @@ export class NodeHttpHandler implements HttpHandler<Readable, NodeHttpOptions> {
4350
host: request.hostname,
4451
method: request.method,
4552
path: path,
46-
port: request.port
53+
port: request.port,
54+
agent: isSSL ? this.httpsAgent : this.httpAgent
4755
};
4856

4957
return new Promise((resolve, reject) => {

0 commit comments

Comments
 (0)