Bread
Bread

Reputation: 443

http Transport Proxy function

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?

Upvotes: 2

Views: 1295

Answers (1)

Sridhar
Sridhar

Reputation: 2532

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.

Upvotes: 3

Related Questions