首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么要为go1.16上的传输配置ReadIdleTimeout HTTP/2选项?

为什么要为go1.16上的传输配置ReadIdleTimeout HTTP/2选项?
EN

Stack Overflow用户
提问于 2021-10-20 14:17:07
回答 1查看 176关注 0票数 0

客户端,我们注意到

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

代码语言:javascript
复制
//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

代码语言:javascript
复制
//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.go在很长一段时间前就加入了ConfigureTransport

代码语言:javascript
复制
// 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选项?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-20 15:45:58

在包golang.org/x/net/http2 2中有两种类似的探测功能,它们的作用非常不同:

代码语言:javascript
复制
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是一年前才推出的:

代码语言:javascript
复制
commit 08b38378de702b893ee869b94b32f833e2933bd2
Author: Damien Neil <dneil@google.com>
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.
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69647694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档