Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 24 additions & 11 deletions sdk/src/accounts/grpcAccountSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
private commitmentLevel: CommitmentLevel;
public listenerId?: number;
private enableReconnect: boolean;

private constructor(
client: Client,
Expand All @@ -26,11 +27,13 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
program: Program,
accountPublicKey: PublicKey,
decodeBuffer?: (buffer: Buffer) => T,
resubOpts?: ResubOpts
resubOpts?: ResubOpts,
enableReconnect = false
) {
super(accountName, program, accountPublicKey, decodeBuffer, resubOpts);
this.client = client;
this.commitmentLevel = commitmentLevel;
this.enableReconnect = enableReconnect;
}

public static async create<U>(
Expand All @@ -57,7 +60,8 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
program,
accountPublicKey,
decodeBuffer,
resubOpts
resubOpts,
grpcConfigs.enableReconnect
);
}

Expand Down Expand Up @@ -92,15 +96,24 @@ export class grpcAccountSubscriber<T> extends WebSocketAccountSubscriber<T> {
transactionsStatus: {},
};

this.stream.on('error', (error) => {
// @ts-ignore
if (error.code === 1) {
// expected: 1 CANCELLED: Cancelled on client
return;
} else {
console.error('GRPC unexpected error caught:', error);
}
});
if (this.enableReconnect) {
this.stream.on('error', (error) => {
// @ts-ignore
if (error.code === 1) {
// expected: 1 CANCELLED: Cancelled on client
console.error(
'GRPC (grpcAccountSubscriber) Cancelled on client caught:',
error
);
return;
} else {
console.error(
'GRPC (grpcAccountSubscriber) unexpected error caught:',
error
);
}
});
}

this.stream.on('data', (chunk: SubscribeUpdate) => {
if (!chunk.account) {
Expand Down
37 changes: 26 additions & 11 deletions sdk/src/accounts/grpcProgramAccountSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export class grpcProgramAccountSubscriber<
private stream: ClientDuplexStream<SubscribeRequest, SubscribeUpdate>;
private commitmentLevel: CommitmentLevel;
public listenerId?: number;
private enableReconnect: boolean;

private constructor(
client: Client,
Expand All @@ -31,7 +32,8 @@ export class grpcProgramAccountSubscriber<
options: { filters: MemcmpFilter[] } = {
filters: [],
},
resubOpts?: ResubOpts
resubOpts?: ResubOpts,
enableReconnect = false
) {
super(
subscriptionName,
Expand All @@ -43,6 +45,7 @@ export class grpcProgramAccountSubscriber<
);
this.client = client;
this.commitmentLevel = commitmentLevel;
this.enableReconnect = enableReconnect;
}

public static async create<U>(
Expand Down Expand Up @@ -73,7 +76,8 @@ export class grpcProgramAccountSubscriber<
program,
decodeBufferFn,
options,
resubOpts
resubOpts,
grpcConfigs.enableReconnect
);
}

Expand Down Expand Up @@ -119,15 +123,26 @@ export class grpcProgramAccountSubscriber<
entry: {},
transactionsStatus: {},
};
this.stream.on('error', (error) => {
// @ts-ignore
if (error.code === 1) {
// expected: 1 CANCELLED: Cancelled on client
return;
} else {
console.error('GRPC unexpected error caught:', error);
}
});

if (this.enableReconnect) {
this.stream.on('error', (error) => {
// @ts-ignore
if (error.code === 1) {
// expected: 1 CANCELLED: Cancelled on client
console.error(
'GRPC (grpcProgramAccountSubscriber) Cancelled on client caught:',
error
);
return;
} else {
console.error(
'GRPC (grpcProgramAccountSubscriber) unexpected error caught:',
error
);
}
});
}

this.stream.on('data', (chunk: SubscribeUpdate) => {
if (!chunk.account) {
return;
Expand Down
5 changes: 5 additions & 0 deletions sdk/src/accounts/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ export type GrpcConfigs = {
token: string;
commitmentLevel?: CommitmentLevel;
channelOptions?: ChannelOptions;
/**
* Whether to enable automatic reconnection on connection loss .
* Defaults to false, will throw on connection loss.
*/
enableReconnect?: boolean;
};

export interface HighLeverageModeConfigAccountSubscriber {
Expand Down
Loading