Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type GenServer ¶
The GenServer interface defines the init method for a custom GenServer. The reason just the init method is defined, is that everything else is parsed out via reflection. This is also the reason why the GenServer has its own package (because it is way more complex than any other component).
Communication types ¶
There are 3 ways of how to communicate with a GenServer. The first one is via a Call. A Call is synchronous (meaning it will only return whenever the actual actor is done with whatever it was supposed to do). Calls are blocking but good if you need to know that a message was definitely processed.
The second way of communicating with a GenServer is a cast. A Cast is partly synchronous (it will return after the GenServer has received the message and is about to start processing it). Casts are great when you only care about the actor receiving a message but not if the operation was successful.
The third and final way is a normal send (the handlers for which are postfixed with "Info"). This is completely asynchronous and acts like any other actor would (just that a GenServer offers some more framework sugar to make it easier to work with).
Usage ¶
The handlers links for a custom GenServer are described via the method names. The general format of a GenServer handler is:
Handle + MessageType + (Call | Cast | Info)
So to handle a GenericMessage Cast, the method name would look like so:
func (m myGenServer) HandleGenericMessageCast(ctx *Context, message GenericMessage)
And a handler for a KillMessage Call would look like this:
func (m myGenServer) HandleKillMessageCall(ctx *Context, message KillMessage) Message
A default handler for a DownMessage would look like this:
func (m myGenServer) HandleDownMessageInfo(ctx *Context, message DownMessage)
Note that the Call method returns a message, while the normal send handler (Info) and the Cast handler don't. This is because a Call is the only GenServer operation that can directly return something to the sender.
You can optionally define "catch-all" handlers by leaving out the message type:
func (m myGenServer) HandleCast(ctx *Context, message Message) func (m myGenServer) HandleCall(ctx *Context, message Message) Message func (m myGenServer) HandleInfo(ctx *Context, message Message)
type ReceivedMessage ¶
type ReceivedMessage struct { }
The ReceivedMessage struct is the acknowledgement a Cast operation returns when the GenServer has received a message.
func Cast ¶
func Cast(context quacktors.Context, pid *quacktors.Pid, message quacktors.Message) (ReceivedMessage, error)
Cast sends a message to the GenServer and blocks until the GenServer has received the message and is about to start processing the it. If there was an error, the GenServer went down or the PID was dead to begin with, Cast returns a non-nil error. Otherwise the error is nil. This operation is blocking (if only for a very short time) and should be used if you need to make sure a GenServer has received a message but don't care whether the GenServer has failed or not.
func CastWithTimeout ¶ added in v0.0.6
func CastWithTimeout(context quacktors.Context, pid *quacktors.Pid, message quacktors.Message, duration time.Duration) (ReceivedMessage, error)
CastWithTimeout is the same as Cast but also accepts a duration. If the GenServer doesn't return a result within the timeout period, an error is returned.
func (ReceivedMessage) Type ¶
func (r ReceivedMessage) Type() string
Type of ReceivedMessage returns "ReceivedMessage"
type ResponseMessage ¶
The ResponseMessage struct is returned as the result type of a Call operation on a GenServer.
func Call ¶
func Call(context quacktors.Context, pid *quacktors.Pid, message quacktors.Message) (ResponseMessage, error)
Call sends a message to the GenServer and blocks until the operation was completed by the GenServer and the GenServer returned a result. If there was an error, the GenServer went down or the PID was dead to begin with, Call returns an empty response message and a non-nil error. Otherwise the error is nil. This operation is blocking and should be used if you need to make sure a GenServer has processed a message.
func CallWithTimeout ¶ added in v0.0.6
func CallWithTimeout(context quacktors.Context, pid *quacktors.Pid, message quacktors.Message, duration time.Duration) (ResponseMessage, error)
CallWithTimeout is the same as Call but also accepts a duration. If the GenServer doesn't return a result within the timeout period, an error is returned.
func (ResponseMessage) Type ¶
func (r ResponseMessage) Type() string
Type of ResponseMessage returns "ResponseMessage"