Documentation
¶
Index ¶
- Variables
- func AssertStartStopAtRandom(t *testing.T, a Actor)
- func AssertWorkerEndSig(t *testing.T, aw any)
- func FanOut[T any, MS MailboxSender[T]](receiveC <-chan T, senders []MS)
- func TestSuite(t *testing.T, fact func() Actor)
- type Actor
- type Context
- type Mailbox
- type MailboxReceiver
- type MailboxSender
- type Option
- type StartableWorker
- type StoppableWorker
- type Worker
- type WorkerFunc
- type WorkerStatus
Constants ¶
This section is empty.
Variables ¶
var ErrStopped = errors.New("actor stopped")
ErrStopped is the error returned by Context.Err when the Actor is stopped.
Functions ¶
func AssertStartStopAtRandom ¶
func AssertWorkerEndSig ¶
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.
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 ¶
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 ¶
FromMailboxes creates single Actor combining actors of supplied Mailboxes.
type 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.
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 ¶
OptCapacity sets initial Mailbox queue capacity. Value must be power of 2.
func OptMailbox ¶
OptMailbox sets all Mailbox capacity options at once.
func OptMinCapacity ¶
OptMinCapacity sets minimum Mailbox queue capacity. Value must be power of 2.
func OptOnStart ¶
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 ¶
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 )