Skip to content

Commit 560fbf4

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

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-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) => {

packages/types/src/http.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,33 @@ export interface HttpOptions {}
145145
* Represents the http options that can be passed to a browser http client.
146146
*/
147147
export interface BrowserHttpOptions extends HttpOptions {
148+
/**
149+
* The number of milliseconds a request can take before being automatically
150+
* terminated.
151+
*/
148152
requestTimeout?: number;
149153
}
150154

151155
/**
152156
* Represents the http options that can be passed to a node http client.
153157
*/
154158
export interface NodeHttpOptions extends HttpOptions {
159+
/**
160+
* The maximum time in milliseconds that the connection phase of a request
161+
* may take before the connection attempt is abandoned.
162+
*/
155163
connectionTimeout?: number;
164+
165+
/**
166+
* Whether sockets should be kept open even when there are no outstanding
167+
* requests so that future requests can forgo having to reestablish a TCP or
168+
* TLS connection.
169+
*/
170+
keepAlive?: boolean;
171+
172+
/**
173+
* The maximum time in milliseconds that a socket may remain idle before it
174+
* is closed.
175+
*/
156176
socketTimeout?: number;
157177
}

0 commit comments

Comments
 (0)