iam*_*itk 7 httprequest go go-gin go-context
如果连接在 10 秒前关闭,如何取消进一步处理?
有c.Request.Context().Done()但找不到如何使用它的示例。
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
time.Sleep(10 * time.Second) // or some db operation
log.Print("Processing")
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run()
}
Run Code Online (Sandbox Code Playgroud)
bla*_*een 10
您可以异步运行长时间运行的操作,并将其发送到通道以发出完成信号。
然后,您阻止该完成通道并c.Request.Context().Done()使用select以下语句:
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
signal := make(chan struct{}, 1)
go longRunningOperation(signal)
select {
case <-signal:
close(signal) // remember to clean up after yourself
// move on, will print "Processing"
case <-c.Request.Context().Done():
// abort
return
}
log.Print("Processing")
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run()
}
func longRunningOperation(signal chan<- struct{}) {
time.Sleep(10 * time.Second)
signal <- struct{}{} // signal that this operation has finished
}
Run Code Online (Sandbox Code Playgroud)
这种方法的缺点是,长时间运行的操作本身将继续执行。
当程序的函数返回时,Goroutine 就会退出main,但实际服务器中的情况并非如此gin。所以这可能不是你想要的。
对于数据库操作,大多数 API 都需要一个context.Context参数,该参数可用于检测请求取消。因此,您可以将调用链向下传递c.Request.Context(),以确保异步长时间运行的操作在客户端断开连接时也会终止。
func Handler(c *gin.Context) {
signal := make(chan struct{}, 1)
go longRunningOperation(c.Request.Context(), signal)
...
}
func longRunningOperation(ctx context.Context, signal chan<- struct{}) {
if err := doSomethingContext(ctx); err != nil {
return
}
signal <- struct{}{} // signal that this operation has finished (successfully)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7637 次 |
| 最近记录: |