Actors
Minimal actor framework to build reactive and distributed system in golang using protocol buffers.
If you are not familiar with the actor model, the blog post from Brian Storti here is an excellent and short introduction to the actor model.
Also, check reference section at the end of the post for more material regarding actor model
Features
- Send a synchronous message to an actor from a non actor system
- Send an asynchronous(fire-and-forget) message to an actor from a non actor system
- Actor to Actor communication (check the examples folder)
- Enable/Disable Passivation mode to remove/keep idle actors
- PreStart hook for an actor.
- PostStop hook for an actor for a graceful shutdown
- ActorSystem (WIP)
- Actor to Actor communication
- Restart an actor
- (Un)Watch an actor
- Stop and actor
- Create a child actor
- Supervisory Strategy (Restart and Stop directive)
- Behaviors (Become/BecomeStacked/UnBecome/UnBecomeStacked)
- Logger interface with a default logger
- Examples (check the examples' folder)
- Integration with OpenTelemetry for traces and metrics.
- Remoting
- Actors can send messages to other actors on a remote system
- Actors can look up other actors' address on a remote system
- Clustering
- More tests
Installation
go get github.com/super-flat/actors
Example
Send a fire-forget message to an actor from a non actor system
package main
import (
"context"
"fmt"
"os"
"os/signal"
"sync"
"syscall"
"time"
goakt "github.com/super-flat/actors/actors"
samplepb "github.com/super-flat/actors/examples/protos/pb/v1"
"github.com/super-flat/actors/log"
"go.uber.org/atomic"
)
func main() {
ctx := context.Background()
// use the goakt default logger. real-life implement the logger interface`
logger := log.DefaultLogger
// create the actor system configuration. kindly in real-life application handle the error
config, _ := goakt.NewConfig("SampleActorSystem", "127.0.0.1:0",
goakt.WithExpireActorAfter(10*time.Second), // set big passivation time
goakt.WithLogger(logger),
goakt.WithActorInitMaxRetries(3))
// create the actor system. kindly in real-life application handle the error
actorSystem, _ := goakt.NewActorSystem(config)
// start the actor system
_ = actorSystem.Start(ctx)
// create an actor
pingActor := actorSystem.StartActor(ctx, "Ping", NewPingActor())
pongActor := actorSystem.StartActor(ctx, "Pong", NewPongActor())
// start the conversation
_ = pingActor.SendAsync(ctx, pongActor, new(samplepb.Ping))
// shutdown both actors after 3 seconds of conversation
timer := time.AfterFunc(3*time.Second, func() {
log.DefaultLogger.Infof("PingActor=%s has processed %d messages", pingActor.ActorPath().String(), pingActor.ReceivedCount(ctx))
log.DefaultLogger.Infof("PongActor=%s has processed %d messages", pongActor.ActorPath().String(), pongActor.ReceivedCount(ctx))
_ = pingActor.Shutdown(ctx)
_ = pongActor.Shutdown(ctx)
})
defer timer.Stop()
// capture ctrl+c
interruptSignal := make(chan os.Signal, 1)
signal.Notify(interruptSignal, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
<-interruptSignal
// stop the actor system
_ = actorSystem.Stop(ctx)
os.Exit(0)
}
type PingActor struct {
mu sync.Mutex
count *atomic.Int32
logger log.Logger
}
var _ goakt.Actor = (*PingActor)(nil)
func NewPingActor() *PingActor {
return &PingActor{
mu: sync.Mutex{},
}
}
func (p *PingActor) PreStart(ctx context.Context) error {
// set the logger
p.mu.Lock()
defer p.mu.Unlock()
p.logger = log.DefaultLogger
p.count = atomic.NewInt32(0)
p.logger.Info("About to Start")
return nil
}
func (p *PingActor) Receive(ctx goakt.ReceiveContext) {
switch ctx.Message().(type) {
case *samplepb.Pong:
p.logger.Infof(fmt.Sprintf("received Pong from %s", ctx.Sender().ActorPath().String()))
// reply the sender in case there is a sender
if ctx.Sender() != goakt.NoSender {
// let us reply to the sender
_ = ctx.Self().SendAsync(ctx.Context(), ctx.Sender(), new(samplepb.Ping))
}
p.count.Add(1)
default:
p.logger.Panic(goakt.ErrUnhandled)
}
}
func (p *PingActor) PostStop(ctx context.Context) error {
p.logger.Info("About to stop")
p.logger.Infof("Processed=%d messages", p.count.Load())
return nil
}
type PongActor struct {
mu sync.Mutex
count *atomic.Int32
logger log.Logger
}
var _ goakt.Actor = (*PongActor)(nil)
func NewPongActor() *PongActor {
return &PongActor{
mu: sync.Mutex{},
}
}
func (p *PongActor) PreStart(ctx context.Context) error {
// set the logger
p.mu.Lock()
defer p.mu.Unlock()
p.logger = log.DefaultLogger
p.count = atomic.NewInt32(0)
p.logger.Info("About to Start")
return nil
}
func (p *PongActor) Receive(ctx goakt.ReceiveContext) {
switch ctx.Message().(type) {
case *samplepb.Ping:
p.logger.Infof(fmt.Sprintf("received Ping from %s", ctx.Sender().ActorPath().String()))
// reply the sender in case there is a sender
if ctx.Sender() != nil && ctx.Sender() != goakt.NoSender {
_ = ctx.Self().SendAsync(ctx.Context(), ctx.Sender(), new(samplepb.Pong))
}
p.count.Add(1)
default:
p.logger.Panic(goakt.ErrUnhandled)
}
}
func (p *PongActor) PostStop(ctx context.Context) error {
p.logger.Info("About to stop")
p.logger.Infof("Processed=%d messages", p.count.Load())
return nil
}
Contribution
Contributions are welcome!
The project adheres to Semantic Versioning and Conventional Commits.
This repo uses Earthly.
To contribute please:
- Fork the repository
- Create a feature branch
- Submit a pull request
Test & Linter
Prior to submitting a pull request, please run:
earthly +test