为什么在go1.16中配置了ReadIdleTimeout HTTP/2选项的传输?

huangapple go评论81阅读模式
英文:

Why configures the ReadIdleTimeout HTTP/2 option for the transport on go1.16?

问题

根据你提供的内容,我们注意到在google-api-go-client/transport/http/configure_http2_go116.go文件中,通过配置ReadIdleTimeout选项来为传输配置HTTP/2。这样做的目的是为了更快地清除断开的空闲连接,防止客户端尝试重用无法工作的连接。

而在google-api-go-client/transport/http/configure_http2_not_go116.go文件中,由于在Go 1.16及更高版本中才提供了相应的接口,因此该函数不执行任何操作。

根据net/http2/transport.go文件的注释,ConfigureTransport函数在很久之前就已经添加了,用于将net/http的HTTP/1传输配置为使用HTTP/2。

至于为什么在go1.16上配置传输的ReadIdleTimeout HTTP/2选项,具体原因可能需要查看更多的上下文信息。

英文:

From google api golang client, we notice that

google-api-go-client/transport/http/configure_http2_go116.go

//go:build go1.16
// +build go1.16

...
// configureHTTP2 configures the ReadIdleTimeout HTTP/2 option for the
// transport. This allows broken idle connections to be pruned more quickly,
// preventing the client from attempting to re-use connections that will no
// longer work.
func configureHTTP2(trans *http.Transport) {
	http2Trans, err := http2.ConfigureTransports(trans)
	if err == nil {
		http2Trans.ReadIdleTimeout = time.Second * 31
	}
}

Whereas in this file google-api-go-client/transport/http/configure_http2_not_go116.go

//go:build !go1.16
// +build !go1.16

// configureHTTP2 configures the ReadIdleTimeout HTTP/2 option for the
// transport. The interface to do this is only available in Go 1.16 and up, so
// this performs a no-op.
func configureHTTP2(trans *http.Transport) {}

Per net/http2/transport.go the ConfigureTransport was added long time before.

// ConfigureTransport configures a net/http HTTP/1 Transport to use HTTP/2.
// It returns an error if t1 has already been HTTP/2-enabled.
//
// Use ConfigureTransports instead to configure the HTTP/2 Transport.
func ConfigureTransport(t1 *http.Transport) error {

Why configures the ReadIdleTimeout HTTP/2 option for the transport on go1.16?

答案1

得分: 2

golang.org/x/net/http2包中有两个听起来很相似的函数,但它们的作用非常不同:

func ConfigureTransport(t1 *http.Transport) error
func ConfigureTransports(t1 *http.Transport) (*Transport, error)

我认为你可能把前者和后者搞混了。

根据问题跟踪器:
https://go-review.googlesource.com/c/net/+/264017

这两个非常相似的名称很不幸,但它们在godoc中会排在一起,而复数形式的ConfigureTransports提示了它的目的:它允许你同时配置http和http2传输。

ConfigureTransports是在一年前引入的:

commit 08b38378de702b893ee869b94b32f833e2933bd2
Author: Damien Neil <dneil@google.com>
Date:   Tue Oct 20 12:34:04 2020 -0700

    http2: add ConfigureTransports
    
    ConfigureTransport函数没有提供任何方法来获取它创建的http2 Transport因此无法配置传输参数如ReadIdleTimeout
英文:

There are two similar sounding functions in package golang.org/x/net/http2 with very different roles:

func ConfigureTransport  (t1 *http.Transport) error
func ConfigureTransports (t1 *http.Transport) (*Transport, error)

I think you are confusing the former with the latter.

From the issue tracker:
https://go-review.googlesource.com/c/net/+/264017

> The very similar names are unfortunate, but they'll sort next to each
> other in godoc and the pluralized ConfigureTransports hints at its
> purpose: it lets you configure both the http and http2 transports.

ConfigureTransports was only introduced exactly a year ago:

commit 08b38378de702b893ee869b94b32f833e2933bd2
Author: Damien Neil &lt;dneil@google.com&gt;
Date:   Tue Oct 20 12:34:04 2020 -0700

    http2: add ConfigureTransports
    
    The ConfigureTransport function doesn&#39;t provide any way to get at the
    http2 Transport it creates, making it impossible to configure transport
    parameters such as ReadIdleTimeout.

huangapple
  • 本文由 发表于 2021年10月20日 22:17:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/69647694.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定