act

package
v1.999.225-0...-975a085 Latest Latest
Warning

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

Go to latest
Published: May 27, 2024 License: MIT Imports: 9 Imported by: 0

README

Standard Actors Library

Actor

Implements generic Actor behavior.

Doc: https://docs.ergo.services/actors/actor

Supervisor

Implements generic Supervisor behavior.

A supervisor is responsible for starting, stopping, and monitoring its child processes. The basic idea of a supervisor is that it is to keep its child processes alive by restarting them when necessary.

Doc: https://docs.ergo.services/actors/supervisor

Pool

Implements generic pool of workers.

This behavior implements a basic design pattern with a pool of workers. All messages/requests received by the pool process are forwarded to the workers using the "Round Robin" algorithm. The worker process is automatically restarting on termination.

Doc: https://docs.ergo.services/actors/pool

Web

Web API Gateway behavior.

The Web API Gateway pattern is also sometimes known as the "Backend For Frontend" (BFF) because you build it while thinking about the needs of the client app. Therefore, BFF sits between the client apps and the microservices. It acts as a reverse proxy, routing requests from clients to services.

Doc: https://docs.ergo.services/actors/web

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrSupervisorStrategyActive   = errors.New("supervisor strategy is active")
	ErrSupervisorChildUnknown     = errors.New("unknown child")
	ErrSupervisorChildRunning     = errors.New("child process is already running")
	ErrSupervisorChildDisabled    = errors.New("child is disabled")
	ErrSupervisorRestartsExceeded = errors.New("restart intensity is exceeded")
	ErrSupervisorChildDuplicate   = errors.New("duplicate child spec Name")

	ErrPoolEmpty = errors.New("no worker process in the pool")
)

Functions

This section is empty.

Types

type Actor

type Actor struct {
	gen.Process
	// contains filtered or unexported fields
}

Actor implementats ProcessBehavior interface and provides callbacks for - initialization - handling messages/requests. - termination All callbacks of the ActorBehavior are optional for the implementation.

func (*Actor) HandleCall

func (a *Actor) HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

func (*Actor) HandleCallAlias

func (a *Actor) HandleCallAlias(alias gen.Alias, from gen.PID, ref gen.Ref, request any) (any, error)

func (*Actor) HandleCallName

func (a *Actor) HandleCallName(name gen.Atom, from gen.PID, ref gen.Ref, request any) (any, error)

func (*Actor) HandleEvent

func (a *Actor) HandleEvent(message gen.MessageEvent) error

func (*Actor) HandleInspect

func (a *Actor) HandleInspect(from gen.PID, item ...string) map[string]string

func (*Actor) HandleLog

func (a *Actor) HandleLog(message gen.MessageLog) error

func (*Actor) HandleMessage

func (a *Actor) HandleMessage(from gen.PID, message any) error

func (*Actor) HandleMessageAlias

func (a *Actor) HandleMessageAlias(alias gen.Alias, from gen.PID, message any) error

func (*Actor) HandleMessageName

func (a *Actor) HandleMessageName(name gen.Atom, from gen.PID, message any) error

func (*Actor) Init

func (a *Actor) Init(args ...any) error

Init

func (*Actor) ProcessInit

func (a *Actor) ProcessInit(process gen.Process, args ...any) (rr error)

ProcessInit

func (*Actor) ProcessRun

func (a *Actor) ProcessRun() (rr error)

func (*Actor) ProcessTerminate

func (a *Actor) ProcessTerminate(reason error)

func (*Actor) SetSplitHandle

func (a *Actor) SetSplitHandle(split bool)

SetSplitHandle enables/disables splitting invoke callback depending on the target type. Enabled splitting makes this process to invoke

  • HandleCall/HandleMessage for the request/message addressed by gen.PID
  • HandleCallName/HandleMessageName for the request/message addressed by gen.ProcessID
  • HandleCallAlias/HandleMessageAlias for the request/message addressed by gen.Alias

func (*Actor) SetTrapExit

func (a *Actor) SetTrapExit(trap bool)

SetTrapExit enables/disables the trap on exit requests sent by SendExit(...). Enabled trap makes the actor ignore such requests transforming them into regular messages (gen.MessageExitPID) except for the request from the parent process with the reason gen.TerminateReasonShutdown. With disabled trap, actor gracefully terminates by invoking Terminate callback with the given reason

func (*Actor) SplitHandle

func (a *Actor) SplitHandle() bool

SplitHandle returns whether the splitting was enabled on this actor

func (*Actor) Terminate

func (a *Actor) Terminate(reason error)

