mercredi 30 octobre 2019

Go - How to interrupt HTTP handler?

Say I have a http handler like this:

func ReallyLongFunction(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello World!")

        // run code that takes a long time here

       //more code here

})

Is there a way I can interrupt this function if the user refreshes the page or kills the request some other way without running the subsequent code and how would I do it?

I tried doing this:

notify := r.Context().Done()
        go func() {
            <-notify
            println("Client closed the connection")
            s.downloadCleanup()
            return
        }()

but the code after whenever I interrupt it still runs anyway.




Aucun commentaire:

Enregistrer un commentaire