Skip to content

Commit

Permalink
refactor: subscriber start as part of connect flow
Browse files Browse the repository at this point in the history
  • Loading branch information
ganchoradkov committed Dec 2, 2024
1 parent e48bc9f commit 5334c60
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 22 deletions.
7 changes: 2 additions & 5 deletions packages/core/src/controllers/relayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,10 +360,12 @@ export class Relayer extends IRelayer {
clearTimeout(this.reconnectTimeout);
this.reconnectTimeout = undefined;
});
await this.subscriber.start();
this.hasExperiencedNetworkDisruption = false;
resolve();
});
} catch (e) {
await this.subscriber.stop();
const error = e as Error;
this.logger.warn(error, error.message);
this.hasExperiencedNetworkDisruption = true;
Expand Down Expand Up @@ -525,11 +527,6 @@ export class Relayer extends IRelayer {

private onConnectHandler = () => {
this.logger.warn({}, "Relayer connected 🛜");
this.subscriber
.start()
.catch((error) =>
this.logger.error(error, "onConnectHandler -> start()" + (error as Error)?.message),
);
this.startPingTimeout();
this.events.emit(RELAYER_EVENTS.connect);
};
Expand Down
19 changes: 3 additions & 16 deletions packages/core/src/controllers/subscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ export class Subscriber extends ISubscriber {
private storagePrefix = CORE_STORAGE_PREFIX;
private subscribeTimeout = toMiliseconds(ONE_MINUTE);
private initialSubscribeTimeout = toMiliseconds(ONE_SECOND * 15);
private restartPromise: Promise<void> | undefined;
private clientId: string;
private batchSubscribeTopicsLimit = 500;

Expand Down Expand Up @@ -451,14 +450,8 @@ export class Subscriber extends ISubscriber {
}

private restart = async () => {
if (this.restartPromise) return;
this.restartPromise = new Promise<void>(async (resolve) => {
await this.restore();
await this.onRestart();
resolve();
});
await this.restartPromise;
this.restartPromise = undefined;
await this.restore();
await this.onRestart();
};

private async persist() {
Expand All @@ -472,13 +465,7 @@ export class Subscriber extends ISubscriber {
const numOfBatches = Math.ceil(this.cached.length / this.batchSubscribeTopicsLimit);
for (let i = 0; i < numOfBatches; i++) {
const batch = subs.splice(0, this.batchSubscribeTopicsLimit);
// await this.batchFetchMessages(batch);
await new Promise<void>((resolve) => {
setTimeout(async () => {
await this.batchSubscribe(batch);
resolve();
}, toMiliseconds(ONE_SECOND));
});
await this.batchSubscribe(batch);
}
}
this.events.emit(SUBSCRIBER_EVENTS.resubscribed);
Expand Down
4 changes: 3 additions & 1 deletion packages/sign-client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ export class SignClient extends ISignClient {
await this.auth.init();
await this.engine.init();
this.logger.info(`SignClient Initialization Success`);
this.engine.processRelayMessageCache();
setTimeout(() => {
this.engine.processRelayMessageCache();
}, 1_000);
} catch (error: any) {
this.logger.info(`SignClient Initialization Failure`);
this.logger.error(error.message);
Expand Down

0 comments on commit 5334c60

Please sign in to comment.