feat(routing): rescue blocked relay admission (#68)

This commit is contained in:
AAA authored and GitHub committed 2026-08-20 15:12:11 +08:00 Unverified
1 parent 6351b4463c
commit 31bee238bc
14 files changed
+372 -39

No files matched your search

+12 -16
View File
@@ -75,8 +75,8 @@ import type {
} from "../types";
import { HostPeer } from "../webrtc/host-peer";
import {
limitMediaAssignment,
MAX_HOST_MEDIA_CHILDREN,
reconcileBoundedMediaChildren,
} from "../webrtc/media-assignment";
import { sourceSwitchNotice } from "./host-page-notices";
@@ -757,23 +757,19 @@ export function HostPage({ onAuthorizationRequired }: HostPageProps = {}) {
childPeerIds: string[],
generation: number,
): void {
const assignment = limitMediaAssignment(
{ parentPeerId: null, childPeerIds },
reconcileBoundedMediaChildren(
peersRef.current.keys(),
childPeerIds,
MAX_HOST_MEDIA_CHILDREN,
removePeer,
(peerId) => {
void startPeer(peerId, generation).catch((error: unknown) => {
if (isCurrentGeneration(generation)) {
setNotice(readableError(error));
}
});
},
);
const assignedChildren = new Set(assignment.childPeerIds);
for (const peerId of peersRef.current.keys()) {
if (!assignedChildren.has(peerId)) {
removePeer(peerId);
}
}
for (const peerId of assignedChildren) {
void startPeer(peerId, generation).catch((error: unknown) => {
if (isCurrentGeneration(generation)) {
setNotice(readableError(error));
}
});
}
}
function handleSignalMessage(
+23
View File
@@ -17,6 +17,29 @@ export function limitMediaAssignment(
};
}
export function reconcileBoundedMediaChildren(
currentPeerIds: Iterable<string>,
nextChildPeerIds: readonly string[],
maxChildren: number,
removeChild: (peerId: string) => void,
startChild: (peerId: string) => void,
): void {
const nextPeerIds = new Set(
limitMediaAssignment(
{ parentPeerId: null, childPeerIds: [...nextChildPeerIds] },
maxChildren,
).childPeerIds,
);
for (const peerId of currentPeerIds) {
if (!nextPeerIds.has(peerId)) {
removeChild(peerId);
}
}
for (const peerId of nextPeerIds) {
startChild(peerId);
}
}
export function viewerSignalMessage(
peerAssisted: boolean,
targetPeerId: string,
+16
View File
@@ -512,11 +512,27 @@ export class HybridMediaRouter {
this.relayCapacitySessionsByRoom.set(participant.roomId, advertised);
}
advertised.set(participant.peerId, participant.sessionId);
const controller = this.mediaRouteControllers.get(participant.roomId);
const active = controller?.getActiveRoute();
const routeIntent = this.viewerRouteIntentsByRoom
.get(participant.roomId)
?.get(participant.peerId);
const allowAdmissionRescue =
downstreamEdges === 1 &&
active?.sfu.publicationGeneration === null &&
active.assignments.get(participant.peerId)?.upstream.kind === "none" &&
controller?.getPendingRoute() === undefined &&
!this.pendingRoutePreparations.has(participant.roomId) &&
(routeIntent === undefined || routeIntent.failedParentPeerId === null) &&
!this.failedParentPeerIdsByViewer.get(
viewerConnectionKey(participant.roomId, participant.peerId),
)?.size;
const changes = this.peerRelayTopology.setViewerRelayCapacity(
participant.roomId,
participant.peerId,
downstreamEdges,
this.connectedPeerIds(participant.roomId),
{ rescueUnassignedRelay: allowAdmissionRescue },
);
if (changes.length > 0) {
this.clearChangedConnectionIds(participant.roomId, changes);
+58
View File
@@ -9,6 +9,10 @@ export interface MediaAssignmentChange {
mediaAssignment: MediaAssignment;
}
export interface RelayCapacityUpdateOptions {
rescueUnassignedRelay?: boolean;
}
export const MAX_PEER_RELAY_DEPTH = 4;
interface RelayViewer {
@@ -87,6 +91,7 @@ export class PeerRelayTopology {
peerId: string,
downstreamEdges: RelayDownstreamEdges,
connectedPeerIds: ReadonlySet<string>,
options: RelayCapacityUpdateOptions = {},
): MediaAssignmentChange[] {
const room = this.rooms.get(roomId);
const viewer = room?.viewers.get(peerId);
@@ -95,12 +100,20 @@ export class PeerRelayTopology {
}
const before = snapshot(room);
const previousDownstreamEdges = viewer.downstreamEdges;
viewer.downstreamEdges = downstreamEdges;
for (const [candidatePeerId, candidate] of orderedViewers(room)) {
if (candidate.parentPeerId === null) {
this.assignViewer(room, candidatePeerId, connectedPeerIds);
}
}
if (
options.rescueUnassignedRelay &&
previousDownstreamEdges === 0 &&
downstreamEdges === 1
) {
this.rescueUnassignedRelay(room, peerId, connectedPeerIds);
}
return changedAssignments(before, snapshot(room));
}
@@ -284,6 +297,51 @@ export class PeerRelayTopology {
children.splice(childIndex, 1);
}
}
private rescueUnassignedRelay(
room: RelayRoom,
peerId: string,
connectedPeerIds: ReadonlySet<string>,
): void {
const hostPeerId = room.hostPeerId;
const candidate = room.viewers.get(peerId);
if (
!hostPeerId ||
!candidate ||
!connectedPeerIds.has(hostPeerId) ||
!connectedPeerIds.has(peerId) ||
candidate.parentPeerId !== null ||
candidate.childPeerIds.length !== 0 ||
candidate.downstreamEdges !== 1 ||
room.hostChildPeerIds.length !== downstreamCapacity(room, hostPeerId)
) {
return;
}
const leafPeerId = room.hostChildPeerIds.find((childPeerId) => {
const child = room.viewers.get(childPeerId);
return (
connectedPeerIds.has(childPeerId) &&
child?.parentPeerId === hostPeerId &&
child.childPeerIds.length === 0 &&
child.downstreamEdges === 0
);
});
if (!leafPeerId) {
return;
}
const leaf = room.viewers.get(leafPeerId)!;
const leafIndex = room.hostChildPeerIds.indexOf(leafPeerId);
room.hostChildPeerIds.splice(leafIndex, 1, peerId);
room.hostChildPeerIds.sort(
(left, right) =>
room.viewers.get(left)!.order - room.viewers.get(right)!.order,
);
candidate.parentPeerId = hostPeerId;
candidate.childPeerIds.push(leafPeerId);
leaf.parentPeerId = peerId;
}
}
function findShallowestAvailableParent(