Skip to content

Commit 77bf3ca

Browse files
authored
sdk: add retry logic for polling account subscribers (#75)
1 parent 182d671 commit 77bf3ca

File tree

4 files changed

+54
-12
lines changed

4 files changed

+54
-12
lines changed

sdk/src/accounts/pollingClearingHouseAccountSubscriber.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,14 @@ export class PollingClearingHouseAccountSubscriber
8585

8686
await this.updateAccountsToPoll();
8787
await this.addToAccountLoader();
88-
await this.fetch();
89-
const subscriptionSucceeded = this.didSubscriptionSucceed();
88+
89+
let subscriptionSucceeded = false;
90+
let retries = 0;
91+
while (!subscriptionSucceeded && retries < 5) {
92+
await this.fetch();
93+
subscriptionSucceeded = this.didSubscriptionSucceed();
94+
retries++;
95+
}
9096

9197
if (subscriptionSucceeded) {
9298
this.eventEmitter.emit('update');
@@ -237,6 +243,10 @@ export class PollingClearingHouseAccountSubscriber
237243
// @ts-ignore
238244
this.eventEmitter.emit(accountToPoll.eventType, account);
239245
this.eventEmitter.emit('update');
246+
247+
if (!this.isSubscribed) {
248+
this.isSubscribed = this.didSubscriptionSucceed();
249+
}
240250
}
241251
);
242252
}

sdk/src/accounts/pollingOracleSubscriber.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,21 @@ export class PollingOracleSubscriber implements OracleSubscriber {
3737
}
3838

3939
this.addToAccountLoader();
40-
await this.fetch();
41-
this.eventEmitter.emit('update');
4240

43-
this.isSubscribed = true;
44-
return true;
41+
let subscriptionSucceeded = false;
42+
let retries = 0;
43+
while (!subscriptionSucceeded && retries < 5) {
44+
await this.fetch();
45+
subscriptionSucceeded = this.didSubscriptionSucceed();
46+
retries++;
47+
}
48+
49+
if (subscriptionSucceeded) {
50+
this.eventEmitter.emit('update');
51+
}
52+
53+
this.isSubscribed = subscriptionSucceeded;
54+
return subscriptionSucceeded;
4555
}
4656

4757
addToAccountLoader(): void {
@@ -100,4 +110,8 @@ export class PollingOracleSubscriber implements OracleSubscriber {
100110
this.assertIsSubscribed();
101111
return this.oraclePriceData;
102112
}
113+
114+
didSubscriptionSucceed(): boolean {
115+
return !!this.oraclePriceData;
116+
}
103117
}

sdk/src/accounts/pollingTokenAccountSubscriber.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,20 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
3636
}
3737

3838
this.addToAccountLoader();
39-
await this.fetch();
40-
this.eventEmitter.emit('update');
39+
let subscriptionSucceeded = false;
40+
let retries = 0;
41+
while (!subscriptionSucceeded && retries < 5) {
42+
await this.fetch();
43+
subscriptionSucceeded = this.didSubscriptionSucceed();
44+
retries++;
45+
}
46+
47+
if (subscriptionSucceeded) {
48+
this.eventEmitter.emit('update');
49+
}
4150

42-
this.isSubscribed = true;
43-
return true;
51+
this.isSubscribed = subscriptionSucceeded;
52+
return subscriptionSucceeded;
4453
}
4554

4655
addToAccountLoader(): void {
@@ -96,4 +105,8 @@ export class PollingTokenAccountSubscriber implements TokenAccountSubscriber {
96105
this.assertIsSubscribed();
97106
return this.tokenAccount;
98107
}
108+
109+
didSubscriptionSucceed(): boolean {
110+
return !!this.tokenAccount;
111+
}
99112
}

sdk/src/accounts/pollingUserAccountSubscriber.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
5252
}
5353

5454
await this.addToAccountLoader();
55-
await this.fetchIfUnloaded();
5655

57-
const subscriptionSucceeded = this.didSubscriptionSucceed();
56+
let subscriptionSucceeded = false;
57+
let retries = 0;
58+
while (!subscriptionSucceeded && retries < 5) {
59+
await this.fetchIfUnloaded();
60+
subscriptionSucceeded = this.didSubscriptionSucceed();
61+
retries++;
62+
}
5863

5964
if (subscriptionSucceeded) {
6065
this.eventEmitter.emit('update');

0 commit comments

Comments
 (0)