sigctx

package
v0.0.0-...-0bfd988 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2018 License: MIT Imports: 5 Imported by: 4

README

sigctx

Package sigctx provides contexts for graceful shutdown.

The sigctx package provides a context that terminates when it receives a SIGINT or SIGTERM. This provides a convenient mechanism for triggering graceful application shutdown.

sigctx.New returns a ctx.C, which implements the ubiquitous ctx.Doner interface. It fires when either SIGINT or SIGTERM is caught.

Examples

import (
    "log"
    "github.com/SentimensRG/ctx/sigctx"
)

func main() {
    ctx := sigctx.New()  // returns a regular context.Context

    <-ctx.Done()  // will unblock on SIGINT and SIGTERM
    log.Println("exiting.")
}

sigctx.Tick can be used to react to streams of signals. For example, you can implement a graceful shutdown attempt, followed by a forced shutdown.

import (
    "log"
    "github.com/SentimensRG/ctx/sigctx"
    "github.com/SentimensRG/ctx"
)

func main() {
    t := sigctx.Tick()
    d, cancel := ctx.WithCancel(ctx.Background())

    go func() {
        defer cancel()

        go func() {
            // business logic goes here
        }()

        <-t
        log.Println("attempting graceful shutdown - press Ctrl + c again to force quit")
        go func() {
            defer cancel()
            // cleanup logic goes here
        }()

        <-t
        log.Println("forcing close")
    }()

    <-d.Done()
}

Documentation

Overview

Package sigctx provides a context that expires when a SIGINT or SIGTERM is received.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() ctx.C

New signal-bound ctx.C that terminates when either SIGINT or SIGTERM is caught.

func Tick

func Tick() <-chan struct{}

Tick returns a channel that recvs each time a either SIGINT or SIGTERM are caught.

Types

This section is empty.

Jump to

Keyboard shortcuts

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