-
Notifications
You must be signed in to change notification settings - Fork 120
perf(node-http-handler): replace per-request checkSocketUsage timer with shared interval #1882
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
Open
TrevorBurnham
wants to merge
1
commit into
smithy-lang:main
Choose a base branch
from
TrevorBurnham:check-socket-usage-perf
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+112
−23
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@smithy/node-http-handler": patch | ||
| --- | ||
|
|
||
| consolidate checkSocketUsage timers to reduce overhead |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,6 +38,16 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> { | |
| private socketWarningTimestamp = 0; | ||
| private externalAgent = false; | ||
|
|
||
| /** | ||
| * @internal | ||
| * Single interval for socket usage warnings, shared across all requests. | ||
| * Lazily created on first handle() call. | ||
| * - undefined: not yet initialized | ||
| * - null: initialized, but not needed (maxSockets is Infinity) | ||
| * - NodeJS.Timeout: active interval | ||
| */ | ||
| private socketWarningInterval?: NodeJS.Timeout | null; | ||
|
|
||
| // Node http handler is hard-coded to http/1.1: https://github.com/nodejs/node/blob/ff5664b83b89c55e4ab5d5f60068fb457f1f5872/lib/_http_server.js#L286 | ||
| public readonly metadata = { handlerProtocol: "http/1.1" }; | ||
|
|
||
|
|
@@ -88,13 +98,11 @@ export class NodeHttpHandler implements HttpHandler<NodeHttpHandlerOptions> { | |
|
|
||
| /** | ||
| * Running at maximum socket usage can be intentional and normal. | ||
| * That is why this warning emits at a delay which can be seen | ||
| * at the call site's setTimeout wrapper. The warning will be cancelled | ||
| * if the request finishes in a reasonable amount of time regardless | ||
| * of socket saturation. | ||
| * That is why this check runs on a shared interval rather than | ||
| * immediately, giving transient spikes time to resolve. | ||
| * | ||
| * Additionally, when the warning is emitted, there is an interval | ||
| * lockout. | ||
| * Additionally, when the warning is emitted, there is a 15-second | ||
| * lockout to avoid log spam. | ||
| */ | ||
| if (socketsInUse >= maxSockets && requestsEnqueued >= 2 * maxSockets) { | ||
| logger?.warn?.( | ||
|
|
@@ -162,7 +170,54 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Lazily starts a single shared interval for socket usage warnings. | ||
| * Replaces the per-request timer that was previously created in handle(). | ||
| * Skipped entirely when maxSockets === Infinity on both agents. | ||
| */ | ||
| private ensureSocketWarningInterval(config: ResolvedNodeHttpHandlerConfig): void { | ||
| if (this.socketWarningInterval !== undefined) { | ||
| return; | ||
| } | ||
|
|
||
| const httpMax = config.httpAgent.maxSockets; | ||
| const httpsMax = config.httpsAgent.maxSockets; | ||
|
|
||
| if ( | ||
| (httpMax === Infinity || httpMax === undefined) && | ||
| (httpsMax === Infinity || httpsMax === undefined) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this won't activate much, since someone not making http requests won't specify http configuration |
||
| ) { | ||
| this.socketWarningInterval = null; | ||
| return; | ||
| } | ||
|
|
||
| const warningTimeout = | ||
| config.socketAcquisitionWarningTimeout ?? | ||
| (config.requestTimeout ?? 2000) + (config.connectionTimeout ?? 1000); | ||
|
|
||
| this.socketWarningInterval = setInterval(() => { | ||
| this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage( | ||
| config.httpsAgent, | ||
| this.socketWarningTimestamp, | ||
| config.logger | ||
| ); | ||
| this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage( | ||
| config.httpAgent, | ||
| this.socketWarningTimestamp, | ||
| config.logger | ||
| ); | ||
| }, warningTimeout); | ||
|
|
||
| if (this.socketWarningInterval.unref) { | ||
| this.socketWarningInterval.unref(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can be checked call |
||
| } | ||
| } | ||
|
|
||
| destroy(): void { | ||
| if (this.socketWarningInterval) { | ||
| clearInterval(this.socketWarningInterval); | ||
| this.socketWarningInterval = undefined; | ||
| } | ||
| this.config?.httpAgent?.destroy(); | ||
| this.config?.httpsAgent?.destroy(); | ||
| } | ||
|
|
@@ -175,6 +230,8 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf | |
| this.config = await this.configProvider; | ||
| } | ||
|
|
||
| this.ensureSocketWarningInterval(this.config); | ||
|
|
||
| return new Promise((_resolve, _reject) => { | ||
| const config = this.config!; | ||
|
|
||
|
|
@@ -221,21 +278,6 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf | |
| }); | ||
| } | ||
|
|
||
| // If the request is taking a long time, check socket usage and potentially warn. | ||
| // This warning will be cancelled if the request resolves. | ||
| timeouts.push( | ||
| timing.setTimeout( | ||
| () => { | ||
| this.socketWarningTimestamp = NodeHttpHandler.checkSocketUsage( | ||
| agent, | ||
| this.socketWarningTimestamp, | ||
| config.logger | ||
| ); | ||
| }, | ||
| config.socketAcquisitionWarningTimeout ?? (config.requestTimeout ?? 2000) + (config.connectionTimeout ?? 1000) | ||
| ) | ||
| ); | ||
|
|
||
| const queryString = buildQueryString(request.query || {}); | ||
| let auth = undefined; | ||
| if (request.username != null || request.password != null) { | ||
|
|
@@ -342,6 +384,10 @@ or increase socketAcquisitionWarningTimeout=(millis) in the NodeHttpHandler conf | |
|
|
||
| updateHttpClientConfig(key: keyof NodeHttpHandlerOptions, value: NodeHttpHandlerOptions[typeof key]): void { | ||
| this.config = undefined; | ||
| if (this.socketWarningInterval) { | ||
| clearInterval(this.socketWarningInterval); | ||
| } | ||
| this.socketWarningInterval = undefined; | ||
| this.configProvider = this.configProvider.then((config) => { | ||
| return { | ||
| ...config, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This interval would need a minimum period, but that is beside the point.
The interval fails to let batch traffic resolve when it fires at an inconvenient time.
If switching to an interval, it should warn on monotonic growth over time rather than using the existing criteria.