DEV Community

Md Sulaiman for KhulnaSoft

Posted on

Go Actors - Blazingly Fast, Low-Latency Actors for Golang

Goactors - Blazingly Fast, Low-Latency Actors for Golang

Goactors is an ultra-fast actor engine designed for speed and low-latency applications such as game servers, advertising brokers, and trading engines. It can handle 10 million messages in under 1 second.


πŸš€ Features

βœ… Guaranteed message delivery on actor failure (buffer mechanism)

βœ… Fire & forget, request & response messaging supported

βœ… High-performance dRPC transport layer

βœ… Optimized protobufs without reflection

βœ… Lightweight and highly customizable

βœ… WASM Compilation: Supports GOOS=js and GOOS=wasm32

βœ… Cluster support for distributed, self-discovering actors


πŸ”₯ Benchmarks

make bench
Enter fullscreen mode Exit fullscreen mode
spawned 10 engines
spawned 2000 actors per engine
Send storm starting, will send for 10s using 20 workers
Messages sent per second 1333665
..
Messages sent per second 677231
Concurrent senders: 20 messages sent 6114914, messages received 6114914 - duration: 10s
messages per second: 611491
deadletters: 0
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ Installation

go get github.com/khulnasoft/goactors/...
Enter fullscreen mode Exit fullscreen mode

Note: Goactors requires Golang 1.21


πŸš€ Quickstart

Hello World Example

package main

import (
    "fmt"
    "github.com/khulnasoft/goactors/actor"
)

type message struct {
    data string
}

type helloer struct{}

func newHelloer() actor.Receiver {
    return &helloer{}
}

func (h *helloer) Receive(ctx *actor.Context) {
    switch msg := ctx.Message().(type) {
    case actor.Initialized:
        fmt.Println("Helloer initialized")
    case actor.Started:
        fmt.Println("Helloer started")
    case actor.Stopped:
        fmt.Println("Helloer stopped")
    case *message:
        fmt.Println("Hello, world!", msg.data)
    }
}

func main() {
    engine, _ := actor.NewEngine(actor.NewEngineConfig())
    pid := engine.Spawn(newHelloer, "hello")
    engine.Send(pid, &message{data: "Hello, Goactors!"})
}
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ More examples are available in the examples folder.


πŸ›  Spawning Actors

Default Configuration

e.Spawn(newFoo, "myactorname")
Enter fullscreen mode Exit fullscreen mode

Passing Arguments to Actor Constructor

func newCustomNameResponder(name string) actor.Producer {
    return func() actor.Receiver {
        return &nameResponder{name}
    }
}
Enter fullscreen mode Exit fullscreen mode
pid := engine.Spawn(newCustomNameResponder("Khulnasoft"), "name-responder")
Enter fullscreen mode Exit fullscreen mode

Custom Configuration

e.Spawn(newFoo, "myactorname",
    actor.WithMaxRestarts(4),
    actor.WithInboxSize(2048),
)
Enter fullscreen mode Exit fullscreen mode

Stateless Function Actors

e.SpawnFunc(func(c *actor.Context) {
    switch msg := c.Message().(type) {
    case actor.Started:
        fmt.Println("Actor started")
    }
}, "foo")
Enter fullscreen mode Exit fullscreen mode

🌍 Remote Actors

Goactors allows actors to communicate over a network using the Remote package with protobuf serialization.

Example Configuration

import "crypto/tls"

tlsConfig := &tls.Config{Certificates: []tls.Certificate{cert}}
config := remote.NewConfig().WithTLS(tlsConfig)
remote := remote.New("0.0.0.0:2222", config)
engine, _ := actor.NewEngine(actor.NewEngineConfig().WithRemote(remote))
Enter fullscreen mode Exit fullscreen mode

πŸ“‚ Check out the Remote Actor Examples and Chat Server for details.


🎯 Event Stream

Goactors provides a powerful event stream to handle system events gracefully:

βœ… Monitor crashes, deadletters, and network failures

βœ… Subscribe actors to system events

βœ… Broadcast custom events

List of Internal Events:

  • actor.ActorInitializedEvent
  • actor.ActorStartedEvent
  • actor.ActorStoppedEvent
  • actor.DeadLetterEvent
  • actor.ActorRestartedEvent
  • actor.RemoteUnreachableEvent
  • cluster.MemberJoinEvent
  • cluster.MemberLeaveEvent
  • cluster.ActivationEvent
  • cluster.DeactivationEvent

πŸ“‚ See the Event Stream Example for usage.


βš™οΈ Customizing the Engine

Use function options to customize the Goactors engine:

r := remote.New(remote.Config{ListenAddr: "0.0.0.0:2222"})
engine, _ := actor.NewEngine(actor.EngineOptRemote(r))
Enter fullscreen mode Exit fullscreen mode

πŸ— Middleware

Extend actors with custom middleware for:

  • Metrics collection
  • Data persistence
  • Custom logging

πŸ“‚ Examples available in the middleware folder.


πŸ“ Logging

Goactors uses structured logging via log/slog:

import "log/slog"
slog.SetDefaultLogger(myCustomLogger)
Enter fullscreen mode Exit fullscreen mode

βœ… Testing

make test
Enter fullscreen mode Exit fullscreen mode

πŸ“œ License

Goactors is licensed under the MIT License.

Top comments (0)