Skip to content

Commit 7cc6b40

Browse files
LouisCltclaude
andcommitted
fix(network-proxy): keep proxied connections alive like the direct route
Smithy applies keepAlive and maxSockets: 50 only to agents it builds itself, so the direct route already pooled connections while the proxy agents built with no options paid a fresh CONNECT and TLS handshake per embedding request. Give both proxy agents the same defaults. Also pin that handle() forwards the request options (abort signal, request timeout) and returns the inner handler's promise on both routes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 5919f91 commit 7cc6b40

2 files changed

Lines changed: 30 additions & 17 deletions

File tree

‎src/utils/__tests__/networkProxy.spec.ts‎

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -528,45 +528,56 @@ describe("networkProxy", () => {
528528
expect(NodeHttpHandler).not.toHaveBeenCalled()
529529
})
530530

531-
it("should build both proxy agents without keeping connections alive", () => {
531+
it("should build both proxy agents with the keep-alive defaults of the direct route", () => {
532532
process.env.HTTPS_PROXY = "http://proxy.corp:3128"
533533

534534
expect(createProxyRoutingRequestHandler()).toBeDefined()
535535

536-
// No agent options: an idle connection would outlive the request that opened it.
537-
expect(HttpProxyAgent).toHaveBeenCalledWith("http://proxy.corp:3128")
538-
expect(HttpsProxyAgent).toHaveBeenCalledWith("http://proxy.corp:3128")
536+
const agentOptions = { keepAlive: true, maxSockets: 50 }
537+
expect(HttpProxyAgent).toHaveBeenCalledWith("http://proxy.corp:3128", agentOptions)
538+
expect(HttpsProxyAgent).toHaveBeenCalledWith("http://proxy.corp:3128", agentOptions)
539539
const { proxied } = innerHandlers()
540540
expect(proxied?.options?.httpAgent).toBe(vi.mocked(HttpProxyAgent).mock.instances[0])
541541
expect(proxied?.options?.httpsAgent).toBe(vi.mocked(HttpsProxyAgent).mock.instances[0])
542542
})
543543

544-
it("should send a request through the proxy when NO_PROXY does not cover it", () => {
544+
it("should send a request through the proxy when NO_PROXY does not cover it", async () => {
545545
process.env.HTTPS_PROXY = "http://proxy.corp:3128"
546546
process.env.NO_PROXY = "example.com"
547547

548548
const handler = createProxyRoutingRequestHandler()
549+
const { direct, proxied } = innerHandlers()
550+
const response = { response: { statusCode: 200 } }
551+
proxied?.handle.mockResolvedValue(response)
552+
553+
// The request options carry the abort signal and timeout, so they must reach the route.
549554
const request = requestTo("bedrock-runtime.us-east-1.amazonaws.com")
550-
handler?.handle(request)
555+
const requestOptions = { abortSignal: new AbortController().signal, requestTimeout: 1234 }
556+
const result = handler?.handle(request, requestOptions)
551557

552-
const { direct, proxied } = innerHandlers()
553-
expect(proxied?.handle).toHaveBeenCalledWith(request)
558+
expect(proxied?.handle).toHaveBeenCalledWith(request, requestOptions)
554559
expect(direct?.handle).not.toHaveBeenCalled()
560+
await expect(result).resolves.toBe(response)
555561
})
556562

557-
it("should send a request directly when NO_PROXY covers its host", () => {
563+
it("should send a request directly when NO_PROXY covers its host", async () => {
558564
process.env.HTTPS_PROXY = "http://proxy.corp:3128"
559565
process.env.NO_PROXY = "amazonaws.com"
560566

561567
const handler = createProxyRoutingRequestHandler()
568+
const { direct, proxied } = innerHandlers()
569+
const response = { response: { statusCode: 200 } }
570+
direct?.handle.mockResolvedValue(response)
571+
562572
// The host is only known per request, which is the point of deciding here: indexed
563573
// file contents must not reach a proxy the user excluded.
564574
const request = requestTo("bedrock-runtime.eu-west-1.amazonaws.com")
565-
handler?.handle(request)
575+
const requestOptions = { abortSignal: new AbortController().signal, requestTimeout: 1234 }
576+
const result = handler?.handle(request, requestOptions)
566577

567-
const { direct, proxied } = innerHandlers()
568-
expect(direct?.handle).toHaveBeenCalledWith(request)
578+
expect(direct?.handle).toHaveBeenCalledWith(request, requestOptions)
569579
expect(proxied?.handle).not.toHaveBeenCalled()
580+
await expect(result).resolves.toBe(response)
570581
})
571582

572583
it("should send every request directly when NO_PROXY is '*'", () => {

‎src/utils/networkProxy.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,18 +445,20 @@ export interface ProxyRoutingHandler {
445445
* override — so the destination is only known once a request has been built. Choosing here is
446446
* what makes NO_PROXY apply to the host actually called, rather than one guessed up front.
447447
*
448-
* The agents take no options, so no connection outlives its request. Both routes are HTTP/1.1:
449-
* a client defaulting to NodeHttp2Handler drops to 1.1 once a proxy is configured, which is what
450-
* the chat provider already does.
448+
* Both routes are HTTP/1.1: a client defaulting to NodeHttp2Handler drops to 1.1 once a proxy is
449+
* configured, which is what the chat provider already does.
451450
*/
452451
class ProxyRoutingRequestHandler implements ProxyRoutingHandler {
453452
private readonly direct = new NodeHttpHandler()
454453
private readonly proxied: NodeHttpHandler
455454

456455
constructor(proxyUrl: string) {
456+
// Smithy applies these defaults only to agents it builds itself, as for the direct route.
457+
// Without them every proxied request would pay a fresh CONNECT and TLS handshake.
458+
const agentOptions = { keepAlive: true, maxSockets: 50 }
457459
this.proxied = new NodeHttpHandler({
458-
httpAgent: new HttpProxyAgent(proxyUrl),
459-
httpsAgent: new HttpsProxyAgent(proxyUrl),
460+
httpAgent: new HttpProxyAgent(proxyUrl, agentOptions),
461+
httpsAgent: new HttpsProxyAgent(proxyUrl, agentOptions),
460462
})
461463
}
462464

0 commit comments

Comments
 (0)