func (*Actor) TrapExit

func (a *Actor) TrapExit() bool

TrapExit returns whether the trap was enabled on this actor

type ActorBehavior

type ActorBehavior interface {
	gen.ProcessBehavior

	// Init invoked on a spawn Actor for the initializing.
	Init(args ...any) error

	// HandleMessage invoked if Actor received a message sent with gen.Process.Send(...).
	// Non-nil value of the returning error will cause termination of this process.
	// To stop this process normally, return gen.TerminateReasonNormal
	// or any other for abnormal termination.
	HandleMessage(from gen.PID, message any) error

	// HandleCall invoked if Actor got a synchronous request made with gen.Process.Call(...).
	// Return nil as a result to handle this request asynchronously and
	// to provide the result later using the gen.Process.SendResponse(...) method.
	HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

	// Terminate invoked on a termination process
	Terminate(reason error)

	// HandleMessageName invoked if split handling was enabled using SetSplitHandle(true)
	// and message has been sent by name
	HandleMessageName(name gen.Atom, from gen.PID, message any) error
	HandleMessageAlias(alias gen.Alias, from gen.PID, message any) error
	HandleCallName(name gen.Atom, from gen.PID, ref gen.Ref, request any) (any, error)
	HandleCallAlias(alias gen.Alias, from gen.PID, ref gen.Ref, request any) (any, error)

	// HandleLog invoked on a log message if this process was added as a logger.
	HandleLog(message gen.MessageLog) error

	// HandleEvent invoked on an event message if this process got subscribed on
	// this event using gen.Process.LinkEvent or gen.Process.MonitorEvent
	HandleEvent(message gen.MessageEvent) error

	// HandleInspect invoked on the request made with gen.Process.Inspect(...)
	HandleInspect(from gen.PID, item ...string) map[string]string
}

ActorBehavior interface

type Pool

type Pool struct {
	gen.Process
	// contains filtered or unexported fields
}

func (*Pool) AddWorkers

func (p *Pool) AddWorkers(n int) (int64, error)

func (*Pool) HandleCall

func (p *Pool) HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

func (*Pool) HandleEvent

func (p *Pool) HandleEvent(message gen.MessageEvent) error

func (*Pool) HandleInspect

func (p *Pool) HandleInspect(from gen.PID, item ...string) map[string]string

func (*Pool) HandleMessage

func (p *Pool) HandleMessage(from gen.PID, message any) error

func (*Pool) ProcessInit

func (p *Pool) ProcessInit(process gen.Process, args ...any) (rr error)

func (*Pool) ProcessRun

func (p *Pool) ProcessRun() (rr error)

func (*Pool) ProcessTerminate

func (p *Pool) ProcessTerminate(reason error)

func (*Pool) RemoveWorkers

func (p *Pool) RemoveWorkers(n int) (int64, error)

func (*Pool) Terminate

func (p *Pool) Terminate(reason error)

type PoolBehavior

type PoolBehavior interface {
	gen.ProcessBehavior

	// Init invoked on a spawn Pool for the initializing.
	Init(args ...any) (PoolOptions, error)

	// HandleMessage invoked if Pool received a message sent with gen.Process.Send(...).
	// Non-nil value of the returning error will cause termination of this process.
	// To stop this process normally, return gen.TerminateReasonNormal
	// or any other for abnormal termination.
	HandleMessage(from gen.PID, message any) error

	// HandleCall invoked if Pool got a synchronous request made with gen.Process.Call(...).
	// Return nil as a result to handle this request asynchronously and
	// to provide the result later using the gen.Process.SendResponse(...) method.
	HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

	// Terminate invoked on a termination process
	Terminate(reason error)

	// HandleEvent invoked on an event message if this process got subscribed on
	// this event using gen.Process.LinkEvent or gen.Process.MonitorEvent
	HandleEvent(message gen.MessageEvent) error

	// HandleInspect invoked on the request made with gen.Process.Inspect(...)
	HandleInspect(from gen.PID, item ...string) map[string]string
}

type PoolOptions

type PoolOptions struct {
	WorkerMailboxSize int64
	PoolSize          int64
	WorkerFactory     gen.ProcessFactory
	WorkerArgs        []any
}

type Supervisor

type Supervisor struct {
	gen.Process
	// contains filtered or unexported fields
}

func (*Supervisor) AddChild

func (s *Supervisor) AddChild(child SupervisorChildSpec) error

AddChild allows add a new child to the supervisor. Returns error if spawning child process is failed.

func (*Supervisor) Children

