Documentation ¶
Index ¶
- type Func
- type GoOpt
- type Machine
- func (p *Machine) Active() int
- func (p *Machine) Cancel()
- func (m *Machine) Close()
- func (m *Machine) Go(fn Func, opts ...GoOpt)
- func (m *Machine) Parent() *Machine
- func (m *Machine) Stats() *Stats
- func (m *Machine) Sub(opts ...Opt) *Machine
- func (p *Machine) Tags() []string
- func (p *Machine) Total() int
- func (m *Machine) Wait()
- type Middleware
- type Opt
- type PubSub
- type Routine
- type RoutineStats
- type Stats
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Func ¶
type Func func(routine Routine)
Func is the function passed into machine.Go. The Routine is passed into this function at runtime.
type GoOpt ¶
type GoOpt func(o *goOpts)
GoOpt is a function that configures GoOpts
func GoWithMiddlewares ¶ added in v0.0.8
func GoWithMiddlewares(middlewares ...Middleware) GoOpt
GoWithMiddlewares wraps the gived function with the input middlewares.
func GoWithPID ¶ added in v0.0.8
GoWithPID is a GoOpt that sets/overrides the process ID of the Routine. A random id is assigned if this option is not used.
func GoWithTags ¶ added in v0.0.8
GoWithTags is a GoOpt that adds an array of strings as "tags" to the Routine.
func GoWithTimeout ¶ added in v0.0.8
GoWithTimeout is a GoOpt that creates the Routine's context with the given timeout value
type Machine ¶
type Machine struct {
// contains filtered or unexported fields
}
Machine is a zero dependency runtime for managed goroutines. It is inspired by errgroup.Group with extra bells & whistles:
func (*Machine) Cancel ¶
func (p *Machine) Cancel()
Cancel cancels every goroutines context within the machine instance & it's children
func (*Machine) Close ¶ added in v0.0.5
func (m *Machine) Close()
Close completely closes the machine instance & all of it's children
func (*Machine) Go ¶
Go calls the given function in a new goroutine.
The first call to return a non-nil error who's cause is machine.Cancel cancels the context of every job. All errors that are not of type machine.Cancel will be returned by Wait.
func (*Machine) Parent ¶ added in v0.0.8
Parent returns the parent Machine instance if it exists and nil if not.
func (*Machine) Sub ¶ added in v0.0.8
Sub returns a nested Machine instance that is dependent on the parent machine's context.
type Middleware ¶
Middleware is a function that wraps/modifies the behavior of a machine.Func.
func After ¶ added in v0.0.4
func After(afterFunc func(routine Routine)) Middleware
After exectues the afterFunc after the main goroutine exits.
func Before ¶ added in v0.0.4
func Before(beforeFunc func(routine Routine)) Middleware
Before exectues the beforeFunc before the main goroutine is executed.
func Cron ¶
func Cron(ticker *time.Ticker) Middleware
Cron is a middleware that execute the function every time the ticker ticks until the goroutine's context cancels
func Decider ¶ added in v0.0.4
func Decider(deciderFunc func(routine Routine) bool) Middleware
Decider exectues the deciderFunc before the main goroutine is executed. If it returns false, the goroutine won't be executed.
func PanicRecover ¶ added in v0.0.9
func PanicRecover() Middleware
PanicRecover wraps a goroutine with a middleware the recovers from panics.
type Opt ¶
type Opt func(o *option)
Opt is a single option when creating a machine instance with New
func WithChildren ¶ added in v0.0.8
func WithMaxRoutines ¶
WithMaxRoutines throttles goroutines at the input number. It will panic if <= zero.
func WithMiddlewares ¶
func WithMiddlewares(middlewares ...Middleware) Opt
WithMiddlewares wraps every goroutine function executed by the machine with the given middlewares. Middlewares can be added to individual goroutines with GoWithMiddlewares
func WithParent ¶ added in v0.0.8
func WithPubSub ¶ added in v0.0.5
WithPubSub sets the pubsub implementation for the machine instance. An inmemory implementation is used if none is provided.
type PubSub ¶ added in v0.0.5
type PubSub interface { // Publish publishes the object to the channel by name Publish(channel string, obj interface{}) error // Subscribe subscribes to the given channel Subscribe(ctx context.Context, channel string, handler func(obj interface{})) error Close() }
PubSub is used to asynchronously pass messages between routines.
type Routine ¶
type Routine interface { // Context returns the goroutines unique context that may be used for cancellation Context() context.Context // Cancel cancels the context returned from Context() Cancel() // PID() is the goroutines unique process id PID() int // Tags() are the tags associated with the goroutine Tags() []string // Start is when the goroutine started Start() time.Time // Duration is the duration since the goroutine started Duration() time.Duration // Publish publishes the object to the given channel Publish(channel string, obj interface{}) error // Subscribe subscribes to a channel and executes the function on every message passed to it. It exits if the goroutines context is cancelled. Subscribe(channel string, handler func(obj interface{})) error // Machine returns the underlying routine's machine instance Machine() *Machine }
Routine is an interface representing a goroutine
type RoutineStats ¶
type RoutineStats struct { PID int `json:"pid"` Start time.Time `json:"start"` Duration time.Duration `json:"duration"` Tags []string `json:"tags"` }
RoutineStats holds information about a single goroutine
type Stats ¶
type Stats struct { Tags []string `json:"tags"` TotalRoutines int `json:"totalRoutines"` ActiveRoutines int `json:"activeRoutines"` Routines []RoutineStats `json:"routines"` TotalChildren int `json:"totalChildren"` HasParent bool `json:"hasParent"` }
Stats holds information about goroutines