Best way to use HTTP client in a concurrent application

Gonzalez picture Gonzalez · Apr 21, 2016 · Viewed 11.5k times · Source

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?

Answer

william.taylor.09 picture william.taylor.09 · Apr 21, 2016

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.