Golang HTTP Server Graceful Shutdown

package main

import (
“context”
“errors”
“log”
“net/http”
“os”
“os/signal”
“syscall”
“time”
)

func createChannel() (chan os.Signal, func()) {
stopCh := make(chan os.Signal, 1)
signal.Notify(stopCh, os.Inter…


This content originally appeared on DEV Community and was authored by DEV Community

Sunday Snippet #6 golang HTTP server graceful shutdown

package main

import (
    "context"
    "errors"
    "log"
    "net/http"
    "os"
    "os/signal"
    "syscall"
    "time"
)

func createChannel() (chan os.Signal, func()) {
    stopCh := make(chan os.Signal, 1)
    signal.Notify(stopCh, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)

    return stopCh, func() {
        close(stopCh)
    }
}

func start(server *http.Server) {
    log.Println("application started")
    if err := server.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
        panic(err)
    } else {
        log.Println("application stopped gracefully")
    }
}

func shutdown(ctx context.Context, server *http.Server) {
    ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
    defer cancel()

    if err := server.Shutdown(ctx); err != nil {
        panic(err)
    } else {
        log.Println("application shutdowned")
    }
}

func main() {
    log.SetFlags(log.Lshortfile)
    s := &http.Server{}
    go start(s)

    stopCh, closeCh := createChannel()
    defer closeCh()
    log.Println("notified:", <-stopCh)

    shutdown(context.Background(), s)
}

runtime:

$ go run main.go
main.go:24: application started
^Cmain.go:50: notified: interrupt # ctrl+c
main.go:28: application stopped gracefully
main.go:39: application shutdowned
$


This content originally appeared on DEV Community and was authored by DEV Community


Print Share Comment Cite Upload Translate Updates
APA

DEV Community | Sciencx (2022-02-26T17:00:13+00:00) Golang HTTP Server Graceful Shutdown. Retrieved from https://www.scien.cx/2022/02/26/golang-http-server-graceful-shutdown/

MLA
" » Golang HTTP Server Graceful Shutdown." DEV Community | Sciencx - Saturday February 26, 2022, https://www.scien.cx/2022/02/26/golang-http-server-graceful-shutdown/
HARVARD
DEV Community | Sciencx Saturday February 26, 2022 » Golang HTTP Server Graceful Shutdown., viewed ,<https://www.scien.cx/2022/02/26/golang-http-server-graceful-shutdown/>
VANCOUVER
DEV Community | Sciencx - » Golang HTTP Server Graceful Shutdown. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/26/golang-http-server-graceful-shutdown/
CHICAGO
" » Golang HTTP Server Graceful Shutdown." DEV Community | Sciencx - Accessed . https://www.scien.cx/2022/02/26/golang-http-server-graceful-shutdown/
IEEE
" » Golang HTTP Server Graceful Shutdown." DEV Community | Sciencx [Online]. Available: https://www.scien.cx/2022/02/26/golang-http-server-graceful-shutdown/. [Accessed: ]
rf:citation
» Golang HTTP Server Graceful Shutdown | DEV Community | Sciencx | https://www.scien.cx/2022/02/26/golang-http-server-graceful-shutdown/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.