1
1
import { HttpHandler , HttpRequest , HttpResponse } from "@smithy/protocol-http" ;
2
2
import { buildQueryString } from "@smithy/querystring-builder" ;
3
- import type { NodeHttpHandlerOptions } from "@smithy/types" ;
3
+ import type { Logger , NodeHttpHandlerOptions } from "@smithy/types" ;
4
4
import { HttpHandlerOptions , Provider } from "@smithy/types" ;
5
5
import { Agent as hAgent , request as hRequest } from "http" ;
6
6
import { Agent as hsAgent , request as hsRequest , RequestOptions } from "https" ;
@@ -48,9 +48,15 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
48
48
* @internal
49
49
*
50
50
* @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.
51
53
* @returns timestamp of last emitted warning.
52
54
*/
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 {
54
60
// note, maxSockets is per origin.
55
61
const { sockets, requests, maxSockets } = agent ;
56
62
@@ -79,11 +85,10 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
79
85
* lockout.
80
86
*/
81
87
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.`
87
92
) ;
88
93
return Date . now ( ) ;
89
94
}
@@ -127,6 +132,7 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
127
132
}
128
133
return new hsAgent ( { keepAlive, maxSockets, ...httpsAgent } ) ;
129
134
} ) ( ) ,
135
+ logger : console ,
130
136
} ;
131
137
}
132
138
@@ -152,6 +158,7 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
152
158
} ;
153
159
const reject = async ( arg : unknown ) => {
154
160
await writeRequestBodyPromise ;
161
+ clearTimeout ( socketCheckTimeoutId ) ;
155
162
_reject ( arg ) ;
156
163
} ;
157
164
@@ -175,7 +182,11 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
175
182
// This warning will be cancelled if the request resolves.
176
183
socketCheckTimeoutId = setTimeout (
177
184
( ) => {
178
- this . socketWarningTimestamp = NodeHttpHandler . checkSocketUsage ( agent , this . socketWarningTimestamp ) ;
185
+ this . socketWarningTimestamp = NodeHttpHandler . checkSocketUsage (
186
+ agent ,
187
+ this . socketWarningTimestamp ,
188
+ this . config ! . logger
189
+ ) ;
179
190
} ,
180
191
this . config . socketAcquisitionWarningTimeout ??
181
192
( this . config . requestTimeout ?? 2000 ) + ( this . config . connectionTimeout ?? 1000 )
@@ -252,7 +263,10 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> {
252
263
} ) ;
253
264
}
254
265
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
+ } ) ;
256
270
} ) ;
257
271
}
258
272
0 commit comments