作者:yhonmen | 来源:互联网 | 2023-02-07 09:11
我正在尝试处理每个请求的上下文超时.我们有以下服务器结构:
流程概述:
Go Server:基本上,充当[反向代理].2
Auth服务器:检查请求身份验证.
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"
}
在此处,如果请求未在规定时间内处理,则上下文将返回" 超出截止日期 ".但它继续处理该请求并在指定时间内返回响应.那么当超过时间时,如何停止请求流(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)的超时,那将是非常棒的.
1> Bogdan Iulia..:
您的代码中发生的事情是非常正确的,并且行为符合预期.
您可以使用5 seconds
超时创建上下文.您将其传递给request
并发出请求.假设请求在2秒内返回.然后你做一个select
并等待10秒或等待上下文完成.上下文将始终在创建后的最初5秒内完成,并且每次到达结尾时也会给出该错误.
这context
与请求无关,除非事先取消,否则它将在截止日期前到达.您在功能完成使用后取消请求defer
.
在您的代码中,请求会考虑您的超时.但每次达到超时时它ctx.Err()
都会返回deadline exceeded
.因为这就是内部发生的事情context
.多次调用ctx.Err()将返回相同的错误.
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
.您的代码是正确的,除了选择部分将阻止,直到10秒通过或达到上下文超时.在您的情况下,始终达到上下文超时.
您应该检查呼叫error
返回的内容,client.Do
而不必担心context
此处的错误.你是控制上下文的人.如果请求超时,您当然应该测试一个案例,那么将返回一个正确的错误供您验证.