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: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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