Documentation ¶
Overview ¶
Package mrun provides the ability to register callback hooks on Components, as well as some convenience functions which allow using a context as a wait-group.
Hooks ¶
Hooks are registered onto Components and later called in bulk. mrun will take into account the order Hooks are registered, including Hooks within a Component's children (see mcmp package), and execute them in the same order they were registered. For example:
newHook := func(i int) mrun.Hook { return func(context.Context) error { fmt.Println(i) return nil } } cmp := new(mcmp.Component) mrun.InitHook(cmp, newHook(0)) cmpChild := cmp.Child("child") mrun.InitHook(cmpChild, newHook(1)) mrun.InitHook(cmp, newHook(2)) mrun.Init(context.Background(), cmp) // prints "0", "1", then "2"
Index ¶
- Variables
- func AddHook(cmp *mcmp.Component, key interface{}, hook Hook)
- func Init(ctx context.Context, cmp *mcmp.Component) error
- func InitHook(cmp *mcmp.Component, hook Hook)
- func Shutdown(ctx context.Context, cmp *mcmp.Component) error
- func ShutdownHook(cmp *mcmp.Component, hook Hook)
- func TriggerHooks(ctx context.Context, cmp *mcmp.Component, key interface{}) error
- func TriggerHooksReverse(ctx context.Context, cmp *mcmp.Component, key interface{}) error
- func Wait(ctx context.Context, cancelCh <-chan struct{}) error
- func WithThreads(ctx context.Context, n uint, fn func() error) context.Context
- type Hook
Constants ¶
This section is empty.
Variables ¶
var ErrDone = errors.New("Wait is done waiting")
ErrDone is returned from Wait if cancelCh is closed before all threads have returned.
Functions ¶
func AddHook ¶
AddHook registers a Hook under a typed key. The Hook will be called when TriggerHooks is called with that same key. Multiple Hooks can be registered for the same key, and will be called sequentially when triggered.
Hooks will be called with whatever Context is passed into TriggerHooks.
func InitHook ¶
InitHook registers the given Hook to run when Init is called. This is a special case of AddHook.
As a convention Hooks running on the init event should block only as long as it takes to ensure that whatever is running can do so successfully. For short-lived tasks this isn't a problem, but long-lived tasks (e.g. a web server) will want to use the Hook only to initialize, and spawn off a go-routine to do their actual work. Long-lived tasks should set themselves up to shutdown on the shutdown event (see ShutdownHook).
func Shutdown ¶
Shutdown runs all Hooks registered using ShutdownHook in the reverse order in which they were registered. This is a special case of TriggerHooks.
func ShutdownHook ¶
ShutdownHook registers the given Hook to run when Shutdown is called. This is a special case of AddHook.
See InitHook for more on the relationship between Init(Hook) and Shutdown(Hook).
func TriggerHooks ¶
TriggerHooks causes all Hooks registered with AddHook on the Component under the given key to be called in the order they were registered. The given Context is passed into all Hooks being called.
If any Hook returns an error no further Hooks will be called and that error will be returned.
If the Component has children (see the mcmp package), and those children have Hooks registered under this key, then their Hooks will be called in the expected order. See package docs for an example.
func TriggerHooksReverse ¶
TriggerHooksReverse is the same as TriggerHooks except that registered Hooks are called in the reverse order in which they were registered.
func Wait ¶
Wait blocks until all go-routines spawned using WithThreads on the passed in Context (and its predecessors) have returned. Any number of the go-routines may have returned already when Wait is called, and not all go-routines need to be from the same WithThreads call.
If any of the thread functions returned an error during its runtime Wait will return that error. If multiple returned an error only one of those will be returned. TODO: Handle multi-errors better.
If cancelCh is not nil and is closed before all threads have returned then this function stops waiting and returns ErrDone.
Wait is safe to call in parallel, and will return the same result if called multiple times.
func WithThreads ¶
WithThreads spawns n go-routines, each of which executes the given function. The returned Context tracks these go-routines, and can then be passed into the Wait function to block until the spawned go-routines all return.