graceful

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 2, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

Graceful

What's graceful-shutdown

Example:

    package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/oxygenpay/oxygen/pkg/graceful"
)

func main() {
	srv := &http.Server{}

	go func() {
		err := srv.ListenAndServe()
		if err != nil {
			// example of "force" shutdown
			graceful.ShutdownNow()
		}
	}()

	// you can add as many callbacks as you want. 
	// they will be shut down in descending order (from last to first)

	// add sample callback #1
	graceful.AddCallback(srv.Close)

	// add sample callback #2
	graceful.AddCallback(func() error {
		log.Println("shutting down")
		return nil
	})

	// sample custom error handler
	graceful.ExecOnError(func(err error) {
		fmt.Printf(err.Error())
	})

	// like wg.Wait(), this operation blocks current goroutine
	graceful.WaitShutdown()
}

Documentation

Overview

Package graceful contains API for working with graceful application shutdown.

Application starts listening for SIGINT or SIGTERM signals and handles them properly.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrTimeoutExceeded is returned when the application fails to shutdown for a given period of time.
	ErrTimeoutExceeded = errors.New("failed to perform graceful shutdown: timeout exceeded")

	// ErrForceShutdown is returned when the user or operating system is sending SIGINT or SIGTERM
	// for the application being is graceful-shutdown state.
	ErrForceShutdown = errors.New("failed to perform graceful shutdown: force shutdown occurred")
)

Functions

func AddCallback

func AddCallback(fn ShutdownFunc)

AddCallback registers a callback for execution before shutdown.

func ExecOnError

func ExecOnError(cb func(err error))

ExecOnError executes the given handler when shutdown callback returns any error.

func IsShuttingDown

func IsShuttingDown() bool

func ShutdownNow

func ShutdownNow()

ShutdownNow sends event to initiate graceful shutdown.

func WaitShutdown

func WaitShutdown() error

WaitShutdown waits for application shutdown.

If the user or operating system interrupts the graceful shutdown, ErrForceShutdown is returned. If applications fails to shutdown for a given period of time, ErrTimeoutExceeded is returned.

Types

type ShutdownFunc

type ShutdownFunc func() error

ShutdownFunc is a callback-type for registering callbacks before application shutdown.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL