How to send response to the client before executing the calculations that are not required in response

Issue

Can we send response(or write on user side)with statement like:

json.NewEncoder(w).Encode("some data")

in api before doing some calculation part which are not required in response but needed to store in database. I am thinking like we can give response in less time to the user and other part of function will be continue to work until the return statement.

Correct me if I am thinking in wrong direction.

Solution

One way would be to do your additional work which is not required for the response in another goroutine:

func someHandler(w http.ResponseWriter, r *http.Request) {
    go func() {
        // Do anything here, this won't delay the response
        // But don't touch the writer or request, as they may not be available here
    }()

    if err := json.NewEncoder(w).Encode("some data"); err != nil {
        log.Printf("Error sending response: %v", err)
    }
}

Note that in the launched gorotuine you can’t use the http.ResponseWriter nor the http.Request, as they are only valid to use until you return from your handler. If you need something from them, you must make a copy of the needed parts before you launch the goroutine.

If you want to complete the additional task before you return from the handler, you can still use a goroutine, and use a sync.WaitGroup to wait for it to complete and only then return from the handler. You may or may not flush the response:

func someHandler(w http.ResponseWriter, r *http.Request) {
    wg := &sync.WaitGroup{}
    wg.Add(1)
    go func() {
        defer wg.Done()

        // You may use the writer and request here
    }()

    if err := json.NewEncoder(w).Encode("some data"); err != nil {
        log.Printf("Error sending response: %v", err)
    }

    // Optionally you may flush the data written so far (icnluding HTTP headers)
    if flusher, ok := w.(http.Flusher); ok {
        flusher.Flush()
    }

    wg.Wait()
}

Note that here the goroutine is allowed to use the http.ResponseWriter and http.Request, because the handler does not return until the additional task is completed.

Answered By – icza

Answer Checked By – Candace Johnson (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.