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:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user