Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrShutdownSignalReceived is used when a shutdown signal is received. // It will cause a graceful shutdown. ErrShutdownSignalReceived = errors.New("shutdown signal received") // ErrShutdownRequestReceived is used when a shutdown is initiated without a signal. // It will cause a graceful shutdown. ErrShutdownRequestReceived = errors.New("shutdown request received") // ErrImmediateShutdownSignalReceived is used when a shutdown signal is received for the second time. // It will cause an immediate shutdown. ErrImmediateShutdownSignalReceived = errors.New("immediate shutdown signal received") )
Functions ¶
func DefaultErrorHandler ¶
DefaultErrorHandler returns a standard error handler that will log errors and trigger a shutdown.
Types ¶
type ErrHandlerFn ¶
ErrHandlerFn is an error handler used to handle errors returned from Runners. Returns true if a graceful shutdown should be triggered as a result of the error.
type Grace ¶
type Grace struct { // ErrHandler is the active error handler grace will pass errors to. ErrHandler ErrHandlerFn // LogFn is the log function that grace will use. LogFn func(format string, v ...interface{}) // contains filtered or unexported fields }
Grace manages Runners and handles errors.
func Init ¶
func Init(ctx context.Context, options ...InitOption) *Grace
Init returns a new and initialised instance of Grace.
Example ¶
package main import ( "context" "fmt" "github.com/tomwright/grace" "time" ) func main() { g := grace.Init(context.Background()) fmt.Println("Starting") g.Run(grace.RunnerFunc(func(ctx context.Context) error { for { select { case <-ctx.Done(): fmt.Println("Graceful shutdown initiated") // Fake a 3 second shutdown for i := 3; i >= 0; i-- { fmt.Printf("Shutting down in %d\n", i) time.Sleep(time.Second * time.Duration(i)) } return nil case <-time.After(time.Second): fmt.Println("Hello") } } })) go func() { <-time.After(time.Millisecond * 2500) fmt.Println("Triggering shutdown") g.Shutdown() }() fmt.Println("Started") <-g.Context().Done() g.Wait() fmt.Println("Shutdown") }
Output: Starting Started Hello Hello Triggering shutdown Graceful shutdown initiated Shutting down in 3 Shutting down in 2 Shutting down in 1 Shutting down in 0 Shutdown
type InitOption ¶ added in v0.1.2
type InitOption interface { // Apply allows you to apply options to the grace instance as it is being initialised. Apply(g *Grace) }
InitOption allows you to modify the way grace is initialised.
func InitOptionFn ¶ added in v0.1.2
func InitOptionFn(fn func(g *Grace)) InitOption
InitOptionFn returns an InitOption that applies the given function.
type RecoveredPanicError ¶ added in v0.1.2
type RecoveredPanicError struct { // Err is the recovered error. Err interface{} // Stack is the stack trace from the recover. Stack []byte }
RecoveredPanicError represents a recovered panic.
func (RecoveredPanicError) Error ¶ added in v0.1.2
func (p RecoveredPanicError) Error() string
Error returns the recovered panic as a string
Click to show internal directories.
Click to hide internal directories.