【问题标题】:Is there anyway to close client request in golang/gin?无论如何在golang/gin中关闭客户端请求?
【发布时间】:2015-12-04 21:44:07
【问题描述】:

使用gin 框架。

是否有通知客户端关闭请求连接,然后服务器处理程序可以在不让客户端等待连接的情况下执行任何后台作业?

func Test(c *gin.Context) {
        c.String(200, "ok")
        // close client request, then do some jobs, for example sync data with remote server.
        //
}

【问题讨论】:

    标签: go go-gin


    【解决方案1】:

    是的,你可以这样做。通过简单地从处理程序返回。而你想做的后台工作,你应该把它放在一个新的 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... 
        }()
    }
    

    另见:Goroutine execution inside an http handler

    【讨论】:

      猜你喜欢
      • 2021-08-10
      • 1970-01-01
      • 1970-01-01
      • 2021-07-20
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 2020-02-10
      相关资源
      最近更新 更多