1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00

new "transform arguments" API for better key and metadata extraction (#2733)

* Parser support with all commands

* remove "dist" from all imports for consistency

* address most of my review comments

* small tweak to multi type mapping handling

* tweak multi commands / fix addScript cases

* nits

* addressed all in person review comments

* revert addCommand/addScript changes to multi-commands

addCommand needs to be there for sendCommand like ability within a multi.

If its there, it might as well be used by createCommand() et al, to avoid repeating code.

addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
This commit is contained in:
Shaya Potter
2024-10-31 18:16:59 +02:00
committed by GitHub
parent 5ace34b9c9
commit 4708736f3b
1016 changed files with 6347 additions and 6542 deletions

View File

@@ -9,6 +9,9 @@ import RedisClusterMultiCommand, { RedisClusterMultiCommandType } from './multi-
import { PubSubListener } from '../client/pub-sub';
import { ErrorReply } from '../errors';
import { RedisTcpSocketOptions } from '../client/socket';
import ASKING from '../commands/ASKING';
import { BasicCommandParser } from '../client/parser';
import { parseArgs } from '../commands/generic-transformers';
interface ClusterCommander<
M extends RedisModules,
@@ -69,7 +72,7 @@ export interface RedisClusterOptions<
type ClusterCommand<
NAME extends PropertyKey,
COMMAND extends Command
> = COMMAND['FIRST_KEY_INDEX'] extends undefined ? (
> = COMMAND['NOT_KEYED_COMMAND'] extends true ? (
COMMAND['IS_FORWARD_COMMAND'] extends true ? NAME : never
) : NAME;
@@ -143,131 +146,70 @@ export default class RedisCluster<
TYPE_MAPPING extends TypeMapping,
// POLICIES extends CommandPolicies
> extends EventEmitter {
static extractFirstKey<C extends Command>(
command: C,
args: Parameters<C['transformArguments']>,
redisArgs: Array<RedisArgument>
) {
let key: RedisArgument | undefined;
switch (typeof command.FIRST_KEY_INDEX) {
case 'number':
key = redisArgs[command.FIRST_KEY_INDEX];
break;
case 'function':
key = command.FIRST_KEY_INDEX(...args);
break;
}
return key;
}
static #createCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return async function (this: ProxyCluster, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args);
const typeMapping = this._commandOptions?.typeMapping;
const parser = new BasicCommandParser();
command.parseCommand(parser, ...args);
const firstKey = RedisCluster.extractFirstKey(
command,
args,
redisArgs
);
const reply = await this.sendCommand(
firstKey,
return this._self.#execute(
parser.firstKey,
command.IS_READ_ONLY,
redisArgs,
this._commandOptions,
// command.POLICIES
(client, opts) => client._executeCommand(command, parser, opts, transformReply)
);
return transformReply ?
transformReply(reply, redisArgs.preserve, typeMapping) :
reply;
};
}
static #createModuleCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args);
const typeMapping = this._self._commandOptions?.typeMapping;
const parser = new BasicCommandParser();
command.parseCommand(parser, ...args);
const firstKey = RedisCluster.extractFirstKey(
command,
args,
redisArgs
);
const reply = await this._self.sendCommand(
firstKey,
return this._self.#execute(
parser.firstKey,
command.IS_READ_ONLY,
redisArgs,
this._self._commandOptions,
// command.POLICIES
(client, opts) => client._executeCommand(command, parser, opts, transformReply)
);
return transformReply ?
transformReply(reply, redisArgs.preserve, typeMapping) :
reply;
};
}
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
const prefix = functionArgumentsPrefix(name, fn),
transformReply = getTransformReply(fn, resp);
const prefix = functionArgumentsPrefix(name, fn);
const transformReply = getTransformReply(fn, resp);
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
const fnArgs = fn.transformArguments(...args);
const redisArgs = prefix.concat(fnArgs);
const typeMapping = this._self._commandOptions?.typeMapping;
const parser = new BasicCommandParser();
parser.push(...prefix);
fn.parseCommand(parser, ...args);
const firstKey = RedisCluster.extractFirstKey(
fn,
args,
fnArgs
);
const reply = await this._self.sendCommand(
firstKey,
return this._self.#execute(
parser.firstKey,
fn.IS_READ_ONLY,
redisArgs,
this._self._commandOptions,
// fn.POLICIES
(client, opts) => client._executeCommand(fn, parser, opts, transformReply)
);
return transformReply ?
transformReply(reply, fnArgs.preserve, typeMapping) :
reply;
};
}
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
const prefix = scriptArgumentsPrefix(script),
transformReply = getTransformReply(script, resp);
const prefix = scriptArgumentsPrefix(script);
const transformReply = getTransformReply(script, resp);
return async function (this: ProxyCluster, ...args: Array<unknown>) {
const scriptArgs = script.transformArguments(...args);
const redisArgs = prefix.concat(scriptArgs);
const typeMapping = this._commandOptions?.typeMapping;
const parser = new BasicCommandParser();
parser.push(...prefix);
script.parseCommand(parser, ...args);
const firstKey = RedisCluster.extractFirstKey(
script,
args,
scriptArgs
);
const reply = await this.executeScript(
script,
firstKey,
return this._self.#execute(
parser.firstKey,
script.IS_READ_ONLY,
redisArgs,
this._commandOptions,
// script.POLICIES
(client, opts) => client._executeScript(script, parser, opts, transformReply)
);
return transformReply ?
transformReply(reply, scriptArgs.preserve, typeMapping) :
reply;
};
}
@@ -443,15 +385,20 @@ export default class RedisCluster<
async #execute<T>(
firstKey: RedisArgument | undefined,
isReadonly: boolean | undefined,
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>) => Promise<T>
options: ClusterCommandOptions | undefined,
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, opts?: ClusterCommandOptions) => Promise<T>
): Promise<T> {
const maxCommandRedirections = this.#options.maxCommandRedirections ?? 16;
let client = await this.#slots.getClient(firstKey, isReadonly),
i = 0;
let client = await this.#slots.getClient(firstKey, isReadonly);
let i = 0;
let myOpts = options;
while (true) {
try {
return await fn(client);
return await fn(client, myOpts);
} catch (err) {
// reset to passed in options, if changed by an ask request
myOpts = options;
// TODO: error class
if (++i > maxCommandRedirections || !(err instanceof Error)) {
throw err;
@@ -469,8 +416,14 @@ export default class RedisCluster<
throw new Error(`Cannot find node ${address}`);
}
await redirectTo.asking();
client = redirectTo;
const chainId = Symbol('Asking Chain');
myOpts = options ? {...options} : {};
myOpts.chainId = chainId;
client.sendCommand(parseArgs(ASKING), {chainId: chainId}).catch(err => { console.log(`Asking Failed: ${err}`) } );
continue;
}
@@ -495,21 +448,8 @@ export default class RedisCluster<
return this._self.#execute(
firstKey,
isReadonly,
client => client.sendCommand(args, options)
);
}
executeScript(
script: RedisScript,
firstKey: RedisArgument | undefined,
isReadonly: boolean | undefined,
args: Array<RedisArgument>,
options?: CommandOptions
) {
return this._self.#execute(
firstKey,
isReadonly,
client => client.executeScript(script, args, options)
options,
(client, opts) => client.sendCommand(args, opts)
);
}

View File

@@ -2,7 +2,8 @@ import COMMANDS from '../commands';
import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType, RedisMultiQueuedCommand } from '../multi-command';
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, TransformReply, RedisScript, RedisFunction, TypeMapping, RedisArgument } from '../RESP/types';
import { attachConfig, functionArgumentsPrefix, getTransformReply } from '../commander';
import RedisCluster from '.';
import { BasicCommandParser } from '../client/parser';
import { Tail } from '../commands/generic-transformers';
type CommandSignature<
REPLIES extends Array<unknown>,
@@ -12,7 +13,7 @@ type CommandSignature<
S extends RedisScripts,
RESP extends RespVersions,
TYPE_MAPPING extends TypeMapping
> = (...args: Parameters<C['transformArguments']>) => RedisClusterMultiCommandType<
> = (...args: Tail<Parameters<C['parseCommand']>>) => RedisClusterMultiCommandType<
[...REPLIES, ReplyWithTypeMapping<CommandReply<C, RESP>, TYPE_MAPPING>],
M,
F,
@@ -93,13 +94,15 @@ export type ClusterMultiExecute = (
export default class RedisClusterMultiCommand<REPLIES = []> {
static #createCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args);
const firstKey = RedisCluster.extractFirstKey(
command,
args,
redisArgs
);
const parser = new BasicCommandParser();
command.parseCommand(parser, ...args);
const redisArgs: CommandArguments = parser.redisArgs;
redisArgs.preserve = parser.preserve;
const firstKey = parser.firstKey;
return this.addCommand(
firstKey,
command.IS_READ_ONLY,
@@ -111,13 +114,15 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
static #createModuleCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
firstKey = RedisCluster.extractFirstKey(
command,
args,
redisArgs
);
const parser = new BasicCommandParser();
command.parseCommand(parser, ...args);
const redisArgs: CommandArguments = parser.redisArgs;
redisArgs.preserve = parser.preserve;
const firstKey = parser.firstKey;
return this._self.addCommand(
firstKey,
command.IS_READ_ONLY,
@@ -128,17 +133,18 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
}
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
const prefix = functionArgumentsPrefix(name, fn),
transformReply = getTransformReply(fn, resp);
const prefix = functionArgumentsPrefix(name, fn);
const transformReply = getTransformReply(fn, resp);
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
const fnArgs = fn.transformArguments(...args);
const redisArgs: CommandArguments = prefix.concat(fnArgs);
const firstKey = RedisCluster.extractFirstKey(
fn,
args,
fnArgs
);
redisArgs.preserve = fnArgs.preserve;
const parser = new BasicCommandParser();
parser.push(...prefix);
fn.parseCommand(parser, ...args);
const redisArgs: CommandArguments = parser.redisArgs;
redisArgs.preserve = parser.preserve;
const firstKey = parser.firstKey;
return this._self.addCommand(
firstKey,
fn.IS_READ_ONLY,
@@ -150,22 +156,22 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
const transformReply = getTransformReply(script, resp);
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
const scriptArgs = script.transformArguments(...args);
this.#setState(
RedisCluster.extractFirstKey(
script,
args,
scriptArgs
),
script.IS_READ_ONLY
);
this.#multi.addScript(
const parser = new BasicCommandParser();
script.parseCommand(parser, ...args);
const scriptArgs: CommandArguments = parser.redisArgs;
scriptArgs.preserve = parser.preserve;
const firstKey = parser.firstKey;
return this.#addScript(
firstKey,
script.IS_READ_ONLY,
script,
scriptArgs,
transformReply
);
return this;
};
}
@@ -186,12 +192,12 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
});
}
readonly #multi = new RedisMultiCommand();
readonly #multi: RedisMultiCommand
readonly #executeMulti: ClusterMultiExecute;
readonly #executePipeline: ClusterMultiExecute;
#firstKey: RedisArgument | undefined;
#isReadonly: boolean | undefined = true;
readonly #typeMapping?: TypeMapping;
constructor(
executeMulti: ClusterMultiExecute,
@@ -199,10 +205,10 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
routing: RedisArgument | undefined,
typeMapping?: TypeMapping
) {
this.#multi = new RedisMultiCommand(typeMapping);
this.#executeMulti = executeMulti;
this.#executePipeline = executePipeline;
this.#firstKey = routing;
this.#typeMapping = typeMapping;
}
#setState(
@@ -224,6 +230,19 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
return this;
}
#addScript(
firstKey: RedisArgument | undefined,
isReadonly: boolean | undefined,
script: RedisScript,
args: CommandArguments,
transformReply?: TransformReply
) {
this.#setState(firstKey, isReadonly);
this.#multi.addScript(script, args, transformReply);
return this;
}
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false) {
if (execAsPipeline) return this.execAsPipeline<T>();
@@ -232,8 +251,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
this.#firstKey,
this.#isReadonly,
this.#multi.queue
),
this.#typeMapping
)
) as MultiReplyType<T, REPLIES>;
}
@@ -251,8 +269,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
this.#firstKey,
this.#isReadonly,
this.#multi.queue
),
this.#typeMapping
)
) as MultiReplyType<T, REPLIES>;
}