Skip to content
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

added clientId command in node #438

Merged
merged 2 commits into from
Oct 4, 2023
Merged
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
4 changes: 4 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ export function createIncrByFloat(
return createCommand(RequestType.IncrByFloat, [key, amount.toString()]);
}

export function createClientId(): redis_request.Command {
return createCommand(RequestType.ClientId, []);
}

export function createConfigGet(parameters: string[]): redis_request.Command {
return createCommand(RequestType.ConfigGet, parameters);
}
Expand Down
10 changes: 10 additions & 0 deletions node/src/RedisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BaseClient, ConnectionOptions, ReturnType } from "./BaseClient";
import {
InfoOptions,
createClientGetName,
createClientId,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
Expand Down Expand Up @@ -156,6 +157,15 @@ export class RedisClient extends BaseClient {
return this.createWritePromise(createConfigResetStat());
}

/** Returns the current connection id.
* See https://redis.io/commands/client-id/ for details.
*
* @returns the id of the client.
*/
public clientId(): Promise<number> {
return this.createWritePromise(createClientId());
}

/** Reads the configuration parameters of a running Redis server.
* See https://redis.io/commands/config-get/ for details.
*
Expand Down
30 changes: 27 additions & 3 deletions node/src/RedisClusterClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { BaseClient, ConnectionOptions, ReturnType } from "./BaseClient";
import {
InfoOptions,
createClientGetName,
createClientId,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
Expand Down Expand Up @@ -117,7 +118,8 @@ function toProtobufRoute(
}
}

/** Convert the multi-node response from a list of [address, nodeResponse] pairs to a dictionary of address: nodeResponse.
/** Convert the multi-node response from a list of [address, nodeResponse] pairs to
* a dictionary where each address is the key and its corresponding node response is the value.
*
* @param response - A list of lists, where each inner list contains an address (string)
* and the corresponding node response (of type T). Or a single node response (of type T).
Expand Down Expand Up @@ -216,7 +218,8 @@ export class RedisClusterClient extends BaseClient {
* When no parameter is provided, the default option is assumed.
* @param route - The command will be routed automatically, unless `route` is provided, in which
* case the client will initially try to route the command to the nodes defined by `route`.
* @returns a string containing the information for the sections requested.
* @returns a string containing the information for the sections requested. When specifying a route other than a single node,
* it returns a dictionary where each address is the key and its corresponding node response is the value.
*/
public info(
options?: InfoOptions[],
Expand Down Expand Up @@ -289,6 +292,27 @@ export class RedisClusterClient extends BaseClient {
);
}

/** Returns the current connection id.
Copy link
Contributor

@shachlanAmazon shachlanAmazon Sep 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should the documentation explain that since connections might disconnect and reconnect, and since the command might be routed differently between calls, the result isn't promised to remain constant?
@barshaul

* See https://redis.io/commands/client-id/ for details.
*
* @param route - The command will be routed automatically, unless `route` is provided, in which
* case the client will initially try to route the command to the nodes defined by `route`.
* @returns the id of the client. When specifying a route other than a single node,
* it returns a dictionary where each address is the key and its corresponding node response is the value.
*/
public clientId(route?: Routes): Promise<ClusterResponse<number>> {
const result = this.createWritePromise<number>(
createClientId(),
toProtobufRoute(route)
);
return result.then((res) => {
return convertMultiNodeResponseToDict<number>(
res,
(response) => typeof response == "number"
);
});
}

/** Reads the configuration parameters of a running Redis server.
* See https://redis.io/commands/config-get/ for details.
*
Expand All @@ -298,7 +322,7 @@ export class RedisClusterClient extends BaseClient {
* If `route` is not provided, the command will be sent to the all nodes.
*
* @returns A map of values corresponding to the configuration parameters. When specifying a route other than a single node,
* the response will be a dictionary of Address: nodeResponse.
* it returns a dictionary where each address is the key and its corresponding node response is the value.
*/
public configGet(
parameters: string[],
Expand Down
10 changes: 10 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
InfoOptions,
SetOptions,
createClientGetName,
createClientId,
createConfigGet,
createConfigResetStat,
createConfigRewrite,
Expand Down Expand Up @@ -196,6 +197,15 @@ export class BaseTransaction {
this.commands.push(createIncrByFloat(key, amount));
}

/** Returns the current connection id.
* See https://redis.io/commands/client-id/ for details.
*
* Command Response - the id of the client.
*/
public clientId() {
this.commands.push(createClientId());
}

/** Decrements the number stored at key by one. If the key does not exist, it is set to 0 before performing the operation.
* See https://redis.io/commands/decr/ for details.
*
Expand Down
23 changes: 18 additions & 5 deletions node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type BaseClient = {
mget: (keys: string[]) => Promise<(string | null)[]>;
incr: (key: string) => Promise<number>;
incrBy: (key: string, amount: number) => Promise<number>;
clientId: () => Promise<ClusterResponse<number>>;
decr: (key: string) => Promise<number>;
decrBy: (key: string, amount: number) => Promise<number>;
incrByFloat: (key: string, amount: number) => Promise<string>;
Expand Down Expand Up @@ -235,9 +236,9 @@ export function runBaseTests<Context>(config: {
async () => {
await runTest(async (client: BaseClient) => {
const serverInfo = await client.info([InfoOptions.Server]);
const conf_file = parseInfoResponse(getFirstResult(serverInfo))[
"config_file"
];
const conf_file = parseInfoResponse(
getFirstResult(serverInfo).toString()
)["config_file"];
if (conf_file.length > 0) {
expect(await client.configRewrite()).toEqual("OK");
} else {
Expand Down Expand Up @@ -265,15 +266,15 @@ export function runBaseTests<Context>(config: {
const OldResult = await client.info([InfoOptions.Stats]);
expect(
Number(
parseInfoResponse(getFirstResult(OldResult))[
parseInfoResponse(getFirstResult(OldResult).toString())[
"total_commands_processed"
]
)
).toBeGreaterThan(1);
expect(await client.configResetStat()).toEqual("OK");
const result = await client.info([InfoOptions.Stats]);
expect(
parseInfoResponse(getFirstResult(result))[
parseInfoResponse(getFirstResult(result).toString())[
"total_commands_processed"
]
).toEqual("1");
Expand Down Expand Up @@ -386,6 +387,18 @@ export function runBaseTests<Context>(config: {
config.timeout
);

it(
"clientId test",
async () => {
await runTest(async (client: BaseClient) => {
expect(getFirstResult(await client.clientId())).toBeGreaterThan(
0
);
});
},
config.timeout
);

it(
"decr and decrBy existing key",
async () => {
Expand Down
8 changes: 5 additions & 3 deletions node/tests/TestUtilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ export function flushallOnPort(port: number): Promise<void> {
}

/// This function takes the first result of the response if it got more than one response (like cluster responses).
export function getFirstResult(res: string | Record<string, string>): string {
if (typeof res == "string") {
export function getFirstResult(
res: string | number | Record<string, string> | Record<string, number>
): string | number {
if (typeof res == "string" || typeof res == "number") {
return res;
}
return Object.values(res).at(0) as string;
return Object.values(res).at(0);
}

export function transactionTest(
Expand Down