无论如何,关闭客户端请求在golang /杜松子酒? [英] Is there anyway to close client request in golang/gin?

查看:162
本文介绍了无论如何,关闭客户端请求在golang /杜松子酒?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用杜松子酒

有没有办法通知客户关闭请求连接,然后服务器处理程序可以做任何后台工作,而不让客户端等待连接?

  func测试(c * gin.Context){
c.String(200,ok)
//关闭客户端请求,然后执行一些作业,例如同步数据远程服务器。
//
}


解决方案

是的,你可以这么做。通过简单地从处理程序返回。而你想做的后台工作,你应该把它放在一个新的goroutine上。



请注意,连接和/或请求可以放回池中,但这是无关紧要的,客户会看到服务请求结束。



类似这样的:

  func测试(c * gin.Context){
c.String(200,ok)
//通过从此函数返回,响应将被发送到客户端
//并且连接到客户端将被关闭

//开始的goroutine将继续存在,当然还有:
go func(){
//此函数将继续执行...
}()
}

另请参阅:


Using gin framework.

Is there anyway to notify client to close request connection, then server handler can do any back-ground jobs without letting the clients to wait on the connection?

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

解决方案

Yes, you can do that. By simply returning from the handler. And the background job you want to do, you should put that on a new goroutine.

Note that the connection and/or request may be put back into a pool, but that is irrelevant, the client will see that serving the request ended. You achieve what you want.

Something like this:

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... 
    }()
}

Also see: Goroutine execution inside an http handler

这篇关于无论如何,关闭客户端请求在golang /杜松子酒?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