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
- Variables
- func Run[M proto.Message](h Handler[M]) func(*grpcproc.Process[M]) error
- func Supervise(n *grpcproc.Node, spec Spec, opts ...grpcproc.SpawnOption) (grpcproc.PID, error)
- type CallHandler
- type CallsOnly
- type ChildSpec
- func Child[M proto.Message, H Handler[M]](name string, newHandler func() H, opts ...grpcproc.SpawnOption) ChildSpec
- func ChildFunc[M proto.Message](name string, fn func(*grpcproc.Process[M]) error, opts ...grpcproc.SpawnOption) ChildSpec
- func ChildSupervisor(name string, spec Spec, opts ...grpcproc.SpawnOption) ChildSpec
- type DownHandler
- type Handler
- type Initializer
- type Restart
- type Spec
- type Strategy
- type Terminator
Constants ¶
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 ¶
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 ¶
Run turns a Handler into a process function, for Node.Spawn, Process.Spawn or Process.SpawnMonitor.
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 ¶
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.
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 ¶
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 ¶
Initializer runs before the first message. An error ends the actor before it handles anything, and Terminate is not called.
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.
type Terminator ¶
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).