gpt4 book ai didi

go - 每次请求后关闭 http2 客户端连接

转载 作者:行者123 更新时间:2023-12-01 21:14:45 26 4
gpt4 key购买 nike

我想在每次请求后关闭 http2 客户端连接(比如在 http1.1 中禁用保持连接)。我尝试添加标题 Connection: close但这给了
http2:客户端连接不可用错误。

我尝试使用 request.Close = true 以及在文档中指定,但仍然出现错误。

以下是我正在使用的代码片段:

func main() {

enableCompression := false

transport2 := &http2.Transport{
DisableCompression: !enableCompression,
AllowHTTP: true,
DialTLS: func(network, addr string, cfg *tls.Config) (net.Conn, error) {
return net.Dial(network, addr)
},
}

// Create http client
client := http.Client{
Transport: transport2,
Timeout: time.Duration(1 * time.Second),
}

var err error
var request *http.Request
if request, err = createRequest(); err == nil {
request.Close = true
var response *http.Response

if response, err = client.Do(request); err == nil {
fmt.Println(response)
} else {
fmt.Println("error: ", err)
}
}
}

func createRequest() (request *http.Request, err error) {
if request, err = http.NewRequest("GET", "http://0.0.0.0:1010/hello/sayHello", nil); err == nil {
fmt.Println(request)
}
return
}

这给出了错误:
http2:客户端连接不可用(等待 header 时超出 Client.Timeout)

我错过了什么?

最佳答案

HTTP/2 不允许这种行为。

https://httpwg.org/specs/rfc7540.html#rfc.section.8.1.2

8.1.2.2. Connection-Specific Header Fields

HTTP/2 does not use the Connection header field to indicate connection-specific header fields; in this protocol, connection-specific metadata is conveyed by other means. [...]

[...]

This means that an intermediary transforming an HTTP/1.x message to HTTP/2 will need to remove any header fields nominated by the Connection header field, along with the Connection header field itself. Such intermediaries SHOULD also remove other connection-specific header fields, such as Keep-Alive, Proxy-Connection, Transfer-Encoding, and Upgrade, even if they are not nominated by the Connection header field.



也可以看看
  • https://github.com/golang/go/issues/22441
  • https://github.com/golang/go/issues/17717
  • 关于go - 每次请求后关闭 http2 客户端连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58360811/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com