Categorygithub.com/vardius/shutdown
repositorypackage
1.0.2
Repository: https://github.com/vardius/shutdown.git
Documentation: pkg.go.dev

# README

⏲️ shutdown

Build Status Go Report Card codecov license

logo

shutdown - Simple shutdown signals handler with callback

πŸ“– ABOUT

Contributors:

Want to contribute ? Feel free to send pull requests!

Have problems, bugs, feature ideas? We are using the github issue tracker to manage them.

πŸ“š Documentation

For examples visit godoc#pkg-examples

For GoDoc reference, visit pkg.go.dev

🚏 HOW TO USE

🏫 Basic example

package main

import (
	"context"
	"fmt"
	"log"
	"net/http"
	"syscall"
	"time"

    "github.com/vardius/shutdown"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		fmt.Fprintf(w, "Hello!")
	})

	httpServer := &http.Server{
		Addr:    ":8080",
		Handler: mux,
	}

	stop := func() {
		ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
		defer cancel()

		if err := httpServer.Shutdown(ctx); err != nil {
			log.Printf("shutdown error: %v\n", err)
		} else {
			log.Printf("gracefully stopped\n")
		}
	}

	shutdown.GracefulStop(stop) // will block until shutdown signal is received
}

πŸ“œ License

This package is released under the MIT license. See the complete license in the package