Documentation
¶
Overview ¶
Package gointerrupt makes it easily to handle signals by canceling a context.
Use CtxPair to handle graceful shutdown of a program. See the example for a familiar net/http.Server setup.
Use SignalCtx to cancel contexts when custom signals are received.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CtxPair ¶ added in v1.0.0
type CtxPair struct {
// contains filtered or unexported fields
}
CtxPair provides 2 contexts which can be used to exit a program gracefully when shutdown signals are received by the process.
The graceful context will canceled when an interrupt signal is received. The harsh context will be canceled when a terminate signal is received.
The graceful context being canceled indicates graceful shutdown should begin, and the harsh indicates when it should end.
Example ¶
Demonstrates how a CtxPair can be used to gracefully shutdown a net/http.Server.
// Initialize a go interrupt context pair ctxPair := NewCtxPair(context.Background()) // Wait group is used to not exit the program until the HTTP server go // routine has completed var wg sync.WaitGroup server := http.Server{ Addr: ":5000", } // Run HTTP server wg.Add(1) go func() { if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { panic(err) } wg.Done() }() // Gracefully shutdown HTTP server when SIGINT received go func() { <-ctxPair.Graceful().Done() if err := server.Shutdown(ctxPair.Harsh()); err != nil { panic(err) } }() wg.Wait()
Output:
func NewCtxPair ¶ added in v1.0.0
NewCtxPair creates a new CtxPair which cancels the graceful context on SIGINT and harsh context on SIGTERM.
func (CtxPair) Graceful ¶ added in v1.0.0
Graceful returns the context which is canceled when graceful shutdown should begin.
func (CtxPair) GracefulSignalCtx ¶ added in v1.1.0
GracefulSignalCtx returns the underlying SignalCtx used for the graceful context.
func (CtxPair) Harsh ¶ added in v1.0.0
Harsh returns the context which is canceled when graceful shutdown should end.
func (CtxPair) HarshSignalCtx ¶ added in v1.1.0
HarshSignalCtx returns the underlying SignalCtx used by the harsh context.
type SignalCtx ¶ added in v1.0.0
type SignalCtx struct {
// contains filtered or unexported fields
}
SignalCtx is a context which cancels when a signal is received by the process
Example ¶
Shows how to setup a context.Context to cancel when custom signals are received by the process.
// Setup a context to cancel when a kill signal is sent to the process ctx := NewSignalCtx(context.Background(), syscall.SIGKILL) // Context will cancel when SIGKILL received <-ctx.Ctx().Done()
Output:
func NewSignalCtx ¶ added in v1.0.0
NewSignalCtx creates a SignalCtx for the specified signal
func (SignalCtx) Cancel ¶ added in v1.1.0
func (sigCtx SignalCtx) Cancel()
Cancel forcefully cancels the context.
Example ¶
Shows how to manually cancel a context.
// Setup a context which will cancel when the SIGKILL signal is received ctx := NewSignalCtx(context.Background(), syscall.SIGKILL) // Create a timer which will trigger in 10 seconds timer := time.NewTimer(time.Second * 10) go func() { <-timer.C log.Println("Timer went off! Cancelling context") ctx.Cancel() }() <-ctx.Ctx().Done() log.Println("Context was canceled")
Output: