shutdown

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2019 License: Apache-2.0 Imports: 11 Imported by: 0

README

shutdown

Graceful shutdown package for Go services with built-in healthcheck for persistent dependencies.

Usage

Shutdown

Everything what depends on the graceful shutdown (ex.: long-time file writes, database connection, http requests), most implement the ServiceDescriptor interface.

package example
type ServiceDescriptor interface {
	Stop() error
	Ping() error
	Reconnect() error
}

Also the constructor requires a logging instance with the following interface implementation.

package example
type Log interface {
	Errorf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
}

Create a new instance of the shutdown and adding new services and call the GracefulExit handler.

package example

handler := shutdown.NewHandler(&testLog{t})
handler.Add("service1", "", shutdown.Init, &service1)
wg := &sync.WaitGroup{}
shutdown.GracefuleExit(handler, wg)

wg.Wait()

Determine the sequence of the shutdown when we add them into the list.

package example

handler := shutdown.NewHandler(&testLog{t})
handler.Add("service1", "", shutdown.Init, &service1)
handler.Add("service2", "service1", shutdown.Before, &service2)
handler.Add("service3", "service2", shutdown.After, &service3)
wg := &sync.WaitGroup{}
shutdown.GracefuleExit(handler, wg)
Important

Healthcheck not fully implemented yet!

Documentation

Index

Constants

View Source
const (
	StatusServing int32 = iota
	StatusNotServing
)
View Source
const (
	Init = iota
	Before
	After
)

Variables

This section is empty.

Functions

func GracefulExit

func GracefulExit(handler *Handler, wg *sync.WaitGroup)

Types

type Handler

type Handler struct {
	Healthcheck *Healthcheck
	// contains filtered or unexported fields
}

func NewHandler

func NewHandler(l Log) *Handler

func (*Handler) Add

func (h *Handler) Add(label string, labelPos string, pos int, stop ServiceDescriptor)

func (*Handler) GenerateSeq

func (h *Handler) GenerateSeq(label string, pos int) int

func (*Handler) HealthcheckWithExistingConnections

func (h *Handler) HealthcheckWithExistingConnections()

func (*Handler) InitHealthcheck added in v0.1.1

func (h *Handler) InitHealthcheck()

func (*Handler) SetupHealthcheck

func (h *Handler) SetupHealthcheck(hc *Healthcheck)

func (*Handler) Stop

func (h *Handler) Stop() error

type Healthcheck

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

func NewHC

func NewHC(config *HealthcheckConfig) *Healthcheck

func (*Healthcheck) Add

func (h *Healthcheck) Add(name string, hcs HealthcheckDescriptor)

func (*Healthcheck) Policy

func (h *Healthcheck) Policy() *HealthcheckConfig

func (*Healthcheck) Serve

func (h *Healthcheck) Serve()

func (*Healthcheck) Status

func (h *Healthcheck) Status() (int32, error)

type HealthcheckConfig

type HealthcheckConfig struct {
	RetryAmount   int `json:"retry_amount"`
	Interval      int `json:"interval"`       // in millisecond
	CheckInterval int `json:"check_interval"` // in millisecond
}

func DefaultHealthcheckConfig

func DefaultHealthcheckConfig() *HealthcheckConfig

type HealthcheckDescriptor

type HealthcheckDescriptor interface {
	Ping() error
}

type Log

type Log interface {
	Errorf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Debugf(format string, args ...interface{})
	Warnf(format string, args ...interface{})
}

type Ping

type Ping func() error

type ServiceDescriptor

type ServiceDescriptor interface {
	Stop() error
	Ping() error
}

Jump to

Keyboard shortcuts

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