macula_connection_upgrade (macula v0.20.5)

View Source

Connection Upgrade Manager.

Handles upgrading connections from relay (indirect) to direct when hole punching succeeds. This is a key optimization for reducing latency and bootstrap load.

Upgrade Flow: 1. Connection established via relay (fallback) 2. Hole punch attempt happens in background 3. On success, upgrade_to_direct/2 is called 4. Messages are seamlessly transitioned to direct connection 5. Relay connection is gracefully closed

Key Features: - Message ordering preserved during upgrade - No message loss during transition - Automatic fallback if direct connection fails - Metrics tracking for upgrade success/failure

Summary

Functions

Get upgrade statistics.

Register a relay connection for potential future upgrade. Called when connection is established via relay fallback.

Start the connection upgrade manager.

Unregister a relay connection (closed or upgraded).

Upgrade a relay connection to direct. PeerId - The remote peer's ID RelayConn - Current relay connection handle DirectConn - New direct connection handle from hole punch

Functions

get_upgrade_stats()

-spec get_upgrade_stats() -> map().

Get upgrade statistics.

handle_call(Request, From, State)

handle_cast(Msg, State)

handle_info(Info, State)

init(_)

register_relay(PeerId, RelayConn, Endpoint)

-spec register_relay(binary(), term(), {inet:ip_address(), inet:port_number()}) -> ok.

Register a relay connection for potential future upgrade. Called when connection is established via relay fallback.

start_link()

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

Start the connection upgrade manager.

terminate(Reason, State)

unregister_relay(PeerId)

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

Unregister a relay connection (closed or upgraded).

upgrade_to_direct(PeerId, RelayConn, DirectConn)

-spec upgrade_to_direct(binary(), term(), term()) -> ok | {error, no_relay | upgrade_failed}.

Upgrade a relay connection to direct. PeerId - The remote peer's ID RelayConn - Current relay connection handle DirectConn - New direct connection handle from hole punch