作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
使用 gin框架。
有没有办法通知客户端关闭请求连接,然后服务器处理程序可以在不让客户端等待连接的情况下执行任何后台作业?
func Test(c *gin.Context) {
c.String(200, "ok")
// close client request, then do some jobs, for example sync data with remote server.
//
}
最佳答案
是的,你可以做到。通过简单地从处理程序返回。而你想做的后台工作,你应该把它放在一个新的 goroutine 上。
请注意,连接和/或请求可能会放回池中,但这无关紧要,客户端将看到服务请求已结束。你实现了你想要的。
像这样:
func Test(c *gin.Context) {
c.String(200, "ok")
// By returning from this function, response will be sent to the client
// and the connection to the client will be closed
// Started goroutine will live on, of course:
go func() {
// This function will continue to execute...
}()
}
关于go - 有没有办法关闭 golang/gin 中的客户端请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32470792/
我是一名优秀的程序员,十分优秀!