sigctx

package module
v0.0.0-...-05e1d0f Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2017 License: MIT Imports: 6 Imported by: 1

README

sigctx

Signal for context.Context.

GoDoc Build Status Coverage Status Go Report Card

Description

The package sigctx were developed to easily bind signals and context.Context.

We use a combination of signals and context.Context often. It is mainly processing to context cancel after receiving the signal. Using this package makes it easy to do it.
Example: signal_cancel.go

By the way, if you are using the context, have you ever wanted to send the signal to all the goroutines? You are lucky 🎉 With this package, it is possible to do something close to that.
Example: notify_signal.go

Do you want to run the above two methods at the same time? Of course you can do it.
However, it becomes complicated if you mistake order. (Perhaps this problem may be fixed in the future.) But it will be very simple if the order of invocation is correct!!
I recommend comparing mistake_ctx_order.go and correct_ctx_order.go.

Install

go get github.com/Code-Hex/sigctx

Contributing

Welcome!!
I'm waiting for pull requests or reporting issues.

License

MIT

Author

CodeHex

Documentation

Overview

Package sigctx were developed to easily bind signals and context.Context. For example, When receive a signal, you can notify received signals to all goroutines using context. Or when you receive a signal, you can invoke a context cancel.

Index

Constants

This section is empty.

Variables

View Source
var Canceled = errors.New("signal received")

Canceled is the error returned by Context.Err when the context is canceled.

Functions

func Recv

func Recv(ctx context.Context) <-chan struct{}

Recv send channel when a process receives a signal.

You can use Recv(ctx) only if you pass a signalCtx context as an argument. If you want to use Recv(ctx) please wrap context.Context so that the signalCtx context is the last.

func Signal

func Signal(ctx context.Context) (os.Signal, error)

Signal returns os.Signal and error.

If the context passed to the argument is a signalCtx context, it returns the received os.Signal. Otherwise it returns nil and error message.

func WithCancelSignals

func WithCancelSignals(parent context.Context, sig ...os.Signal) context.Context

WithCancelSignals returns a copy of parent which the value can context cancel by signals.

Context cancellation is executed after receiving the signal.

func WithSignals

func WithSignals(parent context.Context, sig ...os.Signal) context.Context

WithSignals returns a copy of parent which the value can notify signals by Recv(ctx).

When the process receive signals passed to the argument, it sends the information of the received signal to all the goroutines.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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