Skip to content
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

Set a default limit to the connections undici can open #2726

Open
WilfredAlmeida opened this issue May 18, 2024 · 1 comment
Open

Set a default limit to the connections undici can open #2726

WilfredAlmeida opened this issue May 18, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@WilfredAlmeida
Copy link

WilfredAlmeida commented May 18, 2024

At Triton One, we have observed our customers face socket/connection issues. More information here

These issues have been there for more than 2 years.

tl;dr of the issues is that NodeJS tries to open infinite connections with the server and hits a limit and existing connections break and new connection attempts time out. In our tests, NodeJS opened over 16,000 connections. Opening new connection is unnecessary since existing ones can be resued. This is not the case with the Bun runtime. It opens less than 400 connections and doesn't error out at all.

With the current versions of web3.js (latest 1.91.8 as of this writing) the issues are frequent. We've also tested with the new web3.js 2.0.0-preview.3 and the error rate is less, but it still does exist.

The fix for these issues, as mentioned in the doc shared above, is to limit the number of connections NodeJS can open. This can be configured with the http.Agent configurations.

We propose that Web3JS should have a default configurable connection limit like the following:

// npm install undici

import { setGlobalDispatcher, Agent } from "undici";

setGlobalDispatcher(
  new Agent({
    connections: 1,
  })
);

From the http.Agent docs

maxSockets Maximum number of sockets to allow per host. If the same host opens multiple concurrent connections, each request will use new socket until the maxSockets value is reached. If the host attempts to open more connections than maxSockets, the additional requests will enter into a pending request queue, and will enter active connection state when an existing connection terminates. This makes sure there are at most maxSockets active connections at any point in time, from a given host. Default: Infinity.

Setting a default limit will allow connection reuse instead of a new connection being set for every request.

@WilfredAlmeida WilfredAlmeida added the enhancement New feature or request label May 18, 2024
@WilfredAlmeida WilfredAlmeida changed the title Add a default limit to the connections undici can open Set a default limit to the connections undici can open May 18, 2024
@NotoriousPyro
Copy link

Very nice you fixed all my issues I was having when subscribing to a load of accounts on program startup...

I use this

export const connection = new Connection(
    config.rpc.httpEndpoint,
    {
        httpAgent: new Agent({
            keepAlive: config.rpc.keepAlive,
            keepAliveMsecs: 60000,
            maxSockets: 256,
        }),
        ...config.rpc,
        fetch: RetryFetcher,
    },
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants