imroc / req

Simple Go HTTP client with Black Magic
https://req.cool
MIT License
4.25k stars 347 forks source link

Is it OK that I call req.C() every time I need to make a http request? #302

Closed GingerMoon closed 10 months ago

GingerMoon commented 10 months ago

Hi @imroc , thanks a lot for this awesome project!

According to my understanding, it should be OK that I call req.C() every time I need to make a http request from the point view of performance. There will not be performance penalty since different clients use the same global connection pool under the hood, correct? And I am curious if it is possible that different clients use their own connection pool? eg. GitHubClient uses its own connection pool and GitLabClient uses is own connection pool.

Any insight would be much appreciated!

imroc commented 10 months ago

No, req.C() create a new client, not reuse the default client, so they don't share the connection pool.

The best practice is to share the same client for different requests if there is no different settings at client level.

GingerMoon commented 10 months ago

Thanks for replying! So different clients use their own different connection pool. Say if I need to call 100 APIs which have 100 different hosts, and let's assume that every apiclient's connection pool size is 10, and keeping total 10*100 connections is OK for the system. The best practise is using 100 different ApiClients, correct?

imroc commented 10 months ago

Why do you need so many clients? different hosts can also share the same client and connection pool, you can use just only 1 client, reuse the client for every requests.

GingerMoon commented 10 months ago

The number 100 is not real, the real number of APIs(with different hosts) could 10 or so. Let's assume I need to call 11 outbound APIs, and the connection pool size is 10. If I use only one client for calling all the 11 APIs, then the 11th API call will need to wait for 1/10 connections in the connection pool to be released, and the connection will need to be created (the so called 3-times-handshake for TCP connection).

Is it true?

imroc commented 10 months ago

No, connection pool didn't limit size, will create new connection if needed.

GingerMoon commented 10 months ago

Thanks for sharing the insight!