Documentation
¶
Overview ¶
Package parallel provides an implementation of map/reduce using channels and go routines.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrOptInvalidValueQueue indicates the option supplied to set the size of the inbound job queue is invalid ErrOptInvalidValueQueue = errors.New("invalid option value: queue") // ErrOptInvalidValueContext indicates the option supplied to set the context for the parallel operation is invalid ErrOptInvalidValueContext = errors.New("invalid option value: context") // ErrCancelledMapper indicates that the mapper option has been reused after being cancelled ErrCancelledMapper = errors.New("mapper was already cancelled") )
Functions ¶
func OptMappers ¶
func OptMappers(sz int, init func(int) interface{}, destroy func(interface{})) (Option, CancelFunc)
OptMappers can be used to control the number of go routines used to run mappers (defaults to runtime.NumCPU()) and supply `init` and `destroy` hooks for the routines
func Parallel ¶
func Parallel(value interface{}, mapper func(init interface{}, job interface{}) interface{}, reducer func(previous interface{}, current interface{}) interface{}, then func(final interface{}, err error), opts ...Option) (chan interface{}, error)
Parallel performs a map/reduce using go routines and channels
value: is the initial value of the reducer i.e. the first `previous` for the reducer mapper: functions are called in multiple goroutines, they consume jobs and returns `current` for the reducer reducer: functions are called synchronously and returns the value for `previous` for the next invocation then: receives the last output produced by the reducer opts: control context, queue sizes, goroutine pool & `init` values for mappers
The returned channel is the job queue and must be closed by the caller when all jobs have been submitted
Types ¶
type ErrTrappedPanic ¶
type ErrTrappedPanic struct { Panic interface{} Stack []byte }
ErrTrappedPanic wraps an underlying panic and call stack for a panic that was trapped during mapping or reduction
func (ErrTrappedPanic) Error ¶
func (e ErrTrappedPanic) Error() string
type Option ¶
type Option func(*options) error
Option encapsulate all available options for the Parallel operation
func OptContext ¶
OptContext can be used to supply a context for a Parallel operation, typically to affect timeout and provide cancellation