Skip to content

Commit

Permalink
Trigger new reconnect if failure to connect in reconnect; (#538)
Browse files Browse the repository at this point in the history
* Trigger new reconnect if failure to connect in reconnect;

* More potential fixing;

* Fix unrelated thread catchup;

* Added same reconnect approach to the regular websocket; Added clearing of the reconnectCounter
  • Loading branch information
stef-coenen authored Jan 22, 2025
1 parent 1dc4373 commit 68a36f0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
useDotYouClientContext,
useFile,
bytesToSize,
formatDateExludingYearIfCurrent,
} from '@homebase-id/common-app';
import { ImageSource, OdinPayloadImage, OdinPreviewImage } from '@homebase-id/ui-lib';
import { useNavigate, useParams } from 'react-router-dom';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ export const CommunityHistory = memo(
});

flat.sort((a, b) => b.fileMetadata.created - a.fileMetadata.created);
if (inAThread) flat.push(origin as HomebaseFile<CommunityMessage>);
if (inAThread) flat.push(origin);
if (!maxShowOptions) return [flat, slicedCount];

const maxShow = maxShowOptions.count;
return [
flat.slice(0, maxShow),
slicedCount + (flat.length > maxShow ? flat.length - maxShow : 0),
];
const slicedMsgs = flat.slice(0, maxShow);
if (inAThread && slicedMsgs.indexOf(origin) === -1) {
slicedMsgs.push(origin);
}

return [slicedMsgs, slicedCount + (flat.length > maxShow ? flat.length - maxShow : 0)];
}, [messages, origin, maxShowOptions, inAThread, maxAge]);

useEffect(() => {
Expand Down
19 changes: 15 additions & 4 deletions packages/libs/js-lib/src/core/WebsocketData/WebsocketProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const ConnectSocket = async (
.post('/notify/preauth', undefined, {
validateStatus: () => true,
})
.catch((error) => {
console.error({ error });
.catch(() => {
reconnectPromise = undefined;
reject('[WebsocketProvider] Preauth failed');
});
}
Expand Down Expand Up @@ -115,6 +115,7 @@ const ConnectSocket = async (
if (notification.notificationType == 'deviceHandshakeSuccess') {
if (isDebug) console.debug(`[WebsocketProvider] Device handshake success`);
isHandshaked = true;
reconnectCounter = 0;
setupPing();
resolve();
return;
Expand Down Expand Up @@ -146,6 +147,8 @@ const ConnectSocket = async (
ReconnectSocket(dotYouClient, drives, args);
};
});

return connectPromise;
};

const ReconnectSocket = async (
Expand All @@ -155,7 +158,7 @@ const ReconnectSocket = async (
) => {
if (reconnectPromise) return;

reconnectPromise = new Promise<void>((resolve) => {
reconnectPromise = new Promise<void>((resolve, reject) => {
if (isDebug) console.debug('[WebsocketProvider] Reconnecting - Force disconnect');
reconnectCounter++;
subscribers.map((subscriber) => subscriber.onDisconnect && subscriber.onDisconnect());
Expand All @@ -170,7 +173,15 @@ const ReconnectSocket = async (
setTimeout(async () => {
if (isDebug) console.debug('[WebsocketProvider] Reconnecting - Delayed reconnect');

await ConnectSocket(dotYouClient, drives, args);
try {
await ConnectSocket(dotYouClient, drives, args);
} catch (e) {
console.error('[WebsocketProvider] Reconnect failed', e);
reject();

ReconnectSocket(dotYouClient, drives, args);
return;
}
subscribers.map((subscriber) => subscriber.onReconnect && subscriber.onReconnect());

resolve();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const ConnectSocket = async (
});

if (!tokenToConnectOverPeer) {
reconnectPromise = undefined;
reject('[WebsocketProviderOverPeer] Preauth failed');
return;
}
Expand Down Expand Up @@ -146,6 +147,7 @@ const ConnectSocket = async (
if (notification.notificationType == 'deviceHandshakeSuccess') {
if (isDebug) console.debug(`[WebsocketProviderOverPeer] Device handshake success`);
isHandshaked = true;
reconnectCounter = 0;
setupPing();
resolve();
return;
Expand Down Expand Up @@ -177,6 +179,8 @@ const ConnectSocket = async (
ReconnectSocket(dotYouClient, odinId, drives, args);
};
});

return connectPromise;
};

const ReconnectSocket = async (
Expand All @@ -187,7 +191,7 @@ const ReconnectSocket = async (
) => {
if (reconnectPromise) return;

reconnectPromise = new Promise<void>((resolve) => {
reconnectPromise = new Promise<void>((resolve, reject) => {
if (isDebug) console.debug('[WebsocketProviderOverPeer] Reconnecting - Force disconnect');
reconnectCounter++;
subscribers.map((subscriber) => subscriber.onDisconnect && subscriber.onDisconnect());
Expand All @@ -201,8 +205,15 @@ const ReconnectSocket = async (
// Delay the reconnect to avoid a tight loop on network issues
setTimeout(async () => {
if (isDebug) console.debug('[WebsocketProviderOverPeer] Reconnecting - Delayed reconnect');

await ConnectSocket(dotYouClient, odinId, drives, args);
try {
await ConnectSocket(dotYouClient, odinId, drives, args);
} catch (e) {
console.error('[WebsocketProviderOverPeer] Reconnect failed', e);
reject();

ReconnectSocket(dotYouClient, odinId, drives, args);
return;
}
subscribers.map((subscriber) => subscriber.onReconnect && subscriber.onReconnect());

resolve();
Expand Down

0 comments on commit 68a36f0

Please sign in to comment.