graceful

package module
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2023 License: MIT Imports: 7 Imported by: 1

README

graceful shutdown

Go Reference Go Report Card

Example:
package main

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

	"github.com/egnd/go-toolbox/graceful"
)

func main() {
	// init listener and subscribe to os signals
	ctx, cancel := graceful.Init(context.Background(), os.Interrupt, syscall.SIGTERM)
	defer cancel()

	serv := http.Server{
		BaseContext: func(l net.Listener) context.Context { return ctx },
	}

	// start http server and register its stop callback
	graceful.Register(serv.ListenAndServe, func() error {
		return serv.Shutdown(ctx)
	})

	// start other service without stop callback
	graceful.Register(func() error {
		ticker := time.NewTicker(time.Second)
		defer ticker.Stop()

		select {
		case <-ticker.C:
		case <-ctx.Done():
			return nil
		}

		return nil
	})

	// wait for os signal or error from one of the working services
	if err := graceful.Wait(); err != nil {
		log.Fatal(err)
	}

	log.Println("service is correctly stopped")
}

Documentation

Overview

Package graceful has tools for making graceful shutdown.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrCallbackCnt    = errors.New("invalid callbacks count, required only start and stop callbacks") //nolint:revive
	ErrStartFuncType  = errors.New("invalid start func type")                                         //nolint:revive
	ErrEmptyStartFunc = errors.New("empty start func")                                                //nolint:revive
	ErrStopFuncType   = errors.New("invalid stop func type")                                          //nolint:revive
	ErrEmptyStopFunc  = errors.New("empty stop func")                                                 //nolint:revive
	ErrEmptyListener  = errors.New("undefined global listener")                                       //nolint:revive
)

Functions

func Init

func Init(ctx context.Context, signals ...os.Signal) (context.Context, context.CancelFunc)

Init subscribes to os signals and registers cancel func and group context.

func Register

func Register(funcs ...interface{}) error

Register registers some service to listen and its "stop" function.

func Wait

func Wait() error

Wait registers executor for "stop" callbacks and blocks until all registered callbacks will end.

Types

type Callback

type Callback = func()

Callback is a common function type.

type CallbackWithError

type CallbackWithError = func() error

CallbackWithError is a common function type with error.

type Listener

type Listener struct {
	// contains filtered or unexported fields
}

Listener listens errorgroup goroutines.

func NewListener

func NewListener(ctx context.Context, signals ...os.Signal) (*Listener, context.Context, context.CancelFunc)

NewListener create listener for os signals and registers cancel func and group context.

func (*Listener) Register

func (l *Listener) Register(funcs ...interface{})

Register registers some service to listen and its "stop" function.

func (*Listener) Wait

func (l *Listener) Wait() error

Wait registers executor for "stop" callbacks and blocks until all registered callbacks will end.

Jump to

Keyboard shortcuts

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