Skip to content

Commit 84b8926

Browse files
authored
fix(store-sync): block stream retry backoff (#3805)
1 parent cd0fa57 commit 84b8926

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

.changeset/funny-otters-explode.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@latticexyz/store-sync": patch
3+
---
4+
5+
Fixed an issue where losing connection would get stuck in a retry loop.

packages/store-sync/src/createStoreSync.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ import {
3333
mergeWith,
3434
ignoreElements,
3535
switchMap,
36-
Subject,
37-
startWith,
36+
ReplaySubject,
37+
timer,
38+
retry,
3839
} from "rxjs";
3940
import { debug as parentDebug } from "./debug";
4041
import { SyncStep } from "./SyncStep";
@@ -233,20 +234,27 @@ export async function createStoreSync({
233234
);
234235

235236
let latestBlockNumber: bigint | null = null;
236-
const recreateLatestBlockStream$ = new Subject<void>();
237-
const latestBlock$ = recreateLatestBlockStream$.pipe(
238-
startWith(undefined),
239-
switchMap(() =>
240-
createBlockStream({ ...opts, blockTag: followBlockTag }).pipe(
241-
catchError((error) => {
242-
debug("error in latestBlock$, recreating");
243-
recreateLatestBlockStream$.next();
244-
return throwError(() => error);
245-
}),
246-
),
247-
),
248-
shareReplay(1),
237+
238+
const latestBlock$ = defer(() => {
239+
debug("creating block stream");
240+
return createBlockStream({ ...opts, blockTag: followBlockTag });
241+
}).pipe(
242+
// TODO: detect network online and reset this
243+
retry({
244+
delay: (error, retryCount) => {
245+
const backoff = Math.min(4_000, 2 ** retryCount * 50);
246+
return timer(backoff);
247+
},
248+
resetOnSuccess: true,
249+
}),
250+
share({
251+
connector: () => new ReplaySubject(1),
252+
resetOnError: true,
253+
resetOnComplete: false,
254+
resetOnRefCountZero: true,
255+
}),
249256
);
257+
250258
const latestBlockNumber$ = latestBlock$.pipe(
251259
map((block) => block.number),
252260
tap((blockNumber) => {

0 commit comments

Comments
 (0)