actor

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 10 Imported by: 5 modules, 3 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package actor is optional structure on top of grpcproc processes: a handler loop in the style of gen_server, and supervisors that restart what fails. It uses only grpcproc's public API, so it can be ignored or replaced.

An actor is a plain struct holding its dependencies, built by a constructor, so it fits a DI container:

type Orders struct{ repo *Repo }

func (o *Orders) HandleMessage(p *grpcproc.Process[*orderspb.Order], m grpcproc.Msg[*orderspb.Order]) error { … }
func (o *Orders) HandleCall(p *grpcproc.Process[*orderspb.Order], m grpcproc.Msg[*orderspb.Order]) (proto.Message, error) { … }

addr, err := node.Spawn(actor.Run(&Orders{repo: repo}), grpcproc.WithName("orders"))

Index

Constants

View Source
const ReasonMaxRestarts = "max restarts"

ReasonMaxRestarts is the exit reason of a supervisor that restarted its children more often than Spec allows. It is abnormal, so the supervisor's own supervisor restarts it: that is how failure escalates.

Variables

View Source
var (
	// ErrStop, returned from a handler, ends the actor normally. From
	// HandleCall, the reply is sent first.
	ErrStop = errors.New("actor: stop")
	// ErrNoReply, returned from HandleCall, means the actor will answer
	// later with p.Reply(m, …).
	ErrNoReply = errors.New("actor: reply later")
)

Functions

func Run

func Run[M proto.Message](h Handler[M]) func(*grpcproc.Process[M]) error

Run turns a Handler into a process function, for Node.Spawn, Process.Spawn or Process.SpawnMonitor.

func Supervise

func Supervise(n *grpcproc.Node, spec Spec, opts ...grpcproc.SpawnOption) (grpcproc.PID, error)

Supervise starts a supervisor on n and its children, in order. It returns once they have all started, or with the first error, after stopping those already started. opts apply to the supervisor itself (a name, a label).

Types

type CallHandler

type CallHandler[M proto.Message] interface {
	HandleCall(p *grpcproc.Process[M], m grpcproc.Msg[M]) (proto.Message, error)
}

CallHandler answers calls: the returned message, or error, is the reply, and the actor carries on. Return ErrNoReply to answer later with p.Reply(m, …), from any goroutine; ErrStop to reply and then stop.

Without it, a call is answered with an error.

type CallsOnly

type CallsOnly[M proto.Message] struct{}

CallsOnly provides HandleMessage for an actor that only answers calls; embed it by value. A message sent to the actor with Send is logged at Warn and dropped, and the actor carries on, as gen_server's default handle_info does: a stray sender cannot crash it.

type Pricer struct {
	actor.CallsOnly[*pricespb.Quote]
	table *Table
}

func (pr *Pricer) HandleCall(p *grpcproc.Process[*pricespb.Quote], m grpcproc.Msg[*pricespb.Quote]) (proto.Message, error) { … }

addr, err := node.Spawn(actor.Run(&Pricer{table: table}))

Run panics for an actor that embeds CallsOnly but has no HandleCall, which is what spawning Pricer{} rather than &Pricer{} gives.

func (CallsOnly[M]) HandleMessage

func (CallsOnly[M]) HandleMessage(p *grpcproc.Process[M], m grpcproc.Msg[M]) error

HandleMessage logs m and drops it.

type ChildSpec

type ChildSpec struct {
	// Name is the child's identity in the supervisor and the name it is
	// registered under on the node, so it stays reachable across restarts,
	// with grpcproc.Named. It must be unique on the node.
	Name    string
	Restart Restart
	// contains filtered or unexported fields
}

ChildSpec says how to start one child. Build it with Child, ChildFunc or ChildSupervisor.

func Child

func Child[M proto.Message, H Handler[M]](name string, newHandler func() H, opts ...grpcproc.SpawnOption) ChildSpec

Child is a child that runs a Handler, built afresh by newHandler at every start so a restart begins from a clean state:

actor.Child("orders", func() *Orders { return &Orders{repo: repo} })

func ChildFunc

func ChildFunc[M proto.Message](name string, fn func(*grpcproc.Process[M]) error, opts ...grpcproc.SpawnOption) ChildSpec

ChildFunc is a child that runs fn, registered as name.

func ChildSupervisor

func ChildSupervisor(name string, spec Spec, opts ...grpcproc.SpawnOption) ChildSpec

ChildSupervisor is a child that is itself a supervisor.

func (ChildSpec) WithRestart

func (c ChildSpec) WithRestart(r Restart) ChildSpec

WithRestart returns a copy of c with restart policy r.

type DownHandler

type DownHandler[M proto.Message] interface {
	HandleDown(p *grpcproc.Process[M], d grpcproc.Down) error
}

DownHandler is told when a process the actor monitors exits. Without it, Downs are ignored.

type Handler

type Handler[M proto.Message] interface {
	HandleMessage(p *grpcproc.Process[M], m grpcproc.Msg[M]) error
}

Handler handles the messages sent to an actor whose mailbox holds M. Returning an error ends the actor with that error as its exit reason; ErrStop ends it normally.

type Initializer

type Initializer[M proto.Message] interface {
	Init(p *grpcproc.Process[M]) error
}

Initializer runs before the first message. An error ends the actor before it handles anything, and Terminate is not called.

type Restart

type Restart uint8

Restart says when a child is restarted.

const (
	// Permanent children are always restarted.
	Permanent Restart = iota
	// Transient children are restarted only after an abnormal exit: not
	// after "normal" or "shutdown".
	Transient
	// Temporary children are never restarted.
	Temporary
)

func (Restart) String

func (r Restart) String() string

type Spec

type Spec struct {
	Strategy Strategy
	// MaxRestarts within Within is how many restarts the supervisor makes
	// before giving up and exiting with ReasonMaxRestarts. Zero means 3;
	// a negative value means none.
	MaxRestarts int
	// Within defaults to 5s.
	Within time.Duration
	// Shutdown is how long a child has to exit once told to; grpcproc cannot
	// kill a goroutine, so a child that ignores Exit is left behind. Default 5s.
	Shutdown time.Duration
	// Children start in order and stop in reverse order.
	Children []ChildSpec
}

Spec describes a supervisor.

type Strategy

type Strategy uint8

Strategy says which children a supervisor restarts when one exits.

const (
	// OneForOne restarts only the child that exited.
	OneForOne Strategy = iota
	// OneForAll stops every other child and restarts them all.
	OneForAll
	// RestForOne restarts the child and every child started after it.
	RestForOne
)

func (Strategy) String

func (s Strategy) String() string

type Terminator

type Terminator[M proto.Message] interface {
	Terminate(p *grpcproc.Process[M], err error)
}

Terminator runs when the actor ends, however it ends: err is nil after ErrStop, the handler's error, an *grpcproc.ExitError after Exit, context.Canceled when the node stops, or "panic: …" (after which the panic continues, and grpcproc reports it).

Jump to

Keyboard shortcuts

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