func (s *Supervisor) Children() []SupervisorChild

Children returns a list of supervisor children processes

func (*Supervisor) DisableChild

func (s *Supervisor) DisableChild(name gen.Atom) error

DisableChild stops the child process with gen.TerminateReasonShutdown and disables it in the supervisor spec.

func (*Supervisor) EnableChild

func (s *Supervisor) EnableChild(name gen.Atom) error

EnableChild enables the child process in the supervisor spec and attempts to start it. Returns error if spawning child process is failed.

func (*Supervisor) HandleCall

func (s *Supervisor) HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

func (*Supervisor) HandleChildStart

func (s *Supervisor) HandleChildStart(name gen.Atom, pid gen.PID) error

func (*Supervisor) HandleChildTerminate

func (s *Supervisor) HandleChildTerminate(name gen.Atom, pid gen.PID, reason error) error

func (*Supervisor) HandleEvent

func (s *Supervisor) HandleEvent(message gen.MessageEvent) error

func (*Supervisor) HandleInspect

func (s *Supervisor) HandleInspect(from gen.PID, item ...string) map[string]string

func (*Supervisor) HandleMessage

func (s *Supervisor) HandleMessage(from gen.PID, message any) error

func (*Supervisor) ProcessInit

func (s *Supervisor) ProcessInit(process gen.Process, args ...any) (rr error)

ProcessInit

func (*Supervisor) ProcessRun

func (s *Supervisor) ProcessRun() (rr error)

func (*Supervisor) ProcessTerminate

func (s *Supervisor) ProcessTerminate(reason error)

func (*Supervisor) StartChild

func (s *Supervisor) StartChild(name gen.Atom, args ...any) error

StartChild starts new child process defined in the supervisor spec.

func (*Supervisor) Terminate

func (s *Supervisor) Terminate(reason error)

type SupervisorBehavior

type SupervisorBehavior interface {
	gen.ProcessBehavior

	// Init invoked on a spawn Supervisor process. This is a mandatory callback for the implementation
	Init(args ...any) (SupervisorSpec, error)

	// HandleChildStart invoked on a successful child process starting if option EnableHandleChild
	// was enabled in act.SupervisorSpec
	HandleChildStart(name gen.Atom, pid gen.PID) error

	// HandleChildTerminate invoked on a child process termination if option EnableHandleChild
	// was enabled in act.SupervisorSpec
	HandleChildTerminate(name gen.Atom, pid gen.PID, reason error) error

	// HandleMessage invoked if Supervisor received a message sent with gen.Process.Send(...).
	// Non-nil value of the returning error will cause termination of this process.
	// To stop this process normally, return gen.TerminateReasonNormal or
	// gen.TerminateReasonShutdown. Any other - for abnormal termination.
	HandleMessage(from gen.PID, message any) error

	// HandleCall invoked if Supervisor got a synchronous request made with gen.Process.Call(...).
	// Return nil as a result to handle this request asynchronously and
	// to provide the result later using the gen.Process.SendResponse(...) method.
	HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

	// Terminate invoked on a termination supervisor process
	Terminate(reason error)

	// HandleEvent invoked on an event message if this process got subscribed on
	// this event using gen.Process.LinkEvent or gen.Process.MonitorEvent
	HandleEvent(message gen.MessageEvent) error

	// HandleInspect invoked on the request made with gen.Process.Inspect(...)
	HandleInspect(from gen.PID, item ...string) map[string]string
}

type SupervisorChild

type SupervisorChild struct {
	Spec        gen.Atom
	Name        gen.Atom
	PID         gen.PID
	Significant bool
	Disabled    bool
}

type SupervisorChildSpec

type SupervisorChildSpec struct {
	Name        gen.Atom
	Significant bool // ignored for SupervisorTypeSimpleOneForOne or if used restart strategy SupervisorStrategyPermanent
	Factory     gen.ProcessFactory
	Options     gen.ProcessOptions
	Args        []any
}

SupervisorChildSpec

type SupervisorRestart

type SupervisorRestart struct {
	Strategy  SupervisorStrategy
	Intensity uint16
	Period    uint16
	KeepOrder bool // ignored for SupervisorTypeSimpleOneForOne and SupervisorTypeOneForOne
}

type SupervisorSpec

