json - Go json.NewDecoder().Decode() 似乎不遵守上下文截止日期

标签 json http go timeout

我有一个设置了上下文截止日期的 Golang 程序。我正在发送 HTTP 请求,并希望在阅读正文时看到超出截止日期的错误。

似乎当我使用 ioutil.ReadAll 读取响应主体时,读取方法将被中断(?)并返回相应的错误(context.DeadlineExceeded) .

但是,如果我使用 json.NewDecoder(resp.Body).Decode 读取响应正文,则返回的错误为 nil(而不是 context.DeadlineExceeded)。我的完整代码如下。这是 json.NewDecoder(resp.Body).Decode 中的错误吗?

package main

import (
    "context"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
    "time"
)

var url string = "http://ip.jsontest.com/"

func main() {
    readDoesntFail()
    readFails()
}

type IpResponse struct {
    Ip string
}

func readDoesntFail() {
    ctx, _ := context.WithTimeout(context.Background(), time.Second*5)

    req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
    if err != nil {
        panic(err)
    }
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }

    ipResponse := new(IpResponse)
    time.Sleep(time.Second * 6)
    fmt.Println("before reading response body, context error is:", ctx.Err())
    err = json.NewDecoder(resp.Body).Decode(ipResponse)
    if err != nil {
        panic(err)
    }
    fmt.Println("Expected panic but there was none")
}

func readFails() {
    ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
    req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
    if err != nil {
        panic(err)
    }
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        panic(err)
    }

    time.Sleep(time.Second * 6)
    fmt.Println("before reading response body, context error is:", ctx.Err())
    _, err = ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Println("received expected error", err)
    }
}

最佳答案

net/http包可以使用缓冲区来处理请求。这意味着传入的响应正文可能会在您阅读之前部分或全部被读取和缓冲,因此过期的上下文可能不会阻止您完成阅读正文。而这正是发生的事情。

让我们修改您的示例以启动一个故意延迟响应(部分)的测试 HTTP 服务器:

ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    s := []byte(`{"ip":"12.34.56.78"}`)
    w.Write(s[:10])
    if f, ok := w.(http.Flusher); ok {
        f.Flush()
    }
    time.Sleep(time.Second * 6)
    w.Write(s[10:])
}))
defer ts.Close()
url = ts.URL

readDoesntFail()
readFails()

此测试服务器发送一个与 ip.jsontest.com 的响应类似的 JSON 对象。但它只发送 10 字节的主体,然后刷新它,然后在发送其余部分之前故意休眠 6 秒,“允许”客户端超时。

现在让我们看看调用 readDoesntFail() 会发生什么:

before reading response body, context error is: context deadline exceeded
panic: Get "http://127.0.0.1:38230": context deadline exceeded

goroutine 1 [running]:
main.readDoesntFail()
    /tmp/sandbox721114198/prog.go:46 +0x2b4
main.main()
    /tmp/sandbox721114198/prog.go:28 +0x93

Go Playground 上试试.

在您的示例中,json.Decoder.Decode() 读取已缓冲的数据,因此过期的上下文在这里不起作用。在我的示例中,json.Decoder.Decode() 尝试从连接中读取数据,因为数据还没有被缓冲(不可能是因为它还没有被发送),所以一旦上下文过期,从连接中进一步读取会返回超出截止日期的错误。

关于json - Go json.NewDecoder().Decode() 似乎不遵守上下文截止日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72804689/

相关文章:

git - 特别是在 git 推送到 Microsoft Azure 时出现 HTTP 413 错误

go ldflags 设置变量值不起作用

amazon-web-services - 使用 API 代替 SDK 可以吗?

Android: fragment 中的Http请求

arrays - golang 在多播 ip 上发送 json

.net - 如何将 JObject 反序列化为 .NET 对象

iphone - 我如何使用 Objective-C 中的 Web 服务?

json - 无法通过Logstash将JSON数据导入Elastic

php - 在 PHP 上用双引号解析 JSON

http - 去测试导入错误