macula_peer_connection_pool (macula v0.20.5)

View Source

Peer Connection Pool - Manages pooled QUIC connections to remote peers.

Problem: Creating a new QUIC connection per message adds ~50-200ms latency. Solution: Pool connections and reuse them for subsequent messages.

Design: - ETS-based connection pool for O(1) lookups - LRU eviction when pool is full - Automatic connection health monitoring - Configurable pool size and idle timeout

Expected improvement: 1.5-2x latency reduction for repeated messaging.

Summary

Functions

Get list of peer IDs with active connections. Returns list of peer IDs that have pooled connections.

Get a connection to an endpoint (from pool or create new). Returns {ok, Connection, Stream} or {error, Reason}.

Invalidate (remove) a connection from the pool. Called when a connection fails or is no longer valid.

Put a connection directly into the pool (NAT system API). Used by connection upgrade to store successfully upgraded direct connections.

Return a connection to the pool for reuse.

Start the connection pool with default options.

Start the connection pool with options. Options: - max_connections: Maximum pooled connections (default: 100) - idle_timeout_ms: Idle connection timeout (default: 60000)

Get pool statistics.

Functions

get_connected_peers()

-spec get_connected_peers() -> [binary()].

Get list of peer IDs with active connections. Returns list of peer IDs that have pooled connections.

get_connection(Endpoint)

-spec get_connection(binary()) -> {ok, term(), term()} | {error, term()}.

Get a connection to an endpoint (from pool or create new). Returns {ok, Connection, Stream} or {error, Reason}.

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

init(Opts)

invalidate(Endpoint)

-spec invalidate(binary()) -> ok.

Invalidate (remove) a connection from the pool. Called when a connection fails or is no longer valid.

put(PeerId, Connection)

-spec put(binary(), term()) -> ok.

Put a connection directly into the pool (NAT system API). Used by connection upgrade to store successfully upgraded direct connections.

return_connection(Endpoint, _)

-spec return_connection(binary(), {term(), term()}) -> ok.

Return a connection to the pool for reuse.

start_link()

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

Start the connection pool with default options.

start_link(Opts)

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

Start the connection pool with options. Options: - max_connections: Maximum pooled connections (default: 100) - idle_timeout_ms: Idle connection timeout (default: 60000)

stats()

-spec stats() -> map().

Get pool statistics.

terminate(Reason, State)