Module em_pop_sup

em_pop_sup — Supervisor for Population Protocol nodes.

Behaviours: supervisor.

Authors: Steve Roques.

Description

em_pop_sup — Supervisor for Population Protocol nodes

One em_pop node (em_pop_node gen_server + Cowboy listener) is started per agent that includes a pop_port key in its Config map.

ETS registry

An ETS table named em_pop_agent_nodes maps:

AgentName (atom) → PopNodePid (pid)

The table is public with read_concurrency so that em_filter:pop_peers/1 and friends can do O(1) lookups without going through a gen_server call.

The table is created inside start_link/0 with an existence check, so supervisor restarts (which would call start_link/0 again) do not crash on a duplicate table name.

Restart strategy

simple_one_for_one — each dynamically added child is an independent em_pop_node. Restart is transient: a node that exits normally (e.g. via stop_node/1) is not restarted, but an unexpected crash will trigger a restart.

This supervisor is started as a permanent child of em_filter_sup at application boot, before any agent workers are created.

Function Index

get_node/1Return the em_pop node pid for AgentName, or undefined.
start_link/0Start the supervisor and create the ETS registry if needed.
start_node/2Start an em_pop node for AgentName with the given Opts map.
stop_node/1Stop the em_pop node for AgentName.

Function Details

get_node/1

get_node(AgentName::atom()) -> pid() | undefined

Return the em_pop node pid for AgentName, or undefined.

O(1) ETS lookup — safe to call from any process at any time.

start_link/0

start_link() -> {ok, pid()} | {error, term()}

Start the supervisor and create the ETS registry if needed.

The ETS table is created here rather than in init/1 because init/1 runs inside the new supervisor process, whereas we want the table to be owned by the calling process (the application master) so it survives supervisor crashes and restarts.

start_node/2

start_node(AgentName::atom(), Opts::map()) -> {ok, pid()} | {error, term()}

Start an em_pop node for AgentName with the given Opts map.

Delegates to supervisor:start_child/2 and registers the resulting pid in the ETS registry so it can be found quickly by name.

Opts must contain at least: port => pos_integer() — TCP port for the gossip HTTP listener vector => binary() — f32 capability vector (unit norm)

Optional keys: stale_timeout => pos_integer() — default 30 000 ms gossip_interval => non_neg_integer() — default 5 000 ms max_peers => pos_integer() — default 200

stop_node/1

stop_node(AgentName::atom()) -> ok

Stop the em_pop node for AgentName.

Removes the entry from the ETS registry and terminates the child process. No-op if AgentName has no registered node.

The child's restart => transient strategy ensures the supervisor does not try to restart it after a normal termination.


Generated by EDoc