Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrNoExist = errors.New("machine: does not exit")
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache interface { // Get get a value by key and an error if one exists Get(key string) (interface{}, error) // Range executes the given function on the cache. If the function returns false, the iteration stops. Range(fn func(k string, val interface{}) bool) error // Set sets the key and value in the cache Set(key string, val interface{}) error // Del deletes the value by key from the map Del(key string) error }
Cache is a concurrency safe cache implementation used by Machine. A default sync.Map implementation is used if one isn't provided via WithCache
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 WithMiddlewares ¶
func WithMiddlewares(middlewares ...Middleware) GoOpt
WithMiddlewares wraps the gived function with the input middlewares.
func WithPID ¶
WithPID is a GoOpt that sets/overrides the process ID of the Routine. A random id is assigned if this option is not used.
func WithTimeout ¶
WithTimeout 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:
- throttled goroutines
- self-cancellable goroutines with context
- global-cancellable goroutines with context (see Cancel)
- goroutines have IDs and optional tags for easy debugging(see Stats)
- publish/subscribe to channels for passing messages between goroutines
- middlewares for wrapping/decorating functions
- panic recovery
- global concurrency safe cache
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.
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.
type Opt ¶
type Opt func(o *option)
Opt is a single option when creating a machine instance with New
func WithCache ¶
WithCache sets the in memory, concurrency safe cache. If not set, a default sync.Map implementation is used.
func WithMaxRoutines ¶
WithMaxRoutines throttles goroutines at the input number. It will panic if <= zero.
func WithSubscribeChannelBuffer ¶
WithSubscribeChannelBuffer sets the buffer length of the channel returned from a Routine subscribeTo
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{}) // 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{})) // Machine returns the underlying routine's machine instance Machine() *Machine }
Routine is an interface representing a goroutine