Skip to content

armgit / Piik

Public Imported from GitHub
0

Free, open-source screen sharing for private live streams with friends. Watch together in a browser or self-host Piik. 免费开源的私密屏幕共享,支持游戏直播、一起看电影与自部署。

fix: allow bounded NAT route recovery

AAA5e67d05a9097
diff --git a/docs/adr/0009-optional-nat-prediction.md b/docs/adr/0009-optional-nat-prediction.md
index 0eea44b7..96f91986 100644
--- a/docs/adr/0009-optional-nat-prediction.md
+++ b/docs/adr/0009-optional-nat-prediction.md
@@ -58,6 +58,11 @@ connection acquisition reuses the route controller's existing opportunity ledger
the remaining attempts behind a working SFU route. A new connection can
provide another mapping opportunity, not guaranteed independent randomness:
in particular, Native connections may retain the same shared UDP socket.
+ When prediction is enabled, that route-owned Browser P2P edge may also use
+ the existing bounded automatic-recovery budget (two ICE restarts) before
+ reporting the edge failed. Each restart creates a fresh ICE gathering
+ generation without adding a route candidate, SFU fallback, or second retry
+ state; unsuccessful recovery follows the normal route-failed path.
## Consequences
diff --git a/docs/product/routing-transport.md b/docs/product/routing-transport.md
index 40e96d96..bc74b52d 100644
--- a/docs/product/routing-transport.md
+++ b/docs/product/routing-transport.md
@@ -98,6 +98,10 @@ begins only after that exact candidate commits as the active route.
When NAT traversal is enabled, another operation may use the remaining
connection-attempt budget after rollback. The waiting display uses the actual
server-issued attempt ordinal; it never counts time as an attempted connection.
+When NAT prediction is enabled, a route-owned P2P candidate may also use its
+existing bounded automatic-recovery budget (two ICE restarts) before it reports
+that failure; each restart creates a fresh mapping generation without reopening
+the route candidate or introducing a second reconciliation loop.
When a newly committed Host-root Viewer exposes unused downstream capacity while
another Host root has at least two direct children, the same background operation may
diff --git a/src/client/webrtc/viewer-peer.ts b/src/client/webrtc/viewer-peer.ts
index 6f22f17d..a19a6a52 100644
--- a/src/client/webrtc/viewer-peer.ts
+++ b/src/client/webrtc/viewer-peer.ts
@@ -440,7 +440,14 @@ export class ViewerPeer implements ViewerMediaPeer {
if (this.recoveryOwner === "route") {
this.clearDisconnectTimer();
if (state === "failed") {
- this.reportRecoveryExhausted();
+ if (
+ this.natPredictionEnabled &&
+ this.automaticRecoveryRequests < VIEWER_MAX_AUTOMATIC_RECOVERY_REQUESTS
+ ) {
+ this.attemptAutomaticRecovery();
+ } else {
+ this.reportRecoveryExhausted();
+ }
}
return;
}
@@ -475,7 +482,10 @@ export class ViewerPeer implements ViewerMediaPeer {
this.reportRecoveryExhausted();
return;
}
- const rebuild = this.automaticRecoveryRequests > 0;
+ const rebuild =
+ this.recoveryOwner === "route"
+ ? false
+ : this.automaticRecoveryRequests > 0;
if (
!this.events.sendRestartRequest(
this.parentPeerId,
diff --git a/tests/viewer-peer-generation.test.ts b/tests/viewer-peer-generation.test.ts
index c1d3ed6c..50d3f6d3 100644
--- a/tests/viewer-peer-generation.test.ts
+++ b/tests/viewer-peer-generation.test.ts
@@ -471,7 +471,7 @@ describe("ViewerPeer connection generations", () => {
expect(timeoutDelays.get(2)).toBe(3_000);
});
- it("leaves a pending route candidate under the route deadline", async () => {
+ it("gives a NAT-enabled pending route one bounded ICE retry", async () => {
const restartRequests: string[] = [];
const exhausted: string[] = [];
const peer = new ViewerPeer(
@@ -489,7 +489,7 @@ describe("ViewerPeer connection generations", () => {
return true;
},
},
- { recoveryOwner: "route" },
+ { recoveryOwner: "route", natPredictionEnabled: true },
);
await peer.acceptSignal("host", offer("route-candidate"));
@@ -503,7 +503,14 @@ describe("ViewerPeer connection generations", () => {
connection.connectionState = "failed";
connection.dispatchEvent(new Event("connectionstatechange"));
- expect(restartRequests).toEqual([]);
+ expect(restartRequests).toEqual(["route-candidate"]);
+ expect(exhausted).toEqual([]);
+
+ timeoutCallbacks.get(1)!();
+ expect(restartRequests).toEqual(["route-candidate", "route-candidate"]);
+ expect(exhausted).toEqual([]);
+
+ timeoutCallbacks.get(2)!();
expect(exhausted).toEqual(["route-candidate"]);
});