superchan

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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)

View Source
var Log = log.Default()
View Source
var MakeSignalError = func(sig os.Signal) error {
	return fmt.Errorf("caught sig: %v", sig)
}
View Source
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.

View Source
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 Main

type Main = Superchan[os.Signal]

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 NewRaw

func NewRaw[T any](parent context.Context) *Superchan[T]

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]) IsDead

func (s *Superchan[T]) IsDead() bool

func (*Superchan[T]) SetDeferred

func (s *Superchan[T]) SetDeferred(f []func())

func (*Superchan[T]) Wait

func (s *Superchan[T]) Wait() error

Wait (blocks) for context to be cancelled, then run deferred funcs.

Prefer instead using s.DeferLast(wg.Done) with external waitgroup.

Jump to

Keyboard shortcuts

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