type SupervisorSpec struct {
	Children []SupervisorChildSpec
	Type     SupervisorType
	Restart  SupervisorRestart

	// EnableHandleChild enables HandleChildStart/HandleChildTerminate callback
	// invoking on starting/stopping child processes. These callbacks are invoked
	// after the restart strategy finishes its work with all children.
	EnableHandleChild bool

	// DisableAutoShutdown makes the supervisor not shutdown if there is no one
	// running child process left (it happens if all child processes have been
	// terminated normally - by itself or the child spec was disabled).
	// This options is ignored for SupervisorTypeSimpleOneForOne
	// or if used restart strategy SupervisorStrategyPermanent
	DisableAutoShutdown bool
}

SupervisorSpec

type SupervisorStrategy

type SupervisorStrategy int

SupervisorStrategy defines restart strategy for the children processes

const (
	// SupervisorStrategyTransient child process is restarted only if
	// it terminates abnormally, that is, with an exit reason other
	// than TerminateReasonNormal, TerminateReasonShutdown.
	// This is default strategy.
	SupervisorStrategyTransient SupervisorStrategy = 0

	// SupervisorStrategyTemporary child process is never restarted
	// (not even when the supervisor restart strategy is rest_for_one
	// or one_for_all and a sibling death causes the temporary process
	// to be terminated)
	SupervisorStrategyTemporary SupervisorStrategy = 1

	// SupervisorStrategyPermanent child process is always restarted
	SupervisorStrategyPermanent SupervisorStrategy = 2
)

func (SupervisorStrategy) String

func (s SupervisorStrategy) String() string

type SupervisorType

type SupervisorType int

SupervisorType

const (

	// SupervisorTypeOneForOne If one child process terminates and is to be restarted, only
	// that child process is affected. This is the default restart strategy.
	SupervisorTypeOneForOne SupervisorType = 0

	// SupervisorTypeAllForOne If one child process terminates and is to be restarted, all other
	// child processes are terminated and then all child processes are restarted.
	SupervisorTypeAllForOne SupervisorType = 1

	// SupervisorTypeRestForOne If one child process terminates and is to be restarted,
	// the 'rest' of the child processes (that is, the child
	// processes after the terminated child process in the start order)
	// are terminated. Then the terminated child process and all
	// child processes after it are restarted
	SupervisorTypeRestForOne SupervisorType = 2

	// SupervisorTypeSimpleOneForOne A simplified one_for_one supervisor, where all
	// child processes are dynamically added instances
	// of the same process type, that is, running the same code.
	SupervisorTypeSimpleOneForOne SupervisorType = 3
)

func (SupervisorType) String

func (s SupervisorType) String() string

type Web

type Web struct {
	gen.Process
	// contains filtered or unexported fields
}

func (*Web) HandleCall

func (w *Web) HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

func (*Web) HandleEvent

func (w *Web) HandleEvent(message gen.MessageEvent) error

func (*Web) HandleInspect

func (w *Web) HandleInspect(from gen.PID, item ...string) map[string]string

func (*Web) HandleMessage

func (w *Web) HandleMessage(from gen.PID, message any) error

func (*Web) ProcessInit

func (w *Web) ProcessInit(process gen.Process, args ...any) (rr error)

func (*Web) ProcessRun

func (w *Web) ProcessRun() (rr error)

func (*Web) ProcessTerminate

func (w *Web) ProcessTerminate(reason error)

func (*Web) Terminate

func (w *Web) Terminate(reason error)

type WebBehavior

type WebBehavior interface {
	gen.ProcessBehavior

	// Init invoked on a spawn Web for the initializing.
	Init(args ...any) (WebOptions, error)

	// HandleMessage invoked if Web received a message sent with gen.Process.Send(...).
	// Non-nil value of the returning error will cause termination of this process.
	// To stop this process normally, return gen.TerminateReasonNormal
	// or any other for abnormal termination.
	HandleMessage(from gen.PID, message any) error

	// HandleCall invoked if Web got a synchronous request made with gen.Process.Call(...).
	// Return nil as a result to handle this request asynchronously and
	// to provide the result later using the gen.Process.SendResponse(...) method.
	HandleCall(from gen.PID, ref gen.Ref, request any) (any, error)

	// Terminate invoked on a termination process
	Terminate(reason error)

	// HandleEvent invoked on an event message if this process got subscribed on
	// this event using gen.Process.LinkEvent or gen.Process.MonitorEvent
	HandleEvent(message gen.MessageEvent) error

	// HandleInspect invoked on the request made with gen.Process.Inspect(...)
	HandleInspect(from gen.PID, item ...string) map[string]string
}

type WebOptions

type WebOptions struct {
	Host        string
	Port        uint16
	CertManager gen.CertManager
	Handler     http.Handler
}

Jump to

Keyboard shortcuts

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