actor

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 8, 2023 License: BSD-3-Clause Imports: 4 Imported by: 0

README

Tideland Go Actor

GitHub release GitHub license Go Module GoDoc Workflow Go Report Card

Description

Tideland Go Actor provides running backend goroutines for the sequential execution of anonymous functions following the actor model. The Actors can work asynchronously as well as synchronously. Additionally the Actor provides methods for the repeated execution of Actions. So background operation can be automated.

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.

All together simplifies the implementation of concurrent code.

I hope you like it. ;)

Example
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()
}
Contributors

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

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Action

type Action func()

Action defines the signature of an actor action.

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 Go

func Go(options ...Option) (*Actor, error)

Go starts an Actor with the given options.

func (*Actor) DoAsync

func (act *Actor) DoAsync(action Action) error

DoAsync sends the actor function to the backend goroutine and returns when it's queued.

func (*Actor) DoAsyncWithContext added in v0.3.0

func (act *Actor) DoAsyncWithContext(ctx context.Context, action Action) error

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) DoSync

func (act *Actor) DoSync(action Action) error

DoSync executes the actor function and returns when it's done.

func (*Actor) DoSyncWithContext added in v0.3.0

func (act *Actor) DoSyncWithContext(ctx context.Context, action Action) error

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) Err

func (act *Actor) Err() error

Err returns information if the Actor has an error.

func (*Actor) IsDone added in v0.3.0

func (act *Actor) IsDone() bool

IsDone allows to simply check if the Actor is done in a select or if statement.

func (*Actor) Repeat added in v0.3.0

func (act *Actor) Repeat(
	interval time.Duration,
	action Action) (func(), error)

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.

func (*Actor) Stop

func (act *Actor) Stop()

Stop terminates the Actor backend.

type Finalizer

type Finalizer func(err error) error

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

type Option func(act *Actor) error

Option defines the signature of an option setting function.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets the context for the actor.

func WithFinalizer

func WithFinalizer(finalizer Finalizer) Option

WithFinalizer sets a function for finalizing the work of a Loop.

func WithQueueCap

func WithQueueCap(c int) Option

WithQueueCap defines the channel capacity for actions sent to an Actor.

func WithRecoverer added in v0.3.0

func WithRecoverer(recoverer Recoverer) Option

WithRecoverer sets a function for recovering from a panic during executing an action.

type Recoverer added in v0.3.0

type Recoverer func(reason any) error

Recoverer defines the signature of a function for recovering from a panic during executing an action. The reason is the panic value. The function should return the error to be returned by the Actor. If the error is nil, the Actor will continue to work.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL