I'm building an HTTP client in Go and I need to make calls to an endpoint via a proxy server. The proxy address is not fixed, so I'm currently creating a new http.Client instance every time I need to make a call. This isn't very efficient, as it involves creating a new transport and client object every time.
Is there a way to create the http.Client instance only once and then update the proxy address before making a call? I'd like to avoid creating a new client instance every time I need to make a request.
One more thing - There will be a fixed number of proxy servers (<5) and hence fixed number of URLs but I don't know the address in the beginning of those servers, and I will get those addresses dynamically.
Because of this condition, I am thinking if I could create multiple client - one for each server and reuse them based on the URL I get. But I would still prefer a solution where I could create only one client and update proxy URL somehow.
Any suggestions would be very helpful.
Here's my current code for creating the client:
proxyURL, err := url.Parse("http://proxy_url:proxy_port")
if err != nil {
fmt.Println(err)
return
}
transport := &http.Transport{
Proxy: http.ProxyURL(proxyURL),
}
client := &http.Client{
Transport: transport,
}
Thanks!