dongwaner1367 2016-08-29 09:27
浏览 426
已采纳

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

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

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

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
}

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

  • 写回答

1条回答 默认 最新

  • doudi8829 2016-08-29 09:47
    关注

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

    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.

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

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

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题