gointerrupt

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2020 License: MIT Imports: 4 Imported by: 0

README

GoDoc

Go Interrupt

Easily handle interrupts via context.

Table Of Contents

Overview

Makes it easy to gracefully shutdown programs.

Provides a graceful shutdown context.Context which is canceled when the first interrupt signal (SIGINT) is received. A harsh shutdown context.Context is also provided and is canceled when the first termination signal (SIGTERM) is received.

This allows processes to attempt to gracefully shutdown components by passing a context. The harsh shutdown signal can be used to kill a graceful shutdown processes as nicely as possible.

Examples

HTTP Server Example

This example shows how to use gointerrupt to gracefully shutdown a net/http.Server.

package main

import (
	"context"
	"net/http"
	"sync"
	
	"github.com/Noah-Huppert/gointerrupt"
)

func main() {
	// Initialize a go interrupt context pair
	ctxPair := gointerrupt.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()
}

Custom Signal Example

This example shows how to cancel a context with custom signals.

package main

import (
	"context"
	"net/http"
	"syscall"
	
	"github.com/Noah-Huppert/gointerrupt"
)

func main() {
	// Setup a context to cancel when a kill signal is sent to the process
	ctx := gointerrupt.NewSignalCtx(context.Background(), syscall.SIGKILL)
	
	// Context will cancel when SIGKILL received
	<-ctx.Ctx().Done()
}

Documentation

Overview

Easily handle signals by canceling a context.

Use CtxPair to handle graceful shutdown of a program. See the example for a farmiliar 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

func NewCtxPair(bkgrnd context.Context) CtxPair

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

func (pair CtxPair) Graceful() context.Context

Graceful returns the context which is canceled when graceful shutdown should begin.

func (CtxPair) Harsh added in v1.0.0

func (pair CtxPair) Harsh() context.Context

Harsh returns the context which is canceled when graceful shutdown should end.

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

func NewSignalCtx(bkgrnd context.Context, sig os.Signal) SignalCtx

NewSignalCtx creates a SignalCtx for the specified signal

func (SignalCtx) Ctx added in v1.0.0

func (sigCtx SignalCtx) Ctx() context.Context

Ctx returns the context which is canceled when a signal is received.

Jump to

Keyboard shortcuts

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