douzi8127 2018-07-31 01:58
浏览 85
已采纳

对Go的http2实现的困惑

I run tons of traffic over SSL. I was thinking of speeding up these calls by using a http2 client. However, I'm hesitant to do so because it feels like i have less control on how it behaves.

Here is a production client using Go's basic net/http

ClientHTTP := &http.Client{
    Transport: &http.Transport{
        Proxy: http.ProxyFromEnvironment,
        Dial: (&net.Dialer{
            Timeout:   timeout * time.Second,
            KeepAlive: 1 * time.Minute,
        }).Dial,
        TLSHandshakeTimeout: timeout * time.Second,
        MaxIdleConns:        3000,
        MaxIdleConnsPerHost: 3000,
        IdleConnTimeout:     60 * time.Second,
    },
    Timeout: timeout * time.Second,
}

As far as I can tell I have way less control on the transport.

ClientHTTP2 := &http.Client{
    Transport: &http2.Transport{
        AllowHTTP: true,
    },
    Timeout: timeout * time.Second,
}

Is there anything I'm missing? Is http2 production ready? I understand that http2 uses a single TCP connection and therefore things likes pools go away. Yet it somehow feels incomplete. Will these behave the same way as the production client? Is there a better way to implement ClientHTTP2 and lastly, AllowHTTP doesn't seem to do anything. In the case where there might be an http call I thought I'd be able to able it safely, but instead it errors out.

  • 写回答

1条回答 默认 最新

  • douxing8939 2018-07-31 06:54
    关注

    http.Transport supports HTTP2, however you have to configure the more modern DialContext, not Dial (which is deprecated):

    package main
    
    import (
            "fmt"
            "net"
            "net/http"
            "net/http/httputil"
            "time"
    )
    
    func main() {
            c := &http.Client{
                    Transport: &http.Transport{
                            Proxy: http.ProxyFromEnvironment,
                            DialContext: (&net.Dialer{ // use DialContext here
                                    Timeout:   10 * time.Second,
                                    KeepAlive: 1 * time.Minute,
                            }).DialContext,
                            TLSHandshakeTimeout: 10 * time.Second,
                            MaxIdleConns:        3000,
                            MaxIdleConnsPerHost: 3000,
                            IdleConnTimeout:     60 * time.Second,
                    },
                    Timeout: 1 * time.Second,
            }
            res, _ := c.Get("https://http2.akamai.com/")
            b, _ := httputil.DumpResponse(res, false)
            fmt.Println(string(b))
    }
    
    // HTTP/2.0 200 OK
    // Content-Length: 11928
    // Accept-Ch: DPR, Width, Viewport-Width, Downlink, Save-Data
    // Access-Control-Allow-Credentials: false
    // ...
    

    The only reason to use http2.Transport is to skip the initial connection upgrade (aka. prior knowledge). If that is not a concern, stick to the standard client and transport.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助