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

Should I use different agent instances for different hosts or use a global one? #98

Open
Aex12 opened this issue May 16, 2021 · 6 comments

Comments

@Aex12
Copy link

Aex12 commented May 16, 2021

If I need to connect to different hosts, it's better to use a separated agent instance for each of this hosts, or use the same agent for all the connections?

@gchicoye
Copy link

gchicoye commented Jul 6, 2021

Same question, did you find out @Aex12 ?

@Aex12
Copy link
Author

Aex12 commented Jul 22, 2021

Same question, did you find out @Aex12 ?

I stopped using this library, and started using the native Agent class of Node. It has a keepAlive parameter, which defaults to false, but when enabled, it keeps sockets alive in a per-host basis. I made several tests comparing performance with both agents (multiple requests to a few different hosts) and the native Node (v14) implementation gave me better results than this library.

As to the question if it is better to use the same instance for all hosts, or a separated one for each host, I found no noticeable difference in performance between these approaches. Using always the same Agent instance for all requests, no matter what host, is easier to maintain, so this is how I'm doing it.

I found this to be the better performant config, at least for my use case: high concurrency, lots of requests per second, and a few selection of hosts.

import { Agent as HttpsAgent } from 'https';

export const httpsAgent = new HttpsAgent({
    keepAlive: true,
    timeout: 30 * 1000,
    maxSockets: 8, // this is per-host.
    scheduling: 'fifo',
});

You can checkout the rest of parameters available here:
https://nodejs.org/dist/latest-v14.x/docs/api/http.html#http_new_agent_options

@brandondoran
Copy link

@Aex12 did you compare fifo vs lifo?

@Aex12
Copy link
Author

Aex12 commented Sep 15, 2021

@Aex12 did you compare fifo vs lifo?

FIFO is better suited when you have lots of requests per second to the same host. LIFO is better when you have occasional requests to the same host.

@kurtextrem
Copy link

Just a heads-up: The manual solution does not automatically call setNoDelay() like this package does.

@harsh-sri
Copy link

Imo, Using same agent would be better considering the memory imact.

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

No branches or pull requests

5 participants