actor

package
v0.0.0-...-3511abf Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: Apache-2.0 Imports: 22 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrUnexpectedMessage

func ErrUnexpectedMessage(ctx *Context) error

ErrUnexpectedMessage is returned by an actor in response to a message that it was not expecting to receive.

Types

type Actor

type Actor interface {
	// Receive defines the actor's behavior. Receive is called for each message in the inbox until
	// a request to stop is received or the parent shuts the actor down.
	Receive(context *Context) error
}

Actor is an object that encapsulates both state and behavior.

type ActorFunc

type ActorFunc func(context *Context) error

ActorFunc is a function that encapsulates behavior. It is a stateless actor, useful for a mocking.

func (ActorFunc) Receive

func (f ActorFunc) Receive(context *Context) error

Receive implements actor.Actor.

type Address

type Address struct {
	// contains filtered or unexported fields
}

Address is the location of an actor within an actor system.

func Addr

func Addr(rawPath ...interface{}) Address

Addr returns a new address with the provided actor path components. Each of the path components must be URL-safe.

func AddrFromString

func AddrFromString(fullPath string) Address

AddrFromString is the inverse of `Address.String()`.

func (Address) Child

func (a Address) Child(child interface{}) Address

Child returns a new address that is a child of this address.

func (Address) IsAncestorOf

func (a Address) IsAncestorOf(address Address) bool

IsAncestorOf returns true if the provided address is a descendant of this address.

func (Address) Local

func (a Address) Local() string

Local returns the local ID of the actor relative to the parent's ID space.

func (Address) MarshalJSON

func (a Address) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (Address) MarshalText

func (a Address) MarshalText() (text []byte, err error)

MarshalText implements the encoding.TextMarshaler interface.

func (Address) Parent

func (a Address) Parent() Address

Parent returns this actor's parent address.

func (Address) String

func (a Address) String() string

func (*Address) UnmarshalJSON

func (a *Address) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

func (*Address) UnmarshalText

func (a *Address) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

type ChildFailed

type ChildFailed struct {
	Child *Ref
	Error error
}

ChildFailed is a message notifying the actor that one of its children has failed.

type ChildStopped

type ChildStopped struct {
	Child *Ref
}

ChildStopped is a message notifying the actor when a child has stopped.

type Context

type Context struct {
	// contains filtered or unexported fields
}

Context holds contextual information for the context's recipient and the current message.

func (*Context) ActorOf

func (c *Context) ActorOf(id interface{}, actor Actor) (*Ref, bool)

ActorOf adds the actor to the system as a child of the context's recipient. If an actor with that ID already exists, that actor's reference is returned instead. The second argument is true if the actor reference was created and false otherwise.

func (*Context) ActorOfFromFactory

func (c *Context) ActorOfFromFactory(id interface{}, factory func() Actor) (*Ref, bool)

ActorOfFromFactory behaves the same as ActorOf but will only create the actor instance if it's needed. It is intended for cases where an actor needs to be looked up many times safely but usually exists.

func (*Context) AddLabel

func (c *Context) AddLabel(key string, value interface{})

AddLabel adds a new label to the context's recipient's logger.

func (*Context) AddLabels

func (c *Context) AddLabels(ctx logger.Context)

AddLabels adds new labels to the context's recipient's logger.

func (*Context) Ask

func (c *Context) Ask(actor *Ref, message Message) Response

Ask sends the specified message to the actor, returning a future to the result of the call. The new context's sender is set to the recipient of this context.

func (*Context) AskAll

func (c *Context) AskAll(message Message, actors ...*Ref) Responses

AskAll sends the specified message to all actors, returning a future to all results of the call. Results are returned in arbitrary order. The result channel is closed after all actors respond. The new context's sender is set to recipient of this context.

func (*Context) Child

func (c *Context) Child(id interface{}) *Ref

Child returns the child with the given local ID.

func (*Context) Children

func (c *Context) Children() []*Ref

Children returns a list of references to the context's recipient's children.

func (*Context) ExpectingResponse

func (c *Context) ExpectingResponse() bool

ExpectingResponse returns true if the sender is expecting a response and false otherwise.

func (*Context) Kill

func (c *Context) Kill(id interface{}) bool

Kill removes the child with the given local ID from this parent. All messages from this child to this actor are ignored.

func (*Context) Log

func (c *Context) Log() *log.Entry

Log returns the context's recipient's logger.

func (*Context) Message

func (c *Context) Message() Message

Message returns the underlying message.

func (*Context) MustActorOf

func (c *Context) MustActorOf(id interface{}, actor Actor) *Ref

MustActorOf adds the actor with the provided address. It panics if a new actor was not created.

func (*Context) Respond

func (c *Context) Respond(message Message)

Respond returns a response message for this request message back to the sender.

func (*Context) RespondCheckError

func (c *Context) RespondCheckError(message Message, err error)

RespondCheckError returns a response message for this request message back to the sender. If the response has an error send that instead.

func (*Context) Self

func (c *Context) Self() *Ref

Self returns the reference to the context's recipient.

func (*Context) Tell

func (c *Context) Tell(actor *Ref, message Message)

Tell sends the specified message to the actor (fire-and-forget semantics). The new context's sender is set to the recipient of this context.

func (*Context) TellAll

func (c *Context) TellAll(message Message, actors ...*Ref)

TellAll sends the specified message to all actors (fire-and-forget semantics).

type Message

type Message interface{}

Message holds the communication protocol between actors. Actors can send messages to other actors and receive messages from other actors.

type Messenger

type Messenger interface {
	Tell(*Ref, Message)
	Ask(*Ref, Message) Response
}

Messenger is an interface for an actor system or context.

type Ping

type Ping struct{}

