1

ginフレームワークの使用。

リクエスト接続を閉じるようにクライアントに通知する方法はありますか?サーバーハンドラーは、クライアントに接続を待機させずにバックグラウンドジョブを実行できますか?

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

1 に答える 1

3

はい、できます。ハンドラーから単に戻ることによって。実行したいバックグラウンド ジョブは、新しいゴルーチンに配置する必要があります。

接続および/またはリクエストがプールに戻される可能性があることに注意してください。あなたはあなたが望むものを達成します。

このようなもの:

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

参照: http ハンドラ内でのゴルーチンの実行

于 2015-09-09T05:13:56.833 に答える