actor

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package actor provides a generic, reusable actor pattern for serial message processing with independent context lifecycle.

Index

Constants

This section is empty.

Variables

View Source
var ErrAborted = errors.New("actor aborted")

ErrAborted is returned when the current execution is aborted via Abort().

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

ErrStopped is returned by Send when the actor has been stopped.

Functions

This section is empty.

Types

type Actor

type Actor[Req, Resp any] struct {
	// contains filtered or unexported fields
}

Actor is a generic execution unit with a serial mailbox and independent context lifecycle.

func New

func New[Req, Resp any](handler Handler[Req, Resp], opts ...Option) *Actor[Req, Resp]

New creates and starts a new Actor.

func (*Actor[Req, Resp]) Abort

func (a *Actor[Req, Resp]) Abort() bool

Abort cancels the currently running request without stopping the actor. Returns true if a running request was cancelled, false if the actor was idle. Safe to call concurrently and multiple times.

func (*Actor[Req, Resp]) Context

func (a *Actor[Req, Resp]) Context() context.Context

Context returns the actor's context.

func (*Actor[Req, Resp]) Done

func (a *Actor[Req, Resp]) Done() <-chan struct{}

Done returns a channel that is closed when the actor's run loop has exited. Use this to wait for the actor to fully quiesce after Stop().

func (*Actor[Req, Resp]) InboxLen

func (a *Actor[Req, Resp]) InboxLen() int

InboxLen returns the number of pending messages in the inbox.

func (*Actor[Req, Resp]) IsPersistent

func (a *Actor[Req, Resp]) IsPersistent() bool

IsPersistent reports whether the actor is exempt from idle reaping.

func (*Actor[Req, Resp]) IsRunning

func (a *Actor[Req, Resp]) IsRunning() bool

IsRunning reports whether the actor is currently executing a request.

func (*Actor[Req, Resp]) LastActive

func (a *Actor[Req, Resp]) LastActive() time.Time

LastActive returns the time of the last activity.

func (*Actor[Req, Resp]) Send

func (a *Actor[Req, Resp]) Send(req Req) <-chan Result[Resp]

Send delivers a request to the actor's mailbox and returns a channel that receives exactly one Result when processing completes.

func (*Actor[Req, Resp]) Source

func (a *Actor[Req, Resp]) Source() string

Source returns the creation source tag.

func (*Actor[Req, Resp]) Stop

func (a *Actor[Req, Resp]) Stop()

Stop cancels the actor's context and signals the mailbox to reject new messages. Safe to call multiple times.

func (*Actor[Req, Resp]) TrySend

func (a *Actor[Req, Resp]) TrySend(req Req) (<-chan Result[Resp], bool)

TrySend attempts a non-blocking send. It returns the result channel and true if the message was enqueued, or nil and false if the mailbox is full or the actor is stopped.

type Handler

type Handler[Req, Resp any] func(ctx context.Context, req Req) (Resp, error)

Handler processes a single request and returns a response.

type Option

type Option func(*config)

Option configures an Actor.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets a parent context for the actor. When this context is cancelled, the actor is cascade-terminated (equivalent to Stop).

func WithDrainTimeout

func WithDrainTimeout(d time.Duration) Option

func WithInboxSize

func WithInboxSize(n int) Option

func WithPersistent

func WithPersistent() Option

func WithSource

func WithSource(source string) Option

type Result

type Result[Resp any] struct {
	Value Resp
	Err   error
}

Result is the outcome of a single request.

Jump to

Keyboard shortcuts

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