简体   繁体   English

对Go的http2实现的困惑

[英]Confusion over Go's http2 implementation

I run tons of traffic over SSL. 我通过SSL运行大量流量。 I was thinking of speeding up these calls by using a http2 client. 我当时正在考虑使用http2客户端加快这些调用的速度。 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 这是使用Go的基本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? http2生产就绪了吗? I understand that http2 uses a single TCP connection and therefore things likes pools go away. 我了解到http2使用单个TCP连接,因此像池之类的东西就消失了。 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. 有没有更好的方法来实现ClientHTTP2 ,最后, AllowHTTP似乎什么也没做。 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. 在可能会有一个http调用的情况下,我认为我可以安全地启用它,但会出错。

http.Transport supports HTTP2, however you have to configure the more modern DialContext, not Dial (which is deprecated): http.Transport支持http.Transport ,但是您必须配置更现代的DialContext,而不是Dial(已弃用):

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). 使用http2.Transport的唯一原因是跳过初始连接升级(又称为先验知识)。 If that is not a concern, stick to the standard client and transport. 如果这不是问题,请坚持使用标准客户并进行运输。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM