4

I'm using superagent and while inspecting the network I noticed that superagent is creating a new TCP connection for each request. I'm using superagent for a sequence of requests which results in a large number of TCP connections (can reach several hundreds).

I tried to follow this idea and use agentkeepalive package however this approach has some downsides:

  1. While superagent works out-of-the-box with http and https, agentkeepalive needs to be defined per protocol.
  2. As a result of the previous section, in case I do an http request that redirects to https request I get an error of Protocol "https:" not supported. Expected "http:" since the protocol has changed.
  3. As a result of section 2 the app crushes since with an uncaughtException (which happens only upon redirects and not when using the wrong protocol i.e. the http agentkeepalive for https request)

Needless to say that using request.set('Connection', 'keep-alive'); didn't solve it.

So my question is: how can I reuse the TCP connection while using superagent without getting errors in redirects? Is there another solution to reuse the TCP connection in superagent beside the agentkeepalive package?

Rivi
  • 791
  • 3
  • 15
  • 23

1 Answers1

0

Use this library: https://www.npmjs.com/package/agentkeepalive You can integrate it with superagent like this:

const request = require('superagent');
const Agent = require('agentkeepalive');

const keepaliveAgent = new Agent({
  maxFreeSockets: 50,
});

request.get('http//www.example.com/path')
       .agent(keepaliveAgent);

In this example, the max number of connections is infinity, but it keep, at max, 50 idle connections opened, with the default keep alive timeout set (30 secs)

Edit:

About the http and https protocol using agentkeepalive, what I did in my case was the following workaround:

this.agent = new (baseUrl.toLowerCase().startsWith('https:')
            ? Agent.HttpsAgent
            : Agent)(
            {
                maxFreeSockets: 50,
                timeout: 60000,
                freeSocketTimeout: 30000,
            },
        );

Assuming that baseUrl will always be used with this agent. While it may not be out of the box with the protocol, it is a very simple solution and I used it as a base class for all my REST clients