Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/aws-cdk/lib/cli/proxy-agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ export class ProxyAgentProvider {
agent: new ProxyAgent({
ca: await this.tryReadCABundle(caBundlePath),
getProxyForUrl,
// This agent replaces the AWS SDK's default one, which keeps connections alive. Without
// keep-alive, every SDK call opens a new TLS connection, which makes commands that send
// many calls, such as `cdk gc`, very slow.
keepAlive: true,
}),
caBundlePath,
};
Expand Down
25 changes: 25 additions & 0 deletions packages/aws-cdk/test/cli/proxy-agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as http from 'node:http';
import type { AddressInfo } from 'node:net';
import * as os from 'node:os';
import * as path from 'node:path';
import * as fs from 'fs-extra';
Expand Down Expand Up @@ -49,6 +51,29 @@ describe('ProxyAgentProvider', () => {
const provider = new ProxyAgentProvider(ioHost.asHelper('deploy'));
await expect(provider.create({ proxyAddress })).resolves.toBeDefined();
});

test('create() returns an agent that reuses connections', async () => {
let connections = 0;
const server = http.createServer((_req, res) => res.end('ok'));
server.on('connection', () => connections++);
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));

try {
// An empty proxy address connects directly, whatever the proxy environment variables say.
const { agent } = await new ProxyAgentProvider(ioHost.asHelper('deploy')).create({ proxyAddress: '' });
const url = `http://127.0.0.1:${(server.address() as AddressInfo).port}`;
for (let i = 0; i < 2; i++) {
await new Promise<void>((resolve, reject) => {
http.get(url, { agent }, (res) => res.resume().on('end', resolve)).on('error', reject);
});
}

expect(connections).toBe(1);
} finally {
server.closeAllConnections();
await new Promise((resolve) => server.close(resolve));
}
});
});

describe('CA bundle resolution', () => {
Expand Down
Loading