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 is 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
// contains filtered or unexported fields
}
Grace manages Runners and handles errors.
func Init ¶
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
Click to show internal directories.
Click to hide internal directories.