Skip to content

Commit 74757bf

Browse files
committed
fix: unhandled resub logic
1 parent 5aacc68 commit 74757bf

File tree

2 files changed

+54
-7
lines changed

2 files changed

+54
-7
lines changed

sdk/src/accounts/grpcDriftClientAccountSubscriberV2.ts

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,19 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
136136
'perpMarket',
137137
this.program,
138138
undefined,
139-
this.resubOpts
139+
this.resubOpts,
140+
undefined,
141+
async () => {
142+
try {
143+
if (this.resubOpts?.logResubMessages) {
144+
console.log('[grpcDriftClientAccountSubscriberV2] perp markets subscriber unsubscribed; resubscribing');
145+
}
146+
await this.subscribeToPerpMarketAccounts();
147+
} catch (e) {
148+
console.error('Perp markets resubscribe failed:', e);
149+
}
150+
}
151+
140152
);
141153
await this.perpMarketsSubscriber.subscribe(
142154
perpMarketPubkeys,
@@ -165,7 +177,18 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
165177
'spotMarket',
166178
this.program,
167179
undefined,
168-
this.resubOpts
180+
this.resubOpts,
181+
undefined,
182+
async () => {
183+
try {
184+
if (this.resubOpts?.logResubMessages) {
185+
console.log('[grpcDriftClientAccountSubscriberV2] spot markets subscriber unsubscribed; resubscribing');
186+
}
187+
await this.subscribeToSpotMarketAccounts();
188+
} catch (e) {
189+
console.error('Spot markets resubscribe failed:', e);
190+
}
191+
}
169192
);
170193
await this.spotMarketsSubscriber.subscribe(
171194
spotMarketPubkeys,
@@ -221,7 +244,18 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
221244
);
222245
return client.getOraclePriceDataFromBuffer(buffer);
223246
},
224-
this.resubOpts
247+
this.resubOpts,
248+
undefined,
249+
async () => {
250+
try {
251+
if (this.resubOpts?.logResubMessages) {
252+
console.log('[grpcDriftClientAccountSubscriberV2] oracle subscriber unsubscribed; resubscribing');
253+
}
254+
await this.subscribeToOracles();
255+
} catch (e) {
256+
console.error('Oracle resubscribe failed:', e);
257+
}
258+
}
225259
);
226260

227261
await this.oracleMultiSubscriber.subscribe(
@@ -233,6 +267,7 @@ export class grpcDriftClientAccountSubscriberV2 extends WebSocketDriftClientAcco
233267
}
234268
);
235269

270+
236271
return true;
237272
}
238273

sdk/src/accounts/grpcMultiAccountSubscriber.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export class grpcMultiAccountSubscriber<T> {
2929
private accountName: string;
3030
private decodeBufferFn?: (buffer: Buffer, pubkey?: string) => T;
3131
private resubOpts?: ResubOpts;
32+
private onUnsubscribe?: () => Promise<void>;
3233

3334
public listenerId?: number;
3435
public isUnsubscribing = false;
@@ -47,14 +48,16 @@ export class grpcMultiAccountSubscriber<T> {
4748
accountName: string,
4849
program: Program,
4950
decodeBuffer?: (buffer: Buffer, pubkey?: string) => T,
50-
resubOpts?: ResubOpts
51+
resubOpts?: ResubOpts,
52+
onUnsubscribe?: () => Promise<void>
5153
) {
5254
this.client = client;
5355
this.commitmentLevel = commitmentLevel;
5456
this.accountName = accountName;
5557
this.program = program;
5658
this.decodeBufferFn = decodeBuffer;
5759
this.resubOpts = resubOpts;
60+
this.onUnsubscribe = onUnsubscribe;
5861
}
5962

6063
public static async create<U>(
@@ -63,7 +66,8 @@ export class grpcMultiAccountSubscriber<T> {
6366
program: Program,
6467
decodeBuffer?: (buffer: Buffer, pubkey?: string) => U,
6568
resubOpts?: ResubOpts,
66-
clientProp?: Client
69+
clientProp?: Client,
70+
onUnsubscribe?: () => Promise<void>
6771
): Promise<grpcMultiAccountSubscriber<U>> {
6872
const client = clientProp
6973
? clientProp
@@ -82,7 +86,8 @@ export class grpcMultiAccountSubscriber<T> {
8286
accountName,
8387
program,
8488
decodeBuffer,
85-
resubOpts
89+
resubOpts,
90+
onUnsubscribe
8691
);
8792
}
8893

@@ -291,6 +296,14 @@ export class grpcMultiAccountSubscriber<T> {
291296
} else {
292297
this.isUnsubscribing = false;
293298
}
299+
300+
if(this.onUnsubscribe) {
301+
try {
302+
await this.onUnsubscribe();
303+
} catch (e) {
304+
console.error(e);
305+
}
306+
}
294307
}
295308

296309
private setTimeout(): void {
@@ -302,7 +315,6 @@ export class grpcMultiAccountSubscriber<T> {
302315
if (this.receivingData) {
303316
await this.unsubscribe();
304317
this.receivingData = false;
305-
// Caller should handle resubscribe; keep minimal to avoid duplication
306318
}
307319
},
308320
this.resubOpts?.resubTimeoutMs

0 commit comments

Comments
 (0)