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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With