go - 为什么在 go1.16 上为传输配置 ReadIdleTimeout HTTP/2 选项?
问题描述
从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) {}
Pernet/http2/transport.go
是ConfigureTransport
很久以前添加的。
// 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 中彼此相邻排序,并且复数的 ConfigureTransports 暗示了它的用途:它允许您配置 http 和 http2 传输。
ConfigureTransports
仅在一年前推出:
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.
推荐阅读
- python - 如何从没有formControlName的角度形式将输入传递给api?
- azure - 如何为 PostgreSql 单服务器的 Azure 数据库创建服务主体
- c# - 设备的两个不同版本的 C# SerialPort 问题
- python - tkinter:复选框不会在 for 循环中更新
- jpa - 此线程当前没有活动的外部管理事务
- javascript - 从类内部调用函数时,Javascript 类 this.x 不是函数
- r - 以类似命名的列为条件创建(许多)列
- blazor-webassembly - Blazor AOT dotnet.wasm 太大
- java - org.apache.cxf.interceptor.Interceptor 类转换异常
- python - 如何修复变量发送的不是好的值