Skip to content

Commit 060f4b4

Browse files
wphan0xbigz
authored andcommitted
sdk: restore grpc disconnect default behavior (#1782)
1 parent 707f50a commit 060f4b4

File tree

3 files changed

+55
-22
lines changed

3 files changed

+55
-22
lines changed

sdk/src/accounts/grpcAccountSubscriber.ts

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
1818
private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
1919
private commitmentLevel: CommitmentLevel;
2020
public listenerId?: number;
21+
private enableReconnect: boolean;
2122

2223
private constructor(
2324
client: Client,
@@ -26,11 +27,13 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
2627
program: Program,
2728
accountPublicKey: PublicKey,
2829
decodeBuffer?: (buffer: Buffer) => T,
29-
resubOpts?: ResubOpts
30+
resubOpts?: ResubOpts,
31+
enableReconnect = false
3032
) {
3133
super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
3234
this.client = client;
3335
this.commitmentLevel = commitmentLevel;
36+
this.enableReconnect = enableReconnect;
3437
}
3538

3639
public static async create<U>(
@@ -57,7 +60,8 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
5760
program,
5861
accountPublicKey,
5962
decodeBuffer,
60-
resubOpts
63+
resubOpts,
64+
grpcConfigs.enableReconnect
6165
);
6266
}
6367

@@ -92,15 +96,24 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
9296
transactionsStatus: {},
9397
};
9498

95-
this.stream.on('error', (error) => {
96-
// @ts-ignore
97-
if (error.code === 1) {
98-
// expected: 1 CANCELLED: Cancelled on client
99-
return;
100-
} else {
101-
console.error('GRPC unexpected error caught:', error);
102-
}
103-
});
99+
if (this.enableReconnect) {
100+
this.stream.on('error', (error) => {
101+
// @ts-ignore
102+
if (error.code === 1) {
103+
// expected: 1 CANCELLED: Cancelled on client
104+
console.error(
105+
'GRPC (grpcAccountSubscriber) Cancelled on client caught:',
106+
error
107+
);
108+
return;
109+
} else {
110+
console.error(
111+
'GRPC (grpcAccountSubscriber) unexpected error caught:',
112+
error
113+
);
114+
}
115+
});
116+
}
104117

105118
this.stream.on('data', (chunk: SubscribeUpdate) => {
106119
if (!chunk.account) {

sdk/src/accounts/grpcProgramAccountSubscriber.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export class grpcProgramAccountSubscriber<
2020
private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
2121
private commitmentLevel: CommitmentLevel;
2222
public listenerId?: number;
23+
private enableReconnect: boolean;
2324

2425
private constructor(
2526
client: Client,
@@ -31,7 +32,8 @@ export class grpcProgramAccountSubscriber<
3132
options: { filters: MemcmpFilter[] } = {
3233
filters: [],
3334
},
34-
resubOpts?: ResubOpts
35+
resubOpts?: ResubOpts,
36+
enableReconnect = false
3537
) {
3638
super(
3739
subscriptionName,
@@ -43,6 +45,7 @@ export class grpcProgramAccountSubscriber<
4345
);
4446
this.client = client;
4547
this.commitmentLevel = commitmentLevel;
48+
this.enableReconnect = enableReconnect;
4649
}
4750

4851
public static async create<U>(
@@ -73,7 +76,8 @@ export class grpcProgramAccountSubscriber<
7376
program,
7477
decodeBufferFn,
7578
options,
76-
resubOpts
79+
resubOpts,
80+
grpcConfigs.enableReconnect
7781
);
7882
}
7983

@@ -119,15 +123,26 @@ export class grpcProgramAccountSubscriber<
119123
entry: {},
120124
transactionsStatus: {},
121125
};
122-
this.stream.on('error', (error) => {
123-
// @ts-ignore
124-
if (error.code === 1) {
125-
// expected: 1 CANCELLED: Cancelled on client
126-
return;
127-
} else {
128-
console.error('GRPC unexpected error caught:', error);
129-
}
130-
});
126+
127+
if (this.enableReconnect) {
128+
this.stream.on('error', (error) => {
129+
// @ts-ignore
130+
if (error.code === 1) {
131+
// expected: 1 CANCELLED: Cancelled on client
132+
console.error(
133+
'GRPC (grpcProgramAccountSubscriber) Cancelled on client caught:',
134+
error
135+
);
136+
return;
137+
} else {
138+
console.error(
139+
'GRPC (grpcProgramAccountSubscriber) unexpected error caught:',
140+
error
141+
);
142+
}
143+
});
144+
}
145+
131146
this.stream.on('data', (chunk: SubscribeUpdate) => {
132147
if (!chunk.account) {
133148
return;

sdk/src/accounts/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ export type GrpcConfigs = {
226226
token: string;
227227
commitmentLevel?: CommitmentLevel;
228228
channelOptions?: ChannelOptions;
229+
/**
230+
* Whether to enable automatic reconnection on connection loss .
231+
* Defaults to false, will throw on connection loss.
232+
*/
233+
enableReconnect?: boolean;
229234
};
230235

231236
export interface HighLeverageModeConfigAccountSubscriber {

0 commit comments

Comments
 (0)