macula_gateway_mesh (macula v0.20.5)
View SourceMesh Connection Manager GenServer - manages peer-to-peer QUIC connections.
Responsibilities: - Pool QUIC connections to remote peers by node_id (BOUNDED POOL) - Enforce max_connections limit with LRU eviction - Check connection liveness before reuse - Open new streams on pooled connections - Monitor connection processes for automatic cleanup - Cache connection metadata with timestamps
Pattern: Bounded connection pooling with LRU eviction - Cache connections by node_id (max: max_mesh_connections, default 1000) - Evict least recently used when pool is full - Verify liveness before reuse (open new stream) - Remove dead connections and recreate on demand
Configuration: - max_mesh_connections: Maximum pooled connections (default: 1000)
Extracted from macula_gateway.erl (Phase 9)
Summary
Functions
Get connection metadata for a node.
Get existing connection or create new one. Returns opened stream ready for use.
Handle async send request - spawns process for connection + send. Fire-and-forget pattern - does not block the gen_server.
List all cached connections.
Explicitly remove connection from cache.
Send message asynchronously (fire-and-forget). Connection creation and sending happens in a spawned process. Does NOT block the caller - returns immediately with 'ok'. Use this for pubsub_route and other non-critical messages.
Start the mesh connection manager with options. Options: - cert_file: Path to TLS certificate - key_file: Path to TLS private key Registers as 'macula_gateway_mesh' for discovery by pubsub module.
Stop the mesh connection manager.
Types
-type connection_info() :: #{connection => pid() | reference() | undefined, stream => pid() | reference() | undefined, address => {inet:ip_address(), inet:port_number()}, last_used => integer()}.
Functions
-spec get_connection_info(pid(), binary()) -> {ok, connection_info()} | not_found.
Get connection metadata for a node.
-spec get_or_create_connection(pid(), binary(), {inet:ip_address(), inet:port_number()}) -> {ok, pid()} | {error, term()}.
Get existing connection or create new one. Returns opened stream ready for use.
Handle async send request - spawns process for connection + send. Fire-and-forget pattern - does not block the gen_server.
-spec list_connections(pid()) -> {ok, [{binary(), connection_info()}]}.
List all cached connections.
Explicitly remove connection from cache.
-spec send_async(pid(), binary(), binary() | {inet:ip_address(), inet:port_number()}, binary()) -> ok.
Send message asynchronously (fire-and-forget). Connection creation and sending happens in a spawned process. Does NOT block the caller - returns immediately with 'ok'. Use this for pubsub_route and other non-critical messages.
Start the mesh connection manager with options. Options: - cert_file: Path to TLS certificate - key_file: Path to TLS private key Registers as 'macula_gateway_mesh' for discovery by pubsub module.
-spec stop(pid()) -> ok.
Stop the mesh connection manager.