如何正确关闭请求并继续在后台处理 [英] How to properly close a request and continue processing it on the background

查看:67
本文介绍了如何正确关闭请求并继续在后台处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于传入的HTTP请求,我必须以 202接受状态码进行响应,同时继续在后台处理有效负载.出于示例目的,这是我当前正在做的事情:

For an incoming HTTP request, I had to respond with a 202 Accepted status code, while continue processing the payload in the background. for example purposes this is what I am currently doing:

package main

import (
    "fmt"
    "log"
    "net/http"
    "time"

    "github.com/nbari/violetear"
)

func sleep() {
    time.Sleep(3 * time.Second)
    fmt.Println("done...")
}

func index(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusAccepted)
    go sleep()
}

func main() {
    router := violetear.New()
    router.HandleFunc("*", index)

    http.Handle("/", router)
    log.Fatal(http.ListenAndServe(":8080", router))
}

基本上,在处理程序上,我只使用 WriteHeader ,然后在goroutine中调用 sleep 函数:

Basically, on the handler I just use WriteHeader and later a call the the sleep function within a goroutine:

func index(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusAccepted)
    go sleep()
}

如果我想回答"200 OK",我注意到我可以简单地返回,例如:

In case I would like to respond with "200 OK", I notice that I can simple return, for example:

func index(w http.ResponseWriter, r *http.Request) {
    go sleep()
    return
}

因此想知道我是否应该总是回去,我想关闭:

Therefore wondering if I should return always I want to close:

func index(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusAccepted)
    go sleep()
    return
}

或者只编写标题,然后调用goroutine就足够了.

Or by just writing the header and next calling the goroutine is enough.

推荐答案

从处理程序返回就足够了,应该这样做.引用自 http.Handler :

Returning from the handler is sufficient and is what should be done. Quoting from http.Handler:

返回信号表明请求已完成;在ServeHTTP调用完成之后或与之同时使用ResponseWriter或从Request.Body中读取是无效的.

Returning signals that the request is finished; it is not valid to use the ResponseWriter or read from the Request.Body after or concurrently with the completion of the ServeHTTP call.

请注意,最后的 return 语句不是必需的,您可以简单地省略它.当执行程序的最后一条语句执行时,执行将从处理程序返回,执行过程不会等待从函数启动的goroutine完成.(请注意,延迟的语句将在执行之前执行,但这里没有任何内容.)

Note that the final return statement is not necessary, you can simply omit it. Execution returns from the handler when its last statement is executed, execution does not wait for goroutines started from the function to complete. (Note that deferred statements would be executed prior, but you don't have any here.)

返回时,如果未设置HTTP标头,则会自动设置 200 OK .因此,如果您要 202接受,则以下是最低要求:

Also when returning, if HTTP headers are not set, 200 OK will be set automatically. So if you want 202 Accepted, the following is the minimal required:

func index(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusAccepted)
    go sleep()
}

确保从处理程序返回后,并发goroutine中不会在并发goroutine中使用 http.ResponseWriter httpRequest 值,因为它们可能会被重用,所以您甚至都不应该尝试阅读它们.

Just make sure you don't use the http.ResponseWriter and httpRequest values in the concurrent goroutine after you return from the handler as they may be reused, so you should not even attempt to read them.

这篇关于如何正确关闭请求并继续在后台处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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