Skip to content

Handle early server errors (issue #2417) #2549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
14 changes: 12 additions & 2 deletions packages/client/lib/client/commands-queue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default class RedisCommandsQueue {
readonly #waitingToBeSent = new LinkedList<CommandWaitingToBeSent>();
readonly #waitingForReply = new LinkedList<CommandWaitingForReply>();
readonly #onShardedChannelMoved: OnShardedChannelMoved;
readonly #earlyServerError: Function;

readonly #pubSub = new PubSub();

Expand Down Expand Up @@ -87,7 +88,14 @@ export default class RedisCommandsQueue {
}
}

const { resolve, reject } = this.#waitingForReply.shift()!;
let resolve, reject;
if (this.#waitingForReply.length > 0) {
({ resolve, reject } = this.#waitingForReply.shift()!);
} else {
resolve = (obj: any) => this.#earlyServerError(new Error(`unexpected response from server ${obj}`));
reject = (e: Error) => this.#earlyServerError(e);
}

if (reply instanceof ErrorReply) {
reject(reply);
} else {
Expand All @@ -98,10 +106,12 @@ export default class RedisCommandsQueue {

constructor(
maxLength: number | null | undefined,
onShardedChannelMoved: OnShardedChannelMoved
onShardedChannelMoved: OnShardedChannelMoved,
earlyServerError: Function
) {
this.#maxLength = maxLength;
this.#onShardedChannelMoved = onShardedChannelMoved;
this.#earlyServerError = earlyServerError;
}

addCommand<T = RedisCommandRawReply>(args: RedisCommandArguments, options?: QueueCommandOptions): Promise<T> {
Expand Down
3 changes: 2 additions & 1 deletion packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,8 @@ export default class RedisClient<
#initiateQueue(): RedisCommandsQueue {
return new RedisCommandsQueue(
this.#options?.commandsQueueMaxLength,
(channel, listeners) => this.emit('sharded-channel-moved', channel, listeners)
(channel, listeners) => this.emit('sharded-channel-moved', channel, listeners),
(err: Error) => this.emit('error', err)
);
}

Expand Down