使用 golang 在每个请求上实现上下文超时

Context timeout implementation on every request using golang

我正在尝试处理每个请求的上下文超时。我们有以下服务器结构:

流程概述:

Go Server:基本上,充当[反向代理]。2

Auth Server:检查请求身份验证。

Application Server:核心请求处理逻辑。

现在,如果授权服务器无法在规定的时间内处理请求,那么我想从内存中关闭 goroutine。

这是我尝试过的:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
req, _ := http.NewRequest("GET", authorizationServer, nil)
req.Header = r.Header
req.WithContext(ctx)
res, error := client.Do(req)
select {
case <-time.After(10 * time.Second):
    fmt.Println("overslept")
case <-ctx.Done():
    fmt.Println(ctx.Err()) // prints "context deadline exceeded"
}

在这里,如果请求没有在规定的时间内处理,上下文 returns 为“deadline exceeded”。但它会在超过指定时间的时间内继续处理该请求和 return 响应。那么,当超过时间时,如何停止请求流(goroutine)。

我还需要使用此代码在 60 秒内处理完整的请求:

var netTransport = &http.Transport{
    Dial: (&net.Dialer{
        Timeout: 60 * time.Second,
    }).Dial,
    TLSHandshakeTimeout: 60 * time.Second,
}
client := &http.Client{
    Timeout:   time.Second * 60,
    Transport: netTransport,
    CheckRedirect: func(req *http.Request, via []*http.Request) error {
        return http.ErrUseLastResponse
    },
}

那么我还需要任何单独的上下文实现吗?

注意 1:如果我们可以使用上下文管理 HTTP 服务器创建的每个请求(goroutine)的超时,那就太棒了。

您的代码中发生的事情非常正确并且表现符合预期。

您创建了一个 5 seconds 超时的上下文。您将其传递给 request 并发出该请求。假设在 2 秒内请求 returns。然后您执行 select 并等待 10 秒或等待上下文完成。上下文将始终在创建后的前 5 秒内完成,并且在每次结束时也会给出该错误。

context 与请求无关,除非事先取消,否则它将到达截止日期。当函数完成使用 defer.

时,您取消请求

在您的代码中,请求考虑了您的超时。但是 ctx.Err() 每次到达超时都会 return deadline exceeded 。因为那是 context 内部发生的事情。多次调用 ctx.Err() 将 return 出现相同的错误。

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

go func () {
    select {
    case <-time.After(10 * time.Second):
        fmt.Println("overslept")
    case <-ctx.Done():
        fmt.Println(ctx.Err()) // prints "context deadline exceeded"
    }
}()
req, _ := http.NewRequest("GET", authorizationServer, nil)
req.Header = r.Header
req = req.WithContext(ctx)
res, error := client.Do(req)

来自上下文文档:

// Err returns a non-nil error value after Done is closed. Err returns
// Canceled if the context was canceled or DeadlineExceeded if the
// context's deadline passed. No other values for Err are defined.
// After Done is closed, successive calls to Err return the same value.

在您的代码中,超时将始终达到并且不会取消,这就是您收到 DeadlineExceeeded 的原因。您的代码是正确的,除了 select 部分将阻塞,直到 10 秒通过或达到上下文超时。在您的情况下,总是达到上下文超时。

您应该检查 client.Do 调用 error return,不要担心这里的 context 错误。你是控制上下文的人。如果请求超时,您当然应该测试这种情况,然后会return编辑一个适当的错误供您验证。