Skip to content

Commit

Permalink
fix redis#2205 - reject commands in connect phase when `disableOfflin…
Browse files Browse the repository at this point in the history
…eQueue` is `true`
  • Loading branch information
leibale committed Nov 16, 2022
1 parent d097322 commit e553270
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
18 changes: 17 additions & 1 deletion packages/client/lib/client/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import testUtils, { GLOBAL, waitTillBeenCalled } from '../test-utils';
import RedisClient, { RedisClientType } from '.';
import { RedisClientMultiCommandType } from './multi-command';
import { RedisCommandArguments, RedisCommandRawReply, RedisModules, RedisFunctions, RedisScripts } from '../commands';
import { AbortError, ClientClosedError, ConnectionTimeoutError, DisconnectsClientError, SocketClosedUnexpectedlyError, WatchError } from '../errors';
import { AbortError, ClientClosedError, ClientOfflineError, ConnectionTimeoutError, DisconnectsClientError, SocketClosedUnexpectedlyError, WatchError } from '../errors';
import { defineScript } from '../lua-script';
import { spy } from 'sinon';
import { once } from 'events';
Expand Down Expand Up @@ -874,4 +874,20 @@ describe('Client', () => {
pingInterval: 1
}
});

testUtils.testWithClient('should reject commands in connect phase when `disableOfflineQueue`', async client => {
const connectPromise = client.connect();
await assert.rejects(
client.ping(),
ClientOfflineError
);
await connectPromise;
await client.disconnect();
}, {
...GLOBAL.SERVERS.OPEN,
clientOptions: {
disableOfflineQueue: true
},
disableClientSetup: true
});
});
10 changes: 5 additions & 5 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ScanCommandOptions } from '../commands/SCAN';
import { HScanTuple } from '../commands/HSCAN';
import { attachCommands, attachExtensions, fCallArguments, transformCommandArguments, transformCommandReply, transformLegacyCommandArguments } from '../commander';
import { Pool, Options as PoolOptions, createPool } from 'generic-pool';
import { ClientClosedError, DisconnectsClientError } from '../errors';
import { ClientClosedError, ClientOfflineError, DisconnectsClientError } from '../errors';
import { URL } from 'url';
import { TcpSocketConnectOpts } from 'net';

Expand Down Expand Up @@ -405,16 +405,16 @@ export default class RedisClient<
): Promise<T> {
if (!this.#socket.isOpen) {
return Promise.reject(new ClientClosedError());
}

if (options?.isolated) {
} else if (options?.isolated) {
return this.executeIsolated(isolatedClient =>
isolatedClient.sendCommand(args, {
...options,
isolated: false
})
);
}
} else if (!this.#socket.isReady && this.#options?.disableOfflineQueue) {
return Promise.reject(new ClientOfflineError());
}

const promise = this.#queue.addCommand<T>(args, options);
this.#tick();
Expand Down
6 changes: 6 additions & 0 deletions packages/client/lib/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@ export class ClientClosedError extends Error {
}
}

export class ClientOfflineError extends Error {
constructor() {
super('The client is offline');
}
}

export class DisconnectsClientError extends Error {
constructor() {
super('Disconnects client');
Expand Down

0 comments on commit e553270

Please sign in to comment.