Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly close a request and continue processing it on the background

Tags:

http

go

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.

like image 802
nbari Avatar asked Aug 29 '16 09:08

nbari


1 Answers

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.

like image 118
icza Avatar answered Oct 19 '22 23:10

icza