Skip to content

Commit

Permalink
added clientGetName and clientSetName commands in node
Browse files Browse the repository at this point in the history
  • Loading branch information
Adan committed Sep 3, 2023
1 parent 9392b39 commit 9271901
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
11 changes: 11 additions & 0 deletions node/src/Commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,17 @@ export function createSelect(
return createCommand(RequestType.Select, [index.toString()]);
}

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

export function createClientSetName(
connectionName: string
): redis_request.Command {
return createCommand(RequestType.ClientSetName, [connectionName]);
}

export function createConfigRewrite(
): redis_request.Command {
return createCommand(RequestType.ConfigRewrite, []);
Expand Down
21 changes: 21 additions & 0 deletions node/src/RedisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { Buffer, BufferWriter, Reader, Writer } from "protobufjs";
import {
InfoOptions,
SetOptions,
createClientGetName,
createClientSetName,
createConfigResetStat,
createConfigRewrite,
createCustomCommand,
Expand Down Expand Up @@ -320,6 +322,25 @@ export class RedisClient {
return this.createWritePromise(createSelect(index));
}

/** Get the name of the current connection.
* See https://redis.io/commands/client-getname/ for more details.
*
* @returns the name of the client connection as a string if a name is set,
* or null if no name is assigned.
*/
public clientGetName(): Promise<string | null> {
return this.createWritePromise(createClientGetName());
}

/** Set the current connection's name.
* See https://redis.io/commands/client-setname/ for more details.
*
* @returns "OK" if the connection name was successfully set.
*/
public clientSetName(connectionName: string): Promise<"OK"> {
return this.createWritePromise(createClientSetName(connectionName));
}

/** Rewrite the configuration file with the current configuration.
* See https://redis.io/commands/select/ for details.
*
Expand Down
25 changes: 24 additions & 1 deletion node/src/RedisClusterClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as net from "net";
import { InfoOptions, createConfigResetStat, createConfigRewrite, createCustomCommand, createInfo } from "./Commands";
import { InfoOptions, createClientGetName, createClientSetName, createConfigResetStat, createConfigRewrite, createCustomCommand, createInfo } from "./Commands";
import { connection_request, redis_request } from "./ProtobufMessage";
import { ConnectionOptions, RedisClient, ReturnType } from "./RedisClient";

Expand Down Expand Up @@ -138,6 +138,29 @@ export class RedisClusterClient extends RedisClient {
return this.createWritePromise(createInfo(options), toProtobufRoute(route));
}

/** Get the name of the current connection.
* See https://redis.io/commands/client-getname/ for more 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 name of the client connection as a string if a name is set,
* or null if no name is assigned.
*/
public clientGetName(route?: Routes): Promise<string | null> {
return this.createWritePromise(createClientGetName(), toProtobufRoute(route));
}

/** Set the current connection's name.
* See https://redis.io/commands/client-setname/ for more 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 "OK" if the connection name was successfully set.
*/
public clientSetName(connectionName: string, route?: Routes): Promise<"OK"> {
return this.createWritePromise(createClientSetName(connectionName), toProtobufRoute(route));
}

/** Rewrite the configuration file with the current configuration.
* See https://redis.io/commands/select/ for details.
*
Expand Down
21 changes: 21 additions & 0 deletions node/src/Transaction.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
InfoOptions,
SetOptions,
createClientGetName,
createClientSetName,
createConfigResetStat,
createConfigRewrite,
createCustomCommand,
Expand Down Expand Up @@ -61,6 +63,25 @@ export class BaseTransaction {
this.commands.push(createDel(keys));
}

/** Get the name of the current connection.
* See https://redis.io/commands/client-getname/ for more details.
*
* @CommandResponse Returns the name of the client connection as a string if a name is set,
* or null if no name is assigned.
*/
public clientGetName() {
this.commands.push(createClientGetName());
}

/** Set the current connection's name.
* See https://redis.io/commands/client-setname/ for more details.
*
* @CommandResponse "OK" if the connection name was successfully set.
*/
public clientSetName(connectionName: string) {
this.commands.push(createClientSetName(connectionName));
}

/** Rewrite the configuration file with the current configuration.
* See https://redis.io/commands/select/ for details.
*
Expand Down
26 changes: 25 additions & 1 deletion node/tests/SharedTests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expect, it } from "@jest/globals";
import { v4 as uuidv4 } from "uuid";
import { InfoOptions, ReturnType, SetOptions, parseInfoResponse } from "../";
import { InfoOptions, RedisClusterClient, ReturnType, SetOptions, parseInfoResponse } from "../";
import { Routes } from "../src/RedisClusterClient";
import { Client, GetAndSetRandomValue, getFirstResult } from "./TestUtilities";

type BaseClient = {
Expand All @@ -11,6 +12,8 @@ type BaseClient = {
) => Promise<string | "OK" | null>;
get: (key: string) => Promise<string | null>;
del: (keys: string[]) => Promise<number>;
clientGetName: () => Promise<string | null>;
clientSetName: (connectionName: string, route?: Routes) => Promise<"OK">;
configRewrite: () => Promise<"OK">;
info(options?: InfoOptions[]): Promise<string | string[][]>;
ConfigResetStat: () => Promise<"OK">;
Expand Down Expand Up @@ -165,6 +168,27 @@ export function runBaseTests<Context>(config: {
config.timeout
);

it(
"testing client get and client set",
async () => {
await runTest(async (client: BaseClient) => {
const connectionName = uuidv4();
expect(await client.clientGetName()).toBeNull();
if (client instanceof RedisClusterClient){
const okResponse = await client.clientSetName(connectionName, "allNodes");
for(let i=0; i < okResponse.length; i++){
expect(okResponse[i][1]).toEqual("OK");
}
}
else{
expect(await client.clientSetName(connectionName)).toEqual("OK");
}
expect(await client.clientGetName()).toEqual(connectionName);
});
},
config.timeout
);

it(
"test config rewrite",
async () => {
Expand Down

0 comments on commit 9271901

Please sign in to comment.