Best way to use HTTP client in a concurrent application
15,306
Http clients are thread safe according to the docs (https://golang.org/src/net/http/client.go):
Clients are safe for concurrent use by multiple goroutines.
Author by
Gonzalez
Updated on June 20, 2022Comments
-
Gonzalez almost 2 years
First I'll describe my case. I have to do HTTPS requests to several APIs from my application and they should be ran concurrently. I want to know if I should use a separate HTTP client per goroutine or I can share one client across all goroutines. Of course I'd like to enjoy connection reusing/pooling offered by the HTTP client, but I am concerned about it being thread(aka goroutine)-safe and if the client will run requests concurrently or they'll in fact be sequenced?
-
hunter_tech about 4 yearsIf goroutines are so many, will a single client become bottleneck ??
-
Deep Nirmal about 4 yearsYes same question, what if there are 1000s of goroutines using a single global client variable?
-
JuanPablo over 3 years@user4867444 please could you explain to us: why it won't be a bottleneck?
-
user4867444 over 3 yearsSimply because it doesn't have any locks
-
James Newman about 3 years@user4867444 - Firstly, the question is a perfectly reasonable one. Second, SO is a place for questions, where people help others understand how these things work. Third, you might not understand how this works, as there are locks in the http.Client. The
RoundTripper
is an interface, which by default useshttp.Transport
. This hasatleast 3 mutexe
s without going into more subfields. Next, the cookie jar -http.Jar
, again... has a mutex. There are infact multiple locks, and this question is not only valid, but it's also entirely possible to have an issue here.