gpt4 book ai didi

go - http 传输代理功能

转载 作者:IT王子 更新时间:2023-10-29 02:04:06 26 4
gpt4 key购买 nike

鉴于我想为每个请求使用不同的代理,我执行了以下操作:

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
}}}

如果我使用上面的客户端 Called 发出 10 个 get 请求被打印一次,难道不是每个请求都打印出来吗?

在我看来,第一次调用该函数的结果被缓存并且只被调用一次,但我可能是错的,有什么想法吗?

最佳答案

来自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.

关于go - http 传输代理功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39190688/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com