mediocre-go-lib/mrun/mrun.go

118 lines
3.1 KiB
Go
Raw Permalink Normal View History

2019-06-15 23:13:00 +00:00
// Package mrun provides the ability to register callback hooks on Components,
// as well as some convenience functions which allow using a context as a
2019-05-18 18:36:53 +00:00
// wait-group.
//
// Hooks
//
2019-06-15 23:13:00 +00:00
// Hooks are registered onto Components and later called in bulk. mrun will take
2019-05-18 18:36:53 +00:00
// into account the order Hooks are registered, including Hooks within a
2019-06-15 23:13:00 +00:00
// Component's children (see mcmp package), and execute them in the same order
2019-05-18 18:36:53 +00:00
// they were registered. For example:
//
// newHook := func(i int) mrun.Hook {
// return func(context.Context) error {
// fmt.Println(i)
// return nil
// }
// }
//
2019-06-15 23:13:00 +00:00
// cmp := new(mcmp.Component)
// mrun.InitHook(cmp, newHook(0))
2019-05-18 18:36:53 +00:00
//
2019-06-15 23:13:00 +00:00
// cmpChild := cmp.Child("child")
// mrun.InitHook(cmpChild, newHook(1))
2019-05-18 18:36:53 +00:00
//
2019-06-15 23:13:00 +00:00
// mrun.InitHook(cmp, newHook(2))
// mrun.Init(context.Background(), cmp) // prints "0", "1", then "2"
2019-05-18 18:36:53 +00:00
//
2018-12-06 04:40:46 +00:00
package mrun
import (
"context"
2018-12-06 04:40:46 +00:00
"errors"
)
type futureErr struct {
doneCh chan struct{}
err error
}
func newFutureErr() *futureErr {
return &futureErr{
doneCh: make(chan struct{}),
}
}
func (fe *futureErr) get(cancelCh <-chan struct{}) (error, bool) {
select {
case <-fe.doneCh:
return fe.err, true
case <-cancelCh:
return nil, false
}
}
func (fe *futureErr) set(err error) {
fe.err = err
close(fe.doneCh)
}
type threadCtxKey int
2018-12-06 04:40:46 +00:00
2019-05-18 18:36:53 +00:00
// 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.
2019-02-24 22:38:05 +00:00
func WithThreads(ctx context.Context, n uint, fn func() error) context.Context {
// I dunno why this would happen, but it wouldn't actually hurt anything
if n == 0 {
return ctx
}
oldFutErrs, _ := ctx.Value(threadCtxKey(0)).([]*futureErr)
2019-02-24 22:38:05 +00:00
futErrs := make([]*futureErr, len(oldFutErrs), len(oldFutErrs)+int(n))
copy(futErrs, oldFutErrs)
2018-12-06 04:40:46 +00:00
2019-02-24 22:38:05 +00:00
for i := uint(0); i < n; i++ {
futErr := newFutureErr()
futErrs = append(futErrs, futErr)
go func() {
futErr.set(fn())
}()
}
2019-02-24 22:38:05 +00:00
return context.WithValue(ctx, threadCtxKey(0), futErrs)
2018-12-06 04:40:46 +00:00
}
// ErrDone is returned from Wait if cancelCh is closed before all threads have
// returned.
var ErrDone = errors.New("Wait is done waiting")
2019-02-24 22:38:05 +00:00
// 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
2019-02-24 22:38:05 +00:00
// may have returned already when Wait is called, and not all go-routines need
2019-05-18 18:36:53 +00:00
// to be from the same WithThreads call.
2018-12-06 04:40:46 +00:00
//
// 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
2019-02-24 22:38:05 +00:00
// multiple times.
func Wait(ctx context.Context, cancelCh <-chan struct{}) error {
futErrs, _ := ctx.Value(threadCtxKey(0)).([]*futureErr)
2018-12-06 04:40:46 +00:00
for _, futErr := range futErrs {
err, ok := futErr.get(cancelCh)
if !ok {
return ErrDone
} else if err != nil {
return err
}
}
return nil
}