Skip to content

Commit

Permalink
fix(sdk): ensure reconnection to bridge during prolonged handshake in…
Browse files Browse the repository at this point in the history
…terruptions
  • Loading branch information
thekiba committed Jul 31, 2024
1 parent 468e4cc commit 6ff4761
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
11 changes: 7 additions & 4 deletions packages/sdk/src/provider/bridge/bridge-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export class BridgeGateway {

private readonly defaultTtl = 300;

private readonly defaultReconnectDelay = 5000;
private readonly defaultReconnectDelay = 2000;

private readonly defaultResendDelay = 5000;

private eventSource = createResource(
async (signal?: AbortSignal, openingDeadlineMS?: number): Promise<EventSource> => {
Expand Down Expand Up @@ -123,7 +125,7 @@ export class BridgeGateway {
},
{
attempts: options?.attempts ?? Number.MAX_SAFE_INTEGER,
delayMs: 5_000,
delayMs: this.defaultResendDelay,
signal: options?.signal
}
);
Expand Down Expand Up @@ -166,8 +168,9 @@ export class BridgeGateway {

private async errorsHandler(eventSource: EventSource, e: Event): Promise<EventSource | void> {
if (this.isConnecting) {
logError('Bridge error', JSON.stringify(e));
return;
eventSource.close();
logDebug(`Bridge reconnecting, ${this.defaultReconnectDelay}ms delay`);
return await this.eventSource.recreate(this.defaultReconnectDelay);
}

if (this.isReady) {
Expand Down
21 changes: 13 additions & 8 deletions packages/sdk/src/provider/bridge/bridge-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ export class BridgeProvider implements HTTPProvider {

private listeners: Array<(e: WithoutIdDistributive<WalletEvent>) => void> = [];

private readonly defaultOpeningDeadlineMS = 5000;
private readonly defaultOpeningDeadlineMS = 12000;

private readonly defaultRetryTimeoutMS = 2000;

private abortController?: AbortController;

Expand Down Expand Up @@ -110,12 +112,13 @@ export class BridgeProvider implements HTTPProvider {
await callForSuccess(
_options =>
this.openGateways(sessionCrypto, {
openingDeadlineMS: options?.openingDeadlineMS,
openingDeadlineMS:
options?.openingDeadlineMS ?? this.defaultOpeningDeadlineMS,
signal: _options?.signal
}),
{
attempts: Number.MAX_SAFE_INTEGER,
delayMs: 5_000,
delayMs: this.defaultRetryTimeoutMS,
signal: abortController.signal
}
);
Expand Down Expand Up @@ -207,7 +210,7 @@ export class BridgeProvider implements HTTPProvider {
}),
{
attempts: Number.MAX_SAFE_INTEGER,
delayMs: 5_000,
delayMs: this.defaultRetryTimeoutMS,
signal: abortController.signal
}
);
Expand All @@ -232,7 +235,9 @@ export class BridgeProvider implements HTTPProvider {
): Promise<WithoutId<WalletResponse<T>>>;
public sendRequest<T extends RpcMethod>(
request: WithoutId<AppRequest<T>>,
optionsOrOnRequestSent?: (() => void) | { attempts?: number; onRequestSent?: () => void; signal?: AbortSignal }
optionsOrOnRequestSent?:
| (() => void)
| { attempts?: number; onRequestSent?: () => void; signal?: AbortSignal }
): Promise<WithoutId<WalletResponse<T>>> {
// TODO: remove deprecated method
const options: {
Expand Down Expand Up @@ -309,7 +314,7 @@ export class BridgeProvider implements HTTPProvider {
{
onRequestSent: onRequestSent,
signal: abortController.signal,
attempts: 1,
attempts: 1
}
);
} catch (e) {
Expand Down Expand Up @@ -522,7 +527,7 @@ export class BridgeProvider implements HTTPProvider {
source.bridgeUrl,
sessionCrypto.sessionId,
() => {},
() => {}
() => {}
);

gateway.setListener(message =>
Expand All @@ -547,7 +552,7 @@ export class BridgeProvider implements HTTPProvider {
},
{
attempts: Number.MAX_SAFE_INTEGER,
delayMs: 5_000,
delayMs: this.defaultRetryTimeoutMS,
signal: options?.signal
}
)
Expand Down

0 comments on commit 6ff4761

Please sign in to comment.