Documentation ¶
Overview ¶
Package actor supports the simple creation of concurrent applications following the idea of actor models. The work to be done has to be defined as func() inside your public methods or functions and sent to the actor running in the background.
type Counter struct { counter int act *actor.Actor } func NewCounter() (*Counter, error) { act, err := actor.Go() if err != nil { return nil, err } c := &Counter{ counter: 0, act: act, } // Increment the counter every second. interval := 1 * time.Second c.act.Repeat(interval, func() { c.counter++ }) return c, nil } func (c *Counter) Incr() error { return c.act.DoAsync(func() { c.counter++ }) } func (c *Counter) Get() (int, error) { var counter int if err := c.act.DoSync(func() { counter = c.counter }); err != nil { return 0, err } return counter, nil } func (c *Counter) Stop() { c.act.Stop() }
The options for the constructor allow to pass a context for the Actor, the capacity of the Action queue, a recoverer function in case of an Action panic and a finalizer function when the Actor stops.
Index ¶
- type Action
- type Actor
- func (act *Actor) DoAsync(action Action) error
- func (act *Actor) DoAsyncWithContext(ctx context.Context, action Action) error
- func (act *Actor) DoSync(action Action) error
- func (act *Actor) DoSyncWithContext(ctx context.Context, action Action) error
- func (act *Actor) Done() <-chan struct{}
- func (act *Actor) Err() error
- func (act *Actor) IsDone() bool
- func (act *Actor) Repeat(interval time.Duration, action Action) (func(), error)
- func (act *Actor) RepeatWithContext(ctx context.Context, interval time.Duration, action Action) (func(), error)
- func (act *Actor) Stop()
- type Finalizer
- type Option
- type Recoverer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Actor ¶
type Actor struct {
// contains filtered or unexported fields
}
Actor introduces the actor model, where call simply are executed sequentially in a backend goroutine.
func (*Actor) DoAsync ¶
DoAsync sends the actor function to the backend goroutine and returns when it's queued.
func (*Actor) DoAsyncWithContext ¶ added in v0.3.0
DoAsyncWithContext send the actor function to the backend and returns when it's queued. A context allows to cancel the action or add a timeout.
func (*Actor) DoSyncWithContext ¶ added in v0.3.0
DoSyncWithContext executes the action and returns when it's done. A context allows to cancel the action or add a timeout.
func (*Actor) Done ¶ added in v0.3.0
func (act *Actor) Done() <-chan struct{}
Done returns a channel that is closed when the Actor terminates.
func (*Actor) IsDone ¶ added in v0.3.0
IsDone allows to simply check if the Actor is done in a select or if statement.
func (*Actor) Repeat ¶ added in v0.3.0
Repeat runs an Action in a given interval. It will be done asynchronously until the returned stopper function is called or the Actor is stopped.
func (*Actor) RepeatWithContext ¶ added in v0.3.0
func (act *Actor) RepeatWithContext( ctx context.Context, interval time.Duration, action Action) (func(), error)
RepeatWithContext runs an Action in a given interval. It will be done asynchronously until the context is canceled or timeout, the returned stopper function is called or the Actor is stopped.
type Finalizer ¶
Finalizer defines the signature of a function for finalizing the work of an Actor. The error is the one returned by the Actor.
type Option ¶
Option defines the signature of an option setting function.
func WithContext ¶
WithContext sets the context for the actor.
func WithFinalizer ¶
WithFinalizer sets a function for finalizing the work of a Loop.
func WithQueueCap ¶
WithQueueCap defines the channel capacity for actions sent to an Actor.
func WithRecoverer ¶ added in v0.3.0
WithRecoverer sets a function for recovering from a panic during executing an action.