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.

Share:
15,306
Gonzalez
Author by

Gonzalez

Updated on June 20, 2022

Comments

  • Gonzalez
    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
    hunter_tech about 4 years
    If goroutines are so many, will a single client become bottleneck ??
  • Deep Nirmal
    Deep Nirmal about 4 years
    Yes same question, what if there are 1000s of goroutines using a single global client variable?
  • JuanPablo
    JuanPablo over 3 years
    @user4867444 please could you explain to us: why it won't be a bottleneck?
  • user4867444
    user4867444 over 3 years
    Simply because it doesn't have any locks
  • James Newman
    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 uses http.Transport. This has atleast 3 mutexes 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.