登录
首页 >  Golang >  Go问答

为什么在 go1.16 上要添加 HTTP/2 的 ReadIdleTimeout 配置选项?

来源:stackoverflow

时间:2024-02-10 23:30:25 208浏览 收藏

本篇文章给大家分享《为什么在 go1.16 上要添加 HTTP/2 的 ReadIdleTimeout 配置选项?》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。

问题内容

从 google api golang 客户端,我们注意到

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

而在此文件中 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) {}

根据 net/http2/transport.goconfiguretransport 是很早之前添加的。

// 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 {

为什么要在 go1.16 上为传输配置 readidletimeout http/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 中的 other 和复数的configuretransports 暗示了它的 目的:它允许您配置 http 和 http2 传输。

configuretransports 是在一年前才推出的:

commit 08b38378de702b893ee869b94b32f833e2933bd2
Author: Damien Neil <[email protected]>
Date:   Tue Oct 20 12:34:04 2020 -0700

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

以上就是《为什么在 go1.16 上要添加 HTTP/2 的 ReadIdleTimeout 配置选项?》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>