Skip to content

Commit c16e014

Browse files
authored
fix(node-http-handler): add logger option and clear timeout on errors (#1311)
1 parent 9ff2027 commit c16e014

File tree

4 files changed

+39
-13
lines changed

4 files changed

+39
-13
lines changed

.changeset/purple-frogs-guess.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@smithy/node-http-handler": minor
3+
"@smithy/types": patch
4+
---
5+
6+
add logger option to node-http-handler parameters, clear socket usage check timeout on error

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -759,10 +759,9 @@ describe("NodeHttpHandler", () => {
759759

760760
expect(warningTimestamp).toBeGreaterThan(lastTimestamp);
761761
expect(console.warn).toHaveBeenCalledWith(
762-
"@smithy/node-http-handler:WARN",
763-
"socket usage at capacity=2 and 4 additional requests are enqueued.",
764-
"See https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-configuring-maxsockets.html",
765-
"or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler config."
762+
`@smithy/node-http-handler:WARN - socket usage at capacity=2 and 4 additional requests are enqueued.
763+
See https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-configuring-maxsockets.html
764+
or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler config.`
766765
);
767766
});
768767
});

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

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { HttpHandler, HttpRequest, HttpResponse } from "@smithy/protocol-http";
22
import { buildQueryString } from "@smithy/querystring-builder";
3-
import type { NodeHttpHandlerOptions } from "@smithy/types";
3+
import type { Logger, NodeHttpHandlerOptions } from "@smithy/types";
44
import { HttpHandlerOptions, Provider } from "@smithy/types";
55
import { Agent as hAgent, request as hRequest } from "http";
66
import { Agent as hsAgent, request as hsRequest, RequestOptions } from "https";
@@ -48,9 +48,15 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
4848
* @internal
4949
*
5050
* @param agent - http(s) agent in use by the NodeHttpHandler instance.
51+
* @param socketWarningTimestamp - last socket usage check timestamp.
52+
* @param logger - channel for the warning.
5153
* @returns timestamp of last emitted warning.
5254
*/
53-
public static checkSocketUsage(agent: hAgent | hsAgent, socketWarningTimestamp: number): number {
55+
public static checkSocketUsage(
56+
agent: hAgent | hsAgent,
57+
socketWarningTimestamp: number,
58+
logger: Logger = console
59+
): number {
5460
// note, maxSockets is per origin.
5561
const { sockets, requests, maxSockets } = agent;
5662

@@ -79,11 +85,10 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
7985
* lockout.
8086
*/
8187
if (socketsInUse >= maxSockets && requestsEnqueued >= 2 * maxSockets) {
82-
console.warn(
83-
"@smithy/node-http-handler:WARN",
84-
`socket usage at capacity=${socketsInUse} and ${requestsEnqueued} additional requests are enqueued.`,
85-
"See https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-configuring-maxsockets.html",
86-
"or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler config."
88+
logger?.warn?.(
89+
`@smithy/node-http-handler:WARN - socket usage at capacity=${socketsInUse} and ${requestsEnqueued} additional requests are enqueued.
90+
See https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-configuring-maxsockets.html
91+
or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler config.`
8792
);
8893
return Date.now();
8994
}
@@ -127,6 +132,7 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
127132
}
128133
return new hsAgent({ keepAlive, maxSockets, ...httpsAgent });
129134
})(),
135+
logger: console,
130136
};
131137
}
132138

@@ -152,6 +158,7 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
152158
};
153159
const reject = async (arg: unknown) => {
154160
await writeRequestBodyPromise;
161+
clearTimeout(socketCheckTimeoutId);
155162
_reject(arg);
156163
};
157164

@@ -175,7 +182,11 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
175182
// This warning will be cancelled if the request resolves.
176183
socketCheckTimeoutId = setTimeout(
177184
() => {
178-
this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage(agent, this.socketWarningTimestamp);
185+
this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage(
186+
agent,
187+
this.socketWarningTimestamp,
188+
this.config!.logger
189+
);
179190
},
180191
this.config.socketAcquisitionWarningTimeout ??
181192
(this.config.requestTimeout ?? 2000) + (this.config.connectionTimeout ?? 1000)
@@ -252,7 +263,10 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
252263
});
253264
}
254265

255-
writeRequestBodyPromise = writeRequestBody(req, request, this.config.requestTimeout).catch(_reject);
266+
writeRequestBodyPromise = writeRequestBody(req, request, this.config.requestTimeout).catch((e) => {
267+
clearTimeout(socketCheckTimeoutId);
268+
return _reject(e);
269+
});
256270
});
257271
}
258272

packages/types/src/http/httpHandlerInitialization.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import type { Agent as hAgent, AgentOptions as hAgentOptions } from "http";
22
import type { Agent as hsAgent, AgentOptions as hsAgentOptions } from "https";
33

4+
import { Logger } from "../logger";
5+
46
/**
57
*
68
* This type represents an alternate client constructor option for the entry
@@ -60,6 +62,11 @@ export interface NodeHttpHandlerOptions {
6062
* You can pass https.Agent or its constructor options.
6163
*/
6264
httpsAgent?: hsAgent | hsAgentOptions;
65+
66+
/**
67+
* Optional logger.
68+
*/
69+
logger?: Logger;
6370
}
6471

6572
/**

0 commit comments

Comments
 (0)