TooTallNate / proxy-agents

Node.js HTTP Proxy Agents Monorepo
https://proxy-agents.n8.io
871 stars 229 forks source link

Synchronously update internal sockets length so http.Agent pooling is used #300

Closed lukekarrys closed 2 months ago

lukekarrys commented 2 months ago

Fixes #299

This is a bit of a hack, but I went with this approach because it allows for all of the native socket pooling behavior in http.Agent to be used and connect() to keep it's async signature.

The tradeoff is manipulating the internal sockets dictionary which is readonly (according to @types/node) and therefore needs a lot of @ts-expect-error comments. An alternative approach would require overwriting the addRequest() method with custom pooling code.

The approach in this PR works by synchronously adding a fake socket to the pool so this.sockets[originName].length is accurate before any other requests are added. Then the socket is removed directly after connect() finishes.

changeset-bot[bot] commented 2 months ago

🦋 Changeset detected

Latest commit: 4650f933d24aef7f32246a4b0c03f36f7fa4b54d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package | Name | Type | | ---------- | ----- | | agent-base | Patch |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

vercel[bot] commented 2 months ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
proxy-agents ✅ Ready (Inspect) Visit Preview Mar 29, 2024 11:48pm
lukekarrys commented 2 months ago

I updated the reproduction in #299 to include more examples and when I install agent-base from this PR, it works as expected.

❯ npm i https://proxy-agents-git-fork-lukekarrys-lk-socket-pooling-tootallnate.vercel.app/agent-base.tgz
❯ node index.js
core
10 max sockets
727 total versions
1873533 total bytes
----------------------------------------
agent-base-sync
10 max sockets
727 total versions
1873533 total bytes
----------------------------------------
agent-base-async
10 max sockets
727 total versions
1873533 total bytes
----------------------------------------
https-proxy-agent
10 max sockets
727 total versions
1873533 total bytes
----------------------------------------