actor

package
v0.0.0-...-368fb97 Latest Latest
Warning

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

Go to latest
Published: May 20, 2023 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrStopped = errors.New("actor stopped")

ErrStopped is the error returned by Context.Err when the Actor is stopped.

Functions

func AssertStartStopAtRandom

func AssertStartStopAtRandom(t *testing.T, a Actor)

func AssertWorkerEndSig

func AssertWorkerEndSig(t *testing.T, aw any)

func FanOut

func FanOut[T any, MS MailboxSender[T]](receiveC <-chan T, senders []MS)

FanOut spawns new goroutine in which messages received by receiveC are forwarded to senders. Spawned goroutine will be active while receiveC is open.

func TestSuite

func TestSuite(t *testing.T, fact func() Actor)

Types

type Actor

type Actor interface {
	// Start spawns new goroutine and begins Worker execution.
	//
	// Execution will last until Stop() method is called or Worker returned
	// status indicating that Worker has ended (there is no more work).
	Start()

	// Stop sends signal to Worker to stop execution. Method will block
	// until Worker finishes.
	Stop()
}

Actor is computational entity that executes Worker in individual goroutine.

func Combine

func Combine(actors ...Actor) Actor

Combine returns single Actor which combines all specified actors into one. Calling Start or Stop function on this Actor will invoke respective function on all Actors provided to this function.

func FromMailboxes

func FromMailboxes[T any](mm []Mailbox[T]) Actor

FromMailboxes creates single Actor combining actors of supplied Mailboxes.

func Idle

func Idle(opt ...Option) Actor

Idle returns new Actor without Worker.

func New

func New(w Worker, opt ...Option) Actor

New returns new Actor with specified Worker and Options.

func Noop

func Noop() Actor

Noop returns no-op Actor.

type Context

type Context = gocontext.Context

Context is provided to Worker so they can listen and respond on stop signal sent from Actor.

func ContextEnded

func ContextEnded() Context

ContextStarted returns Context representing ended state for Actor. It is typically used in tests for passing to Worker.DoWork() function.

func ContextStarted

func ContextStarted() Context

ContextStarted returns Context representing started state for Actor. It is typically used in tests for passing to Worker.DoWork() function.

type Mailbox

type Mailbox[T any] interface {
	Actor
	MailboxSender[T]
	MailboxReceiver[T]
}

Mailbox is interface for message transport mechanism between Actors which can receive infinite number of messages. Mailbox is much like native go channel, except that writing to the Mailbox will never block, all messages are going to be queued and Actors on receiving end of the Mailbox will get all messages in FIFO order.

func NewMailbox

func NewMailbox[T any](opt ...Option) Mailbox[T]

NewMailbox returns new Mailbox.

func NewMailboxes

func NewMailboxes[T any](count int, opt ...Option) []Mailbox[T]

NewMailboxes returns slice of new Mailbox instances with specified count.

type MailboxReceiver

type MailboxReceiver[T any] interface {
	// ReceiveC returns channel where data can be received.
	ReceiveC() <-chan T
}

MailboxReceiver is interface for receiver bits of Mailbox.

type MailboxSender

type MailboxSender[T any] interface {
	// SendC returns channel where data can be sent.
	SendC() chan<- T
}

MailboxSender is interface for sender bits of Mailbox.

type Option

type Option func(o *options)

func OptCapacity

func OptCapacity(capacity int) Option

OptCapacity sets initial Mailbox queue capacity. Value must be power of 2.

func OptMailbox

func OptMailbox(capacity, minCapacity int) Option

OptMailbox sets all Mailbox capacity options at once.

func OptMinCapacity

func OptMinCapacity(minCapacity int) Option

OptMinCapacity sets minimum Mailbox queue capacity. Value must be power of 2.

func OptOnStart

func OptOnStart(f func(Context)) Option

OptOnStart adds function to Actor which will be executed before first worker's iteration. If Actor implements StartableWorker interface then function will be called after calling respective method on this interface. This functions is executed in actor's goroutine.

func OptOnStop

func OptOnStop(f func()) Option

OptOnStop adds function to Actor which will be executed after last worker's iteration. If Actor implements StoppableWorker interface then function will be called after calling respective method on this interface. This functions is executed in actor's goroutine.

func OptUsingChan

func OptUsingChan(usingChan bool) Option

OptUsingChan makes Mailbox to function as wrapper for native go channel.

type StartableWorker

type StartableWorker interface {
	// OnStart is called right before DoWork() is called for first time. It can be used to
	// initialize Worker as it will be called only once.
	//
	// Context is provided in case when Actor is stopped early and OnStop should terminated
	// with initialization. This is same Context as one which will be provided to DoWork method
	// in later stages of Worker lifecycle.
	OnStart(Context)
}

StartableWorker defines optional interface which Worker can implement

type StoppableWorker

type StoppableWorker interface {
	// OnStop is called after last DoWork() returns. It can be used to release all
	// resources occupied by Worker.
	//
	// Context is not proved as at this point as it was already ended.
	OnStop()
}

StoppableWorker defines optional interface which Worker can implement

type Worker

type Worker interface {
	// DoWork function is encapsulating single executable unit of work for this Worker.
	//
	// Context is provided so Worker can listen and respond on stop signal sent from Actor.
	//
	// WorkerStatus is returned indicating if Actor should continue executing this Worker.
	// Actor will check this status and stop execution if Worker has no more work, otherwise
	// proceed execution.
	DoWork(c Context) WorkerStatus
}

Worker is entity which encapsulates Actor's executable logic.

Worker's implementation should listen on messages sent via Mailboxes and preform actions by sending new messages or creating new actors.

func NewWorker

func NewWorker(fn WorkerFunc) Worker

NewWorker returns basic Worker implementation which delegates DoWork to supplied WorkerFunc.

type WorkerFunc

type WorkerFunc = func(c Context) WorkerStatus

WorkerFunc is signature of Worker's DoWork function.

type WorkerStatus

type WorkerStatus int8

WorkerStatus is returned by Worker's DoWork function indicating if Actor should continue executing Worker.

const (
	WorkerContinue WorkerStatus = 1
	WorkerEnd      WorkerStatus = 2
)

Jump to

Keyboard shortcuts

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