actor

package module
Version: v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2021 License: BSD-3-Clause Imports: 5 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. Those can work synchronously as well as synchronously. This 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,
	}
	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,
	}
	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 stopping, the timeout of actions, how many actions are queued, and how panics in actions shall be handled.

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 allows to simply use and control a goroutine and sending functions to be executed sequentially by that 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) DoAsyncTimeout

func (act *Actor) DoAsyncTimeout(action Action, timeout time.Duration) error

DoAsyncTimeout send the actor function to the backend and returns when it's queued.

func (*Actor) DoSync

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

DoSync executes the actor function and returns when it's done or it has the default timeout.

func (*Actor) DoSyncTimeout

func (act *Actor) DoSyncTimeout(action Action, timeout time.Duration) error

DoSyncTimeout executes the action and returns when it's done or it has a timeout.

func (*Actor) Err

func (act *Actor) Err() error

Err returns information if the Actor has an error.

func (*Actor) Stop

func (act *Actor) Stop()

Stop terminates the Actor backend.

type Finalizer

type Finalizer func(err error) error

Finalizer is called with the Actors internal status when the backend loop terminates.

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 allows to pass a context for cancellation or timeout.

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 WithRepairer

func WithRepairer(repairer Repairer) Option

WithRepairer defines the panic handler of an actor.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the timeout for sending actions to the actor.

type Repairer

type Repairer func(reason interface{}) error

Repairer allows the Actor to react on a panic during its work. If it returns nil the backend shall continue work. Otherwise the error is stored and the backend terminated.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL