Sobes.tech
Senior

What happens to current requests without Graceful Shutdown when the process terminates?

sobes.tech AI

Answer from AI

Without Graceful Shutdown, when a process in Go terminates, current requests are interrupted immediately because the process ends without waiting for the processing to complete. This can lead to:

  • Data loss or incomplete request processing.
  • Violation of transaction integrity.
  • Errors on the client side.

Graceful Shutdown allows for the proper termination of all active requests, closing connections, and freeing resources before stopping the application. In Go, this is usually implemented through a context with a timeout and handling OS signals, for example:

func main() {
    srv := &http.Server{Addr: ":8080", Handler: myHandler}

    go func() {
        if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
            log.Fatalf("ListenAndServe(): %v", err)
        }
    }()

    quit := make(chan os.Signal, 1)
    signal.Notify(quit, os.Interrupt)
    <-quit

    ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    defer cancel()
    if err := srv.Shutdown(ctx); err != nil {
        log.Fatalf("Server Shutdown Failed:%+v", err)
    }
    log.Println("Server exited properly")
}

Thus, without graceful shutdown, requests are interrupted, but with it — they are terminated properly.

What happens to current requests without Graceful… - sobes.tech