In Chapter 1, Interacting with the Environment, the mechanism of how to implement graceful shutdown was presented. In this recipe, we will describe how to shut down the HTTP server and give it time to handle the existing clients.
Gracefully shutdown the HTTP server
How to do it...
- Open the console and create the folder chapter09/recipe11.
- Navigate to the directory.
- Create the file gracefully.go with the following content:
package main
import (
"context"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"time"
)
func main() {
mux := http.NewServeMux()
mux...