Links And Monitors
Linking and Monitoring Mechanisms
These mechanisms allow processes to respond to termination events of targets with which links or monitors have been created. Such targets can include:
A process identifier (
gen.PID
)A process name (
gen.Atom
orgen.ProcessID
)A process alias (
gen.Alias
)An event (
gen.Event
)A node name (
gen.Atom
, in the case of monitoring a node connection)
The key difference between linking and monitoring lies in the response to the termination event of the target. When a process creates a link with a target, and that target terminates, the linked process receives an exit signal, which generally causes it to terminate as well.
In contrast, when a process creates a monitor with a target, and the target terminates, the monitoring process receives a gen.MessageDown*
message, which contains the reason for the target's termination. This notification allows the monitoring process to react accordingly without necessarily terminating itself.
Links
In Ergo Framework, links are created between processes to ensure that termination events of one process propagate to linked processes. The gen.Process
interface provides the following methods for creating and managing links:
Link
: This universal method allows you to create a link to a target, which can be agen.Atom
(for a local process),gen.PID
,gen.ProcessID
, orgen.Alias
. The process will receive an exit signal upon the target process's termination. To remove the link, use theUnlink
method with the same argument.LinkPID
: Creates a link to a process by itsgen.PID
. The process will receive an exit signal upon the target process's termination. To remove the link, useUnlinkPID
orUnlink
with the same argument.LinkProcessID
: Creates a link to a process by itsgen.ProcessID
. The process will receive an exit signal upon the target process's termination or when the associated name is unregistered. To remove the link, useUnlinkProcessID
orUnlink
.LinkAlias
: Creates a link to a process by itsgen.Alias
. The process will receive an exit signal upon the termination of the process that created the alias or upon the alias's deletion. To remove the link, useUnlinkAlias
orUnlink
.LinkNode
: Creates a link to a node connection using the node name (gen.Atom
). The process will receive an exit signal if the connection to the target node is lost. To remove the link, useUnlinkNode
.LinkEvent
: Creates a link to an event (gen.Event
). The process will receive an exit signal when the producer process of the event terminates or when thegen.Event
is unregistered. To remove the link, useUnlinkEvent
. More information about this mechanism can be found in the Events section.
Exit signals are always delivered with the highest priority and placed in the Urgent queue of the process's mailbox.
In Erlang, the linking mechanism is bidirectional, meaning that if a process that created a link with a target process terminates, it will also cause the termination of the target process.
In contrast, in Ergo Framework, the linking mechanism is unidirectional. The termination of the process that created the link has no effect on the target process. Only the termination of the target process triggers an exit signal to the linked process. This design allows more flexibility and control, preventing unintended cascading failures in linked processes.
Monitors
Monitors allow a process to observe the lifecycle of a target process or event. The gen.Process
interface provides several methods for creating monitors:
Monitor
: A universal method for creating a monitor. The target can be agen.Atom
(for a local process),gen.PID
,gen.ProcessID
, orgen.Alias
. The monitoring process will receive agen.MessageDown*
message upon the termination of the target process. To remove the monitor, use theDemonitor
method with the same argument.MonitorPID
: Creates a monitor for a process by itsgen.PID
. The monitoring process will receive agen.MessageDownPID
message upon the target process's termination, with the reason for termination included in theReason
field. To remove the monitor, useDemonitorPID
orDemonitor
.MonitorProcessID
: Creates a monitor for a process by itsgen.ProcessID
. The monitoring process will receive agen.MessageDownProcessID
message upon the target process's termination, with the reason for termination in theReason
field. If the target process unregisters its name, theReason
field of thegen.MessageDownProcessID
will containgen.ErrUnregistered
. To remove the monitor, useDemonitorProcessID
orDemonitor
.MonitorAlias
: Creates a monitor for a process by itsgen.Alias
. The monitoring process will receive agen.MessageDownAlias
message upon the termination of the process that created the alias or if the alias is deleted. In the case of alias deletion, theReason
field of thegen.MessageDownAlias
will containgen.ErrUnregistered
. To remove the monitor, useDemonitorAlias
orDemonitor
.MonitorNode
: Creates a monitor for a node connection. The monitoring process will receive agen.MessageDownNode
message upon the disconnection of the node. To remove the monitor, useDemonitorNode
.MonitorEvent
: Creates a monitor for an event (gen.Event
). The monitoring process will receive agen.MessageDownEvent
message when the producer of the event terminates or if the event is unregistered. If the event is unregistered, theReason
field of thegen.MessageDownEvent
will containgen.ErrUnregistered
. To remove the monitor, useDemonitorEvent
. More details about this mechanism can be found in the Events section.
Messages of type gen.MessageDown*
are delivered with high priority and placed in the System queue of the process's mailbox.
Remote targets
Thanks to network transparency, the linking and monitoring mechanisms in Ergo Framework can be used with targets on remote nodes.
When a process creates a link or monitor with a remote target (such as a process, alias, or event), if the connection to the node where the target resides is lost, the process will receive an exit signal or a gen.MessageDown*
message, depending on the mechanism used. In this case, the reason for termination will be gen.ErrNoConnection
. This allows processes to handle network disconnections and remote target failures seamlessly, as part of the same fault-tolerant mechanisms used for local processes.
It's important to remember that the methods for creating links and monitors are not available to a process during its initialization state (see the Process section for more details).
Last updated