Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http Transport Proxy function

Tags:

go

Given that I want to use a different proxy per request I did the following:

var proxies chan *url.URL

var anonymousClient = &http.Client{Transport: &http.Transport{Proxy: func(r *http.Request) (*url.URL, error) {
    fmt.Println("Called")
    p := <-proxies
    proxies <- p
    return p, nil
}}}

If I make 10 get requests using the above client Called gets printed once, shouldn't it be printed out with every request?

It looks to me that the result of the first call to that function gets cached and its called only once but I can be wrong, any ideas?

like image 819
Bread Avatar asked Jun 29 '26 12:06

Bread


1 Answers

From the net/http package documentation:

By default, Transport caches connections for future re-use. This may leave many open connections when accessing many hosts. This behavior can be managed using Transport's CloseIdleConnections method and the MaxIdleConnsPerHost and DisableKeepAlives fields.

Transports should be reused instead of created as needed. Transports are safe for concurrent use by multiple goroutines.

like image 105
Sridhar Avatar answered Jul 02 '26 07:07

Sridhar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!