为了账号安全,请及时绑定邮箱和手机立即绑定

Golang:http2 客户端在启用 idleConn 时立即通过代理关闭连接

Golang:http2 客户端在启用 idleConn 时立即通过代理关闭连接

Go
叮当猫咪 2022-10-17 16:50:51
0x00 TL;DR我使用 Go 来实现一个 http 客户端和 squid 作为转发代理来向远程服务器发送请求。当通过代理使用 http/1.1 或 http/1.1, http2 没有代理时一切顺利,但是,当通过代理使用 http2 客户端时,大多数连接立即关闭,只保留一两个。不确定这是我的错误代码还是什么。在 http 传输上启用了 idleConn 配置。提前致谢。0x01 环境代码package mainimport (    "context"    "crypto/tls"    "fmt"    "io"    "io/ioutil"    "net"    "net/http"    "net/url"    "sync"    "time")const (    proxyURL = "http://{{proxy-ip}}:3128")func main() {    wg := &sync.WaitGroup{}    wg.Add(1)    go func() {        doSendRequests()        wg.Done()    }()    wg.Wait()}func doSendRequests() {    //client := newHTTPClient(nil)    client := newHTTPClient(proxy)    round := 0    for {        round += 1        for i := 0; i < 5; i++ {            go func(r int) {                start := time.Now()                var err error                defer func() {                    duration := time.Since(start)                    fmt.Printf("Round: %d, A: %v, duration: %v\n", r, err, duration)                }()                req := newRequest("https://www.google.com")                resp, err := client.Do(req)                if err == nil {                    io.Copy(ioutil.Discard, resp.Body)                    resp.Body.Close()                } else {                    fmt.Printf("A: %v\n", err)                }            }(round)        }        time.Sleep(100 * time.Second)    }}type TLSDialer struct {    net.Dialer}var (    config = &tls.Config{InsecureSkipVerify: true})func (dialer *TLSDialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {    return tls.DialWithDialer(&dialer.Dialer, network, addr, config)}var (    DefaultDialer = net.Dialer{        Timeout:   30 * time.Second,        KeepAlive: 30 * time.Second,        DualStack: true,    }    DefaultTLSDialer = TLSDialer{DefaultDialer})func proxy(req *http.Request) (*url.URL, error) {    uri, err := url.Parse(proxyURL)    if err != nil {        return nil, nil    }    return uri, nil}
查看完整描述

1 回答

?
一只萌萌小番薯

TA贡献1795条经验 获得超7个赞

最后,我弄清楚了,一切都按预期工作,所以它与 net/http2 无关。

更多详情请参考本期:https ://github.com/golang/go/issues/50000 。


查看完整回答
反对 回复 2022-10-17
  • 1 回答
  • 0 关注
  • 190 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号