Skip to content

tideland/go-actor

Repository files navigation

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

About

Go package supporting easier synchronous and asynchronous concurrent programming based on the actor model.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages