Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var CancelBeforeDefer = true
CancelBeforeDefer determines if the context is cancelled before running deferred funcs.
Default is true so incoming signals will act the same as Cancel(err). (context is finished while deferred funcs are running)
var Log = log.Default()
var MakeSignalError = func(sig os.Signal) error { return fmt.Errorf("caught sig: %v", sig) }
var MaxWaitDuration = time.Second * 5
DoneWaitSeconds is the number of seconds to wait for deferred funcs to finish after context is done Only used in Wait() function.
var UseGoroutineDefer = true
UseGoroutineDefer changes the way deferred funcs are run when a Superchan is finished.
Default is true because it often safer to run deferred funcs in goroutines (we handle panic). See DeferFirst and DeferLast.
Functions ¶
func Flags ¶
func Flags()
Flags adds flags to the flag package for advanced superchan configuration at runtime.
func NewDouble ¶
func NewDouble[T any](parent context.Context, handler func(context.Context, **T) error, parallel bool) (*Superchan[T], *Superchan[T])
New Double Superchan for processing a channel, with handler func, defer funcs and cancellation.
Send to chctx.Ch() and cancel everything with chctx.Cancel(err).
Unhandled packets will be sent to chctx2.Ch(), but if buffer is full they are dropped.
Send to first, Handler processes, then Receive from second if handler returns non-nil. (chctx2.UpdateChan()) Reads from the channel and calls the handler func for every update.
The handler func can be nil, or a function called before sending to chctx2.
func NewMain ¶
func NewMain(parent context.Context, signals ...os.Signal) cancellable.Cancellable
New Superchan for signal handling with defer funcs and context cancellation one goroutine is started to handle signals calling cancel and defer funcs, see CancelBeforeDefer.
Note: New uses cancellable.CHANBUFSIZE (1000) for the channel buffer size (see SetChanSize).
For type assert, use x.(*superchan.Superchan[os.Signal])
func SetChanSize ¶
func SetChanSize(size int)
CHANBUFSIZE is the size of the channel buffer (cancellable package)
Types ¶
type Superchan ¶
type Superchan[T any] struct { cancellable.Chan[T] // contains filtered or unexported fields }
Superchan handles signals, is a cancellable.Chan[os.Signal] with defer funcs
Use as main context, for example:
var mainctx = New(context.Background(), os.Interrupt, syscall.SIGTERM)
func main() {
<-mainctx.Done()
log.Fatalln(context.Cause(mainctx))
}
func New ¶
func New[T any](parent context.Context, handler func(context.Context, T) error, parallel bool) *Superchan[T]
New Superchan for processing a channel, with defer funcs and cancellation.
Reads from the channel and calls the handler func for every update. Send to chctx.Ch(), cancel with chctx.Cancel(err).
The handler func should return nil error unless you want the context cancelled, (stopping the reader loop).
func (*Superchan[T]) Defer ¶
func (s *Superchan[T]) Defer(f ...func())
Defer a function to run when the context is cancelled. See CancelBeforeDefer.
Could be a call to shutdown an http server, for example ¶
Ordering: funcs added later are run first (see DeferLast for a single lastfunc)
func (*Superchan[T]) DeferFirst ¶
func (s *Superchan[T]) DeferFirst(f func())
DeferFirst is called first after context is finished.
Could be a call to http.Shutdown, for example
func (*Superchan[T]) DeferLast ¶
func (s *Superchan[T]) DeferLast(f func())
DeferLast is called last after context is finished.
Could be a call to wg.Done, for example
func (*Superchan[T]) GetDeferred ¶
func (s *Superchan[T]) GetDeferred() []func()
func (*Superchan[T]) SetDeferred ¶
func (s *Superchan[T]) SetDeferred(f []func())