In Go, implementing a graceful shutdown for HTTP servers allows you to handle ongoing requests and clean up resources before the server stops accepting new requests. This is particularly useful for scenarios like deploying new versions of your application, performing maintenance, or managing server health. Below is an example of how to achieve a graceful shutdown using Go's `http.Server` and the context package.
package main
import (
"context"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
// Create a new HTTP server
srv := &http.Server{
Addr: ":8080",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}),
}
// Create a channel to listen for OS interrupt signals
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
// Start the server in a goroutine
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("ListenAndServe(): %v", err)
}
}()
// Wait for an interrupt signal
<-stop
log.Println("Shutting down server...")
// Create a context with a timeout for the shutdown
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
// Shutdown the server gracefully
if err := srv.Shutdown(ctx); err != nil {
log.Fatalf("Server Shutdown Failed:%+v", err)
}
log.Println("Server exited gracefully")
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?