Ping is an actor message that the actor system automatically respond with an empty response back once being processed. It is used for synchronizing that the messages that are previous to this Ping message and received by the actor are processed.

type PostStop

type PostStop struct{}

PostStop notifies the actor that its reference is shutting down.

type PreStart

type PreStart struct{}

PreStart notifies the actor before its reference is started.

type Ref

type Ref struct {
	// contains filtered or unexported fields
}

Ref is an immutable actor reference to an actor.

func (*Ref) Address

func (r *Ref) Address() Address

Address returns the address of the actor.

func (*Ref) AwaitTermination

func (r *Ref) AwaitTermination() error

AwaitTermination waits for the actor to stop, returning an error if the actor has failed during its lifecycle.

func (*Ref) Child

func (r *Ref) Child(id interface{}) *Ref

Child returns the child with the given local ID.

func (*Ref) Children

func (r *Ref) Children() []*Ref

Children returns a list of references to the actor's children.

func (*Ref) MarshalJSON

func (r *Ref) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*Ref) Parent

func (r *Ref) Parent() *Ref

Parent returns the reference to the actor's parent.

func (*Ref) RegisteredTime

func (r *Ref) RegisteredTime() time.Time

RegisteredTime returns the time that the actor registered with the system.

func (*Ref) Stop

func (r *Ref) Stop()

Stop asynchronously notifies the actor to stop.

func (*Ref) StopAndAwaitTermination

func (r *Ref) StopAndAwaitTermination() error

StopAndAwaitTermination synchronously stops the actor, returning an error if the actor fails to close properly.

func (*Ref) String

func (r *Ref) String() string

func (*Ref) System

func (r *Ref) System() *System

System returns the underlying system that this actor belongs to.

type Response

type Response interface {
	// Source returns the source of the response.
	Source() *Ref
	// Get returns the result of the `Ask` or nil if the actor did not respond.
	Get() Message
	// GetOrTimeout returns the result of the `Ask` or nil if the actor did not respond. If the
	// timeout is reached, nil is returned and the second result returns false.
	GetOrTimeout(timeout time.Duration) (Message, bool)
	// GetOrElse returns the result of the `Ask` or the provided default if the actor did not
	// respond.
	GetOrElse(defaultValue Message) Message
	// GetOrElseTimeout returns the result of the `Ask` or the provided default if the actor did not
	// respond. If the timeout is reached, the defaultValue is returned and the second result
	// returns false.
	GetOrElseTimeout(defaultValue Message, timeout time.Duration) (Message, bool)
	// Empty returns true if the actor did not respond and false otherwise.
	Empty() (empty bool)
	// Error returns the error if the actor returned an error response and nil otherwise.
	Error() (err error)
	// ErrorOrTimeout returns the error, or nil if there is none, if the actor returned an error
	// response within the deadline. If the deadline is exceeded, the bool returned false.
	ErrorOrTimeout(timeout time.Duration) (bool, error)
}

Response holds a reference to the future result of an `Ask` of an actor. Responses are not thread safe.

type Responses

type Responses <-chan Response

Responses wraps a collection of response objects from different actors.

func (Responses) GetAll

func (r Responses) GetAll() map[*Ref]Message

GetAll waits for all actors to respond and returns a mapping of all actors and their corresponding responses.

func (Responses) MarshalJSON

func (r Responses) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

type System

type System struct {
	*Ref
	// contains filtered or unexported fields
}

System is a hierarchical group of actors.

func NewSystem

func NewSystem(id string) *System

NewSystem constructs a new actor system and starts it.

func NewSystemWithRoot

func NewSystemWithRoot(id string, actor Actor) *System

NewSystemWithRoot constructs a new actor system with the specified root actor and starts it.

func (*System) ActorOf

func (s *System) ActorOf(address Address, actor Actor) (*Ref, bool)

ActorOf adds the actor with the provided address. The second return value denotes whether a new actor was created or not.

func (*System) Ask

func (s *System) Ask(actor *Ref, message Message) Response

Ask sends the specified message to the actor, returning a future to the result of the call. The context's sender is set to `nil`.

func (*System) AskAll

func (s *System) AskAll(message Message, actors ...*Ref) Responses

AskAll sends the specified message to all actors, returning a future to all results of the call. Results are returned in arbitrary order. The result channel is closed after all actors respond. The context's sender is set to `nil`.

func (*System) AskAllTimeout

func (s *System) AskAllTimeout(message Message, timeout time.Duration, actors ...*Ref) Responses

AskAllTimeout sends the specified message to all actors, returning a future to all results of the call. Results are returned in arbitrary order. The result channel is closed after all actors respond. The context's sender is set to `nil`. If the timeout is reached, nil responses are returned.

func (*System) AskAt

func (s *System) AskAt(addr Address, message Message) Response

AskAt sends the specified message to the actor at the provided address, returning a future to the result of the call. The context's sender is set to `nil`. If an actor does not exist at the exact address we attempt to lookup and send the message to an actor registered at the sibling wildcard address instead.

func (*System) Get

func (s *System) Get(address Address) *Ref

Get returns the actor reference with the id, or nil if no actor with that id is found.

func (*System) MustActorOf

func (s *System) MustActorOf(address Address, actor Actor) *Ref

MustActorOf adds the actor with the provided address. It panics if a new actor was not created.

func (*System) Tell

func (s *System) Tell(actor *Ref, message Message)

Tell sends the specified message to the actor (fire-and-forget semantics). The context's sender is set to `nil`.

func (*System) TellAt

func (s *System) TellAt(addr Address, message Message)

TellAt sends the specified message to the actor (fire-and-forget semantics) at the provided address. The context's sender is set to `nil`.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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