Repository navigation
CNC-1364: revert and fix keep alive agent #110
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
Merged
johnbastian-trayio
merged 13 commits into
master
from
CNC-1364/b/revert-and-fix-keepAliveAgent
Sep 3, 2026
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3425286
revert keepAliveAgent from prior PR
johnbastian-trayio 1174e11
Update .eslintrc.js
johnbastian-trayio 1e04a27
new logic to make keepAlive relative to http vs https
johnbastian-trayio 13fa22b
tests
johnbastian-trayio 4a345dd
patch bump
johnbastian-trayio 926804a
test update
johnbastian-trayio e61527f
review feedback fix
johnbastian-trayio 2846364
rename refactor
johnbastian-trayio f3652a4
added `disableKeepAliveAgent ` flag to disable new agent logic
johnbastian-trayio e5c71de
simplify comments
johnbastian-trayio c1a54ac
change explicit method flag behaviour
johnbastian-trayio 98dd3e5
comments updated
johnbastian-trayio 2aadd58
feedback update
johnbastian-trayio 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
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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Whether to skip the keep-alive agent. On by default; a method or a connector's | ||
| * globals can set `disableKeepAliveAgent: true`, and a method can set it back to | ||
| * `false` where its connector disabled it wholesale. | ||
| * | ||
| * A blanket `globals: false` does not discard this, unlike other globals. The | ||
| * flag gets set because keep-alive breaks a service, and `globals: false` is | ||
| * common on auth endpoints for unrelated reasons - silently re-enabling the | ||
| * agent there would defeat the point. Naming the key does discard it, since | ||
| * that is a deliberate statement about this flag rather than collateral. | ||
| * | ||
| * Hence checking `globals` directly rather than through `localOnly`, which | ||
| * cannot tell the blanket opt-out from the per-key one. | ||
| */ | ||
| const _ = require('lodash'); | ||
|
|
||
| module.exports = function (config) { | ||
|
|
||
| if (_.isBoolean(config.disableKeepAliveAgent)) { | ||
| return config.disableKeepAliveAgent; | ||
| } | ||
|
|
||
| //`globals: { disableKeepAliveAgent: false }` - drop the global, leaving the default | ||
| if (_.get(config, 'globals.disableKeepAliveAgent') === false) { | ||
| return false; | ||
| } | ||
|
|
||
| if (_.isBoolean(this._globalOptions.disableKeepAliveAgent)) { | ||
| return this._globalOptions.disableKeepAliveAgent; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| }; |
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 |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * TCP keep-alive, so a call still waiting on a slow API is not dropped by the | ||
| * AWS NAT gateway's 350 second idle timeout. | ||
| * See https://repost.aws/knowledge-center/lambda-vpc-timeout | ||
| * | ||
| * Not HTTP keep-alive: Node's `keepAlive: true` pools connections between | ||
| * requests and only arms the socket once it returns to the pool, after the | ||
| * response - too late. So this agent does not pool. It exists only to arm the | ||
| * socket as it is created. | ||
| * | ||
| * Switch off with `disableKeepAliveAgent` - see globalize/disableKeepAliveAgent.js | ||
| */ | ||
| const http = require('http'); | ||
| const https = require('https'); | ||
|
|
||
| const logger = require('../logger'); | ||
|
|
||
| /* | ||
| * Idle time before the first probe, and the gap between probes thereafter, since | ||
| * an answered probe resets the timer. Under the gateway's 350 seconds with | ||
| * margin, so one probe per idle period is enough. | ||
| */ | ||
| const KEEPALIVE_DELAY = 300000; | ||
|
|
||
| /* | ||
| * Real agents, used only for their `createConnection`, so TLS setup stays Node's | ||
| * job. These are shared, so `maxCachedSessions: 0` keeps them stateless: this | ||
| * agent extends http.Agent, so the TLS session key is computed by | ||
| * `http.Agent.getName` and omits `rejectUnauthorized`, `ca` and the rest. A | ||
| * session opened by a lax request would then be offered to a strict one for the | ||
| * same host. Node still verifies the certificate on resumption, so that is not | ||
| * a bypass today - but nothing here needs session reuse, and this way it cannot | ||
| * become one. | ||
| */ | ||
| const factories = { | ||
| 'http:': new http.Agent({ keepAlive: false }), | ||
| 'https:': new https.Agent({ keepAlive: false, maxCachedSessions: 0 }) | ||
| }; | ||
|
|
||
| class KeepAliveAgent extends http.Agent { | ||
|
|
||
| constructor (options) { | ||
| super(options); | ||
|
|
||
| /* | ||
| Node only rejects a protocol mismatch when the agent declares one, and | ||
| this agent serves both. Declaring none is also what keeps redirects | ||
| working, since needle reuses the same agent for the redirect target. | ||
| */ | ||
| this.protocol = undefined; | ||
| } | ||
|
|
||
| createConnection (options, callback) { | ||
| /* | ||
| needle sets this per request: the proxy's protocol when proxying, else | ||
| the target's. It dispatches anything but `https:` through http, so the | ||
| fallback mirrors that - a TLS socket there would break the request | ||
| rather than secure it, since needle would still be speaking http. | ||
| */ | ||
| const factory = ( factories[options.protocol] || factories['http:'] ); | ||
|
|
||
| const socket = factory.createConnection(options, callback); | ||
| socket.setKeepAlive(true, KEEPALIVE_DELAY); //set SO_KEEPALIVE | ||
|
|
||
| return socket; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| //A caller's own `agent` is returned untouched, so `agent: false` stays an opt-out | ||
| module.exports = function resolveKeepAliveAgent (options, disabled) { | ||
|
|
||
| if (options.agent !== undefined) { | ||
| return options.agent; | ||
| } | ||
|
|
||
| //`disableKeepAliveAgent`, set on the method or the connector's globals | ||
| if (disabled === true) { | ||
| logger.info('keep-alive agent disabled by configuration; not attaching one'); | ||
| return undefined; | ||
| } | ||
|
|
||
| /* | ||
| One per request, not shared: needle writes TLS options (`rejectUnauthorized`, | ||
| `cert`, `key`, ...) onto `agent.options`, so sharing would leak one method's | ||
| TLS settings into later requests. Agents open no sockets, so this is free. | ||
| */ | ||
| return new KeepAliveAgent({ keepAlive: false }); | ||
|
|
||
| }; | ||
|
|
||
| module.exports.KEEPALIVE_DELAY = KEEPALIVE_DELAY; | ||
| module.exports.KeepAliveAgent = KeepAliveAgent; | ||
| module.exports.factories = factories; | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.