Skip to content

koron-go/sigctx

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

sigctx - Signal Context

GoDoc Actions/Go Go Report Card

sigctx is a context of signal listening loop.

In Go 1.16 and later, singal.NotifyContext can be used to achieve the same effect as sigctx.WithCancelSignal, so it is recommended to use signal.NotifyContext instead.

Example

Using WithCancelSignal(), it is similar to context.WithCancel()

import (
    "os"
    "github.com/koron-go/sigctx"
)

ctx, cancel := sigctx.WithCancelSignal(ctx.Background(), os.Interrupt)
defer cancel() // call cancel() at least once.

// TODO: work with the ctx.

Or try *sigctx.Sigctx to control in detail.

import (
    "os"
    "github.com/koron-go/sigctx"
)

// create signal listening loop
sx := sigctx.New(os.Interrupt)

// start to listen signals and get its context.
// the ctx will be done when receive signals.
// if parent context be done, it terminates listening and the ctx.
ctx := sx.Start(context.Background()).Context()

// TODO: work with the ctx.

// stop to listen signals. it make the ctx done.
sx.Stop()

About

Experimental signal + context

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors