machine
packagemoduleThis package is not in the latest version of its module.
Details
Validgo.mod file
The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.
Redistributable license
Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.
Tagged version
Modules with tagged versions give importers more predictable builds.
Stable version
When a project reaches major version v1 it is considered stable.
- Learn more about best practices
Repository
Links
README¶
Machine

import "github.com/autom8ter/machine"
ctx, cancel := context.WithCancel(context.Background())defer cancel()m := machine.New(ctx,machine.WithMaxRoutines(10),machine.WithMiddlewares(machine.PanicRecover()),)defer m.Close()channelName := "acme.com"const publisherID = "publisher"// start another goroutine that publishes to the target channel every second for 5 seconds OR the routine's context cancelsm.Go(func(routine machine.Routine) {fmt.Printf("%v | streaming msg to channel = %v stats = %s\n", routine.PID(), channelName, routine.Machine().Stats().String())// publish message to channelroutine.Publish(channelName, "hey there bud!")}, machine.GoWithTags("publish"),machine.GoWithPID(publisherID),machine.GoWithTimeout(5*time.Second),machine.GoWithMiddlewares(// run every second until context cancelsmachine.Cron(time.NewTicker(1*time.Second)),),)// start a goroutine that subscribes to all messages sent to the target channel for 3 seconds OR the routine's context cancelsm.Go(func(routine machine.Routine) {routine.Subscribe(channelName, func(obj interface{}) bool {fmt.Printf("%v | subscription msg received! channel = %v msg = %v stats = %s\n", routine.PID(), channelName, obj, m.Stats().String()) return true })}, machine.GoWithTags("subscribe"),machine.GoWithTimeout(3*time.Second),)m.Wait()Machine is a zero dependency library for highly concurrent Go applications. It is inspired byerrgroup.Group with extra bells & whistles:
throttled goroutines
self-cancellable goroutines with
Contextglobal-cancellable goroutines with context (seeCancel)
goroutines have IDs and optional tags for easy debugging (seeStats)
nativepublish/subscribe implementation for broadcasting messages to active goroutines
middlewares for wrapping/decorating functions
"sub" machines for creating a dependency tree between groups of goroutines
goroutine leak prevention
native pprof & golang execution tracer integration
Use Cases
Machine is meant to be completely agnostic and dependency free- its use cases are expected to be emergent.Really, it can be usedanywhere goroutines are used.
Highly concurrent and/or asynchronous applications include:
gRPC streaming servers
websocket servers
pubsub servers
reverse proxies
cron jobs
custom database/cache
ETL pipelines
log sink
filesystem walker
code generation
Examples
All examples are < 500 lines of code(excluding code generation)
Documentation¶
Index¶
- Constants
- type Func
- type GoOpt
- type Machine
- func (p *Machine) Active() int
- func (p *Machine) Cancel()
- func (m *Machine) CancelRoutine(id string)
- func (m *Machine) Close()
- func (m *Machine) Go(fn Func, opts ...GoOpt) string
- func (m *Machine) HasRoutine(id string) bool
- func (m *Machine) ID() string
- func (m *Machine) Parent() *Machine
- func (m *Machine) PubSub() pubsub.PubSub
- 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
- func After(afterFunc func(routine Routine)) Middleware
- func Before(beforeFunc func(routine Routine)) Middleware
- func Cron(ticker *time.Ticker) Middleware
- func Decider(deciderFunc func(routine Routine) bool) Middleware
- func PanicRecover() Middleware
- func While(deciderFunc func(routine Routine) bool) Middleware
- type Opt
- func WithChildren(children ...*Machine) Opt
- func WithClosers(closers ...func()) Opt
- func WithDeadline(deadline time.Time) Opt
- func WithID(id string) Opt
- func WithMaxRoutines(max int) Opt
- func WithMiddlewares(middlewares ...Middleware) Opt
- func WithPubSub(pubsub pubsub.PubSub) Opt
- func WithTags(tags ...string) Opt
- func WithTimeout(to time.Duration) Opt
- func WithValue(key, val interface{}) Opt
- type Routine
- type RoutineStats
- type Stats
Examples¶
Constants¶
const DefaultMaxRoutines = 1000Variables¶
This section is empty.
Functions¶
This section is empty.
Types¶
typeFunc¶
type Func func(routineRoutine)
Func is the function passed into machine.Go. The Routine is passed into this function at runtime.
typeGoOpt¶
type GoOpt func(o *goOpts)
GoOpt is a function that configures GoOpts
funcGoWithDeadline¶added inv0.2.0
GoWithDeadline is a GoOpt that creates the Routine's context with the given deadline.
funcGoWithMiddlewares¶added inv0.0.8
func GoWithMiddlewares(middlewares ...Middleware)GoOpt
GoWithMiddlewares wraps the gived function with the input middlewares.
funcGoWithPID¶added inv0.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.
funcGoWithTags¶added inv0.0.8
GoWithTags is a GoOpt that adds an array of strings as "tags" to the Routine.
funcGoWithTimeout¶added inv0.0.8
GoWithTimeout is a GoOpt that creates the Routine's context with the given timeout value
funcGoWithValues¶added inv0.1.1
func GoWithValues(key, val interface{})GoOptGoWithValues adds the k/v to the routine's root context. It can be retrieved with routine.Context().Value()
typeMachine¶
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:
funcNew¶
New Creates a new machine instance with the given root context & options
Example¶
package mainimport ("context""fmt""github.com/autom8ter/machine""time")func main() {ctx, cancel := context.WithCancel(context.Background())defer cancel()m := machine.New(ctx,machine.WithMaxRoutines(10),machine.WithMiddlewares(machine.PanicRecover()),)defer m.Close()channelName := "acme.com"const publisherID = "publisher"// start another goroutine that publishes to the target channel every second for 5 seconds OR the routine's context cancelsm.Go(func(routine machine.Routine) {fmt.Printf("%v | streaming msg to channel = %v stats = %s\n", routine.PID(), channelName, routine.Machine().Stats().String())// publish message to channelroutine.Publish(channelName, "hey there bud!")}, machine.GoWithTags("publish"),machine.GoWithPID(publisherID),machine.GoWithTimeout(5*time.Second),machine.GoWithMiddlewares(// run every second until context cancelsmachine.Cron(time.NewTicker(1*time.Second)),),)// start a goroutine that subscribes to all messages sent to the target channel for 3 seconds OR the routine's context cancelsm.Go(func(routine machine.Routine) {routine.Subscribe(channelName, func(obj interface{}) bool {fmt.Printf("%v | subscription msg received! channel = %v msg = %v stats = %s\n", routine.PID(), channelName, obj, m.Stats().String())return true})}, machine.GoWithTags("subscribe"),machine.GoWithTimeout(3*time.Second),)// start a goroutine that subscribes to the channel until the publishing goroutine exits OR the routine's context cancelsm.Go(func(routine machine.Routine) {routine.Subscribe(channelName, func(obj interface{}) bool {fmt.Printf("%v | subscription msg received! channel = %v msg = %v stats = %s\n", routine.PID(), channelName, obj, m.Stats().String())return m.HasRoutine(publisherID)})}, machine.GoWithTags("subscribeUntil"))m.Wait()}func (*Machine)Cancel¶
func (p *Machine) Cancel()
Cancel cancels every goroutines context within the machine instance & it's children
func (*Machine)CancelRoutine¶added inv0.2.0
CancelRoutine cancels the context of the active routine with the given id if it exists.
func (*Machine)Close¶added inv0.0.5
func (m *Machine) Close()
Close completely closes the machine's pubsub instance & all of it's closer functions. It also closes all of it's child machines(if they exist)
func (*Machine)Go¶
Go calls the given function in a new goroutine and returns the goroutine's unique idit is passed information about the goroutine at runtime via the Routine interface
func (*Machine)HasRoutine¶added inv0.2.0
HasRoutine returns true if the machine has a active routine with the given id
func (*Machine)Parent¶added inv0.0.8
Parent returns the parent Machine instance if it exists and nil if not.
func (*Machine)Sub¶added inv0.0.8
Sub returns a nested Machine instance that is dependent on the parent machine's context.It inherits the parent's pubsub implementation & middlewares if none are providedSub machine's do not inherit their parents max routine setting
typeMiddleware¶
Middleware is a function that wraps/modifies the behavior of a machine.Func.
funcAfter¶added inv0.0.4
func After(afterFunc func(routineRoutine))Middleware
After exectues the afterFunc after the main goroutine exits.
funcBefore¶added inv0.0.4
func Before(beforeFunc func(routineRoutine))Middleware
Before exectues the beforeFunc before the main goroutine is executed.
funcCron¶
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
funcDecider¶added inv0.0.4
func Decider(deciderFunc func(routineRoutine)bool)Middleware
Decider exectues the deciderFunc before the main goroutine is executed.If it returns false, the goroutine won't be executed.
funcPanicRecover¶added inv0.0.9
func PanicRecover()Middleware
PanicRecover wraps a goroutine with a middleware the recovers from panics.
funcWhile¶added inv0.2.0
func While(deciderFunc func(routineRoutine)bool)Middleware
While is a middleware that will execute the Func while deciderFunc() returns true or the context cancels.
typeOpt¶
type Opt func(o *option)
Opt is a single option when creating a machine instance with New
funcWithChildren¶added inv0.0.8
WithChildren sets the machine instances children
funcWithClosers¶added inv0.7.2
func WithClosers(closers ...func())Opt
WithClosers makes the Machine instance execute the given closers before it closes
funcWithDeadline¶added inv0.2.0
WithDeadline is an Opt that creates the Machine's context with the given deadline.
funcWithID¶added inv0.2.0
WithID sets the machine instances unique id. If one isn't provided, a unique id will be assigned
funcWithMaxRoutines¶
WithMaxRoutines throttles goroutines at the input number. It will panic if <= zero.
funcWithMiddlewares¶
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
funcWithPubSub¶added inv0.0.5
WithPubSub sets the pubsub implementation for the machine instance. An inmemory implementation is used if none is provided.
funcWithTimeout¶
WithTimeout is an Opt that creates the Machine's context with the given timeout value
typeRoutine¶
type Routine interface {// Context returns the goroutines unique context that may be used for cancellationContext()context.Context// Cancel cancels the context returned from Context()Cancel()// PID() is the goroutines unique process idPID()string// Tags() are the tags associated with the goroutineTags() []string// Start is when the goroutine startedStart()time.Time// Duration is the duration since the goroutine startedDuration()time.Duration// Publish publishes the object to the given channelPublish(channelstring, 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(channelstring, handlerpubsub.Handler, options ...pubsub.SubOpt)error// TraceLog logs a message within the goroutine execution tracer. ref:https://golang.org/pkg/runtime/trace/#example_TraceLog(messagestring)// Machine returns the underlying routine's machine instanceMachine() *Machine}Routine is an interface representing a goroutine
typeRoutineStats¶
type RoutineStats struct {PIDstring `json:"pid"`Starttime.Time `json:"start"`Durationtime.Duration `json:"duration"`Tags []string `json:"tags"`}RoutineStats holds information about a single goroutine
typeStats¶
type Stats struct {IDstring `json:"id"`Tags []string `json:"tags"`TotalRoutinesint `json:"totalRoutines"`ActiveRoutinesint `json:"activeRoutines"`Routines []RoutineStats `json:"routines"`TotalChildrenint `json:"totalChildren"`HasParentbool `json:"hasParent"`TotalMiddlewaresint `json:"totalMiddlewares"`Timeouttime.Duration `json:"timeout"`Deadlinetime.Time `json:"deadline"`Children []*Stats `json:"children"`}Stats holds information about goroutines
Source Files¶
Directories¶
| Path | Synopsis |
|---|---|
examplesmodule | |
chat/clientcommand | |
chat/servercommand | |
croncommand | |
reverse-proxycommand | |