feat(federation): implement onPeerActivated with dedup

Two-invariant handler: resetOutboxBackoff + syncPeerMutationLog.
In-flight map keyed by peerId coalesces concurrent activations —
a second call for a peer whose activation is still running shares
the same promise. Errors are swallowed and logged — the handler
never throws so fire-and-forget callers at HTTP handler sites
are safe.
This commit is contained in:
Jannis Braun
2026-04-22 00:24:57 +02:00
parent 39c43032e8
commit cca2245cdf
2 changed files with 77 additions and 3 deletions
@@ -42,9 +42,26 @@ export async function onPeerActivated(
peerId: string,
reason: PeerActivationReason,
): Promise<void> {
// Stub — implemented in Task 4.
void peerId;
void reason;
const existing = inFlightActivation.get(peerId);
if (existing) return existing;
const promise = (async () => {
try {
resetOutboxBackoff(peerId);
await syncPeerMutationLog(peerId, reason);
const { connectionManager } = await import('../ws/handler.js');
connectionManager.sendToAdmins({ type: 'federation_peers_changed' as const });
} catch (err) {
console.error(`[federation] onPeerActivated(${peerId}, ${reason}) failed:`, err);
}
})();
inFlightActivation.set(peerId, promise);
try {
await promise;
} finally {
inFlightActivation.delete(peerId);
}
}
/**