mirror of
https://github.com/Moepchi/webspeak3.git
synced 2026-12-03 18:02:49 +08:00
fix(web): only reset the reconnect backoff after a stable connection
On a flaky network every successful reconnect reset the backoff to 1 s, so a connection that kept dropping after a few seconds made each tab reconnect every few seconds. The backoff now starts over only once a connection held for 30 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Hu2cuPLqpuKVfd9wWFFakU
This commit is contained in:
1 parent
c2b52b323d
commit
1e71e43bd7
1 file changed
+12
-2
+12
-2
@@ -7530,7 +7530,7 @@ function AppInner() {
|
|||||||
connecting: rec.parked.connecting,
|
connecting: rec.parked.connecting,
|
||||||
});
|
});
|
||||||
if (data.type === "connected") {
|
if (data.type === "connected") {
|
||||||
reconnectAttemptsRef.current.delete(sessionId);
|
connectedAtRef.current.set(sessionId, Date.now());
|
||||||
void playSound("connect");
|
void playSound("connect");
|
||||||
}
|
}
|
||||||
else if (data.type === "poke") void playSound("poke");
|
else if (data.type === "poke") void playSound("poke");
|
||||||
@@ -7540,7 +7540,7 @@ function AppInner() {
|
|||||||
switch (data.type) {
|
switch (data.type) {
|
||||||
case "connected":
|
case "connected":
|
||||||
logClient("info", "Connection", `Connected to ${data.serverName}`);
|
logClient("info", "Connection", `Connected to ${data.serverName}`);
|
||||||
reconnectAttemptsRef.current.delete(sessionId);
|
connectedAtRef.current.set(sessionId, Date.now());
|
||||||
autoConnectPendingRef.current = false;
|
autoConnectPendingRef.current = false;
|
||||||
hasConnectedRef.current = true;
|
hasConnectedRef.current = true;
|
||||||
everConnectedRef.current.add(sessionId);
|
everConnectedRef.current.add(sessionId);
|
||||||
@@ -8116,6 +8116,11 @@ function AppInner() {
|
|||||||
// instantly, which without a delay would tight-loop). Reset to 0 once a
|
// instantly, which without a delay would tight-loop). Reset to 0 once a
|
||||||
// session actually reaches "connected" again (see the two spots below).
|
// session actually reaches "connected" again (see the two spots below).
|
||||||
const reconnectAttemptsRef = useRef<Map<string, number>>(new Map());
|
const reconnectAttemptsRef = useRef<Map<string, number>>(new Map());
|
||||||
|
// The backoff only starts over once a connection held for a while: on a
|
||||||
|
// flaky network a reconnect that drops again after a few seconds used to
|
||||||
|
// reset it to 1 s every time, so each tab reconnected every few seconds.
|
||||||
|
const connectedAtRef = useRef<Map<string, number>>(new Map());
|
||||||
|
const STABLE_CONNECTION_MS = 30_000;
|
||||||
// The gateway sends this every 20 s (heartbeat in gateway/src/index.ts). A
|
// The gateway sends this every 20 s (heartbeat in gateway/src/index.ts). A
|
||||||
// network that dies silently never closes the socket on this side, so a
|
// network that dies silently never closes the socket on this side, so a
|
||||||
// socket that stays quiet this long counts as dropped.
|
// socket that stays quiet this long counts as dropped.
|
||||||
@@ -8124,6 +8129,11 @@ function AppInner() {
|
|||||||
const reconnectTimersRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());
|
const reconnectTimersRef = useRef<Map<string, ReturnType<typeof setTimeout>>>(new Map());
|
||||||
const scheduleReconnect = (sessionId: string) => {
|
const scheduleReconnect = (sessionId: string) => {
|
||||||
if (reconnectTimersRef.current.has(sessionId)) return; // already scheduled
|
if (reconnectTimersRef.current.has(sessionId)) return; // already scheduled
|
||||||
|
const connectedAt = connectedAtRef.current.get(sessionId);
|
||||||
|
connectedAtRef.current.delete(sessionId);
|
||||||
|
if (connectedAt !== undefined && Date.now() - connectedAt >= STABLE_CONNECTION_MS) {
|
||||||
|
reconnectAttemptsRef.current.delete(sessionId);
|
||||||
|
}
|
||||||
const attempt = reconnectAttemptsRef.current.get(sessionId) ?? 0;
|
const attempt = reconnectAttemptsRef.current.get(sessionId) ?? 0;
|
||||||
const delay = Math.min(1000 * 2 ** attempt, 30_000);
|
const delay = Math.min(1000 * 2 ** attempt, 30_000);
|
||||||
reconnectAttemptsRef.current.set(sessionId, attempt + 1);
|
reconnectAttemptsRef.current.set(sessionId, attempt + 1);
|
||||||
|
|||||||
Reference in new issue
Block a user