如何正确关闭请求并继续在后台处理它

问题描述 投票:0回答:2

对于传入的 HTTP 请求,我必须使用 202 Accepted 状态代码进行响应,同时继续在后台处理有效负载。例如,这就是我目前正在做的事情:

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
函数:

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

如果我想回复“200 OK”,我注意到我可以简单地返回,例如:

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

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

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

或者只需编写 header 并接下来调用 goroutine 就足够了。

http go
2个回答
8
投票

从处理程序返回就足够了,也是应该做的。引用自

http.Handler

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

请注意,最后的

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

此外,返回时,如果未设置 HTTP 标头,则会自动设置

200 OK
。因此,如果您想要
202 Accepted
,以下是最低要求:

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

只需确保从处理程序返回后,不要在并发 Goroutine 中使用

http.ResponseWriter
httpRequest
值,因为它们可能会被重用,因此您甚至不应该尝试读取它们。


0
投票

当你在 goroutine 中使用 Context 时,这不能很好地工作,你的 Context 将在返回 http 响应代码时被取消。

我得到的错误是

get identity: get credentials: request canceled, context canceled

为了解决这个问题,我必须将其包裹在上下文中,让它一直存在到 goroutine 结束。

context.WithoutCancel(r.Context())
© www.soinside.com 2019 - 2024. All rights reserved.