Documentation ¶
Overview ¶
Package mvr is a minimal viable runtime that provides a top-level context cancelled by Unix signals, graceful shutdown by waiting for all registered goroutines to terminate before exitting the application, simple goroutine pool, and asynchronous logger.
Index ¶
- Constants
- func Async(fn func() error) <-chan error
- func Cancel()
- func Context() context.Context
- func Done() <-chan struct{}
- func Err() error
- func ForEachString(args []string, fn func(string) error) (jobs chan func() error)
- func Go(fn func())
- func OnCancel(timeout time.Duration, fn func(context.Context))
- func Parallel(numThreads int, jobs <-chan func() error) (<-chan error, context.CancelFunc)
- func ParallelCtx(parent context.Context, numThreads int, jobs <-chan func() error) (<-chan error, context.CancelFunc)
- func Run(main func() int)
Constants ¶
const DefaultCancellationTimeout = 10 * time.Second
DefaultCancellationTimeout is the default time given to a cancellation function to complete.
Variables ¶
This section is empty.
Functions ¶
func Async ¶
Async starts the given function in a separate goroutine, and returns a channel where the returned error will be placed when the given function completes.
func ForEachString ¶
ForEachString creates a channel of jobs, where each job is an application of the given function to a string from the given list. The result is suitable for Parallel() function.
func Go ¶
func Go(fn func())
Go starts the given function in a new goroutine registered with the runtime.
func OnCancel ¶
OnCancel registers a function to be called when the top-level context gets cancelled. The timeout parameter specifies the time for the function to complete its task; if set to 0 the timeout will be DefaultCancellationTimeout. The supplied function will be called with a context that is cancelled when the timeout expires.
func Parallel ¶
func Parallel(numThreads int, jobs <-chan func() error) (<-chan error, context.CancelFunc)
Parallel invokes job functions from the given queue, sending all non-nil errors to the returned channel. The error channel gets closed when all the jobs from the input queue are either complete or cancelled. The returned cancellation function should always be called at some point to free the associated resources.
func ParallelCtx ¶
func ParallelCtx(parent context.Context, numThreads int, jobs <-chan func() error) (<-chan error, context.CancelFunc)
ParallelCtx invokes job functions from the given queue, sending all non-nil errors to the returned channel. The error channel gets closed when all the jobs from the input queue are either complete or cancelled. The returned cancellation function should always be called at some point to free the associated resources. The function also terminates when the given context is cancelled.
func Run ¶
func Run(main func() int)
Run takes the application entry point function as a parameter, and executes it in a separate goroutine. The return value of the main function will be passed over to os.Exit(). The main function cancels the top-level context upon exit. The Run() function itself never returns.
Types ¶
This section is empty.