If it's an important business task which needs to take place even if the 
machine crashes, then maybe you want to post a request into a persistent 
message queue (e.g. NATS, Kafka) before returning to the user.

The task can then be picked up by a separate worker process.

On Friday, 7 July 2023 at 08:58:03 UTC+1 alex-coder wrote:

> Yes, the question is set too broadly, it is necessary to specify the 
> context, 
> thank you.
>
> четверг, 6 июля 2023 г. в 02:33:18 UTC+3, ben...@gmail.com: 
>
>> For simple things, you can fire up a goroutine to do the "something else" 
>> after the request finishes. For example, I've used this before to kick off 
>> sending an email to a customer in the background (you'll want to 
>> handle/report errors somehow though):
>>
>> package main
>>
>> import (
>> "fmt"
>> "log"
>> "net/http"
>> "time"
>> )
>>
>> func main() {
>> http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
>> fmt.Fprint(w, "sending email in the background")
>> go sendEmail()
>> // response is sent, but sendEmail goroutine continues
>> })
>> log.Print("listening on http://localhost:8080";)
>> http.ListenAndServe(":8080", nil)
>> }
>>
>> func sendEmail() {
>> log.Printf("would send an email here")
>> time.Sleep(time.Second)
>> log.Printf("done sending email")
>> }
>>
>> On Thursday, July 6, 2023 at 2:03:47 AM UTC+12 alex-coder wrote:
>>
>> Hi All !
>>
>> So, http server looks like is a request / response processing.
>> But in case it is nesessary to do something else after the response has 
>> been sent to the client, how to do it properly ?
>> Is there any example to read ?
>>
>> Thank you.
>>
>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/b0f2eb59-a375-46de-bdb4-85b3c9c75cf9n%40googlegroups.com.

Reply via email to