Skip to content

Commit ec7ccaf

Browse files
committed
fix #1758 - implement some CLIENT commands, add name to RedisClientOptions
1 parent 82920ae commit ec7ccaf

14 files changed

+323
-9
lines changed

docs/client-configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| socket.reconnectStrategy | `retries => Math.min(retries * 50, 500)` | A function containing the [Reconnect Strategy](#reconnect-strategy) logic |
1616
| username | | ACL username ([see ACL guide](https://redis.io/topics/acl)) |
1717
| password | | ACL password or the old "--requirepass" password |
18+
| name | | Connection name ([see `CLIENT SETNAME`](https://redis.io/commands/client-setname)) |
1819
| database | | Database number to connect to (see [`SELECT`](https://redis.io/commands/select) command) |
1920
| modules | | Object defining which [Redis Modules](../README.md#packages) to include |
2021
| scripts | | Object defining Lua Scripts to use with this client (see [Lua Scripts](../README.md#lua-scripts)) |

packages/client/lib/client/commands.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ import * as ASKING from '../commands/ASKING';
1515
import * as AUTH from '../commands/AUTH';
1616
import * as BGREWRITEAOF from '../commands/BGREWRITEAOF';
1717
import * as BGSAVE from '../commands/BGSAVE';
18+
import * as CLIENT_CACHING from '../commands/CLIENT_CACHING';
19+
import * as CLIENT_GETNAME from '../commands/CLIENT_GETNAME';
20+
import * as CLIENT_GETREDIR from '../commands/CLIENT_GETREDIR';
1821
import * as CLIENT_ID from '../commands/CLIENT_ID';
22+
import * as CLIENT_KILL from '../commands/CLIENT_KILL';
23+
import * as CLIENT_SETNAME from '../commands/CLIENT_SETNAME';
1924
import * as CLIENT_INFO from '../commands/CLIENT_INFO';
2025
import * as CLUSTER_ADDSLOTS from '../commands/CLUSTER_ADDSLOTS';
2126
import * as CLUSTER_FLUSHSLOTS from '../commands/CLUSTER_FLUSHSLOTS';
@@ -110,8 +115,18 @@ export default {
110115
bgRewriteAof: BGREWRITEAOF,
111116
BGSAVE,
112117
bgSave: BGSAVE,
118+
CLIENT_CACHING,
119+
clientCaching: CLIENT_CACHING,
120+
CLIENT_GETNAME,
121+
clientGetName: CLIENT_GETNAME,
122+
CLIENT_GETREDIR,
123+
clientGetRedir: CLIENT_GETREDIR,
113124
CLIENT_ID,
114125
clientId: CLIENT_ID,
126+
CLIENT_KILL,
127+
clientKill: CLIENT_KILL,
128+
CLIENT_SETNAME,
129+
clientSetName: CLIENT_SETNAME,
115130
CLIENT_INFO,
116131
clientInfo: CLIENT_INFO,
117132
CLUSTER_ADDSLOTS,

packages/client/lib/client/index.spec.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { AbortError, ClientClosedError, ConnectionTimeoutError, DisconnectsClien
77
import { defineScript } from '../lua-script';
88
import { spy } from 'sinon';
99
import { once } from 'events';
10+
import { ClientKillFilters } from '../commands/CLIENT_KILL';
1011

1112
export const SQUARE_SCRIPT = defineScript({
1213
NUMBER_OF_KEYS: 0,
@@ -125,6 +126,18 @@ describe('Client', () => {
125126
});
126127
});
127128

129+
testUtils.testWithClient('should set connection name', async client => {
130+
assert.equal(
131+
await client.clientGetName(),
132+
'name'
133+
);
134+
}, {
135+
...GLOBAL.SERVERS.OPEN,
136+
clientOptions: {
137+
name: 'name'
138+
}
139+
});
140+
128141
describe('legacyMode', () => {
129142
function sendCommandAsync<M extends RedisModules, S extends RedisScripts>(client: RedisClientType<M, S>, args: RedisCommandArguments): Promise<RedisCommandRawReply> {
130143
return new Promise((resolve, reject) => {
@@ -445,14 +458,9 @@ describe('Client', () => {
445458
});
446459

447460
testUtils.testWithClient('executeIsolated', async client => {
448-
await client.sendCommand(['CLIENT', 'SETNAME', 'client']);
449-
450-
assert.equal(
451-
await client.executeIsolated(isolatedClient =>
452-
isolatedClient.sendCommand(['CLIENT', 'GETNAME'])
453-
),
454-
null
455-
);
461+
const id = await client.clientId(),
462+
isolatedId = await client.executeIsolated(isolatedClient => isolatedClient.clientId());
463+
assert.ok(id !== isolatedId);
456464
}, GLOBAL.SERVERS.OPEN);
457465

458466
async function killClient<M extends RedisModules, S extends RedisScripts>(client: RedisClientType<M, S>): Promise<void> {
@@ -644,7 +652,10 @@ describe('Client', () => {
644652

645653
await Promise.all([
646654
once(subscriber, 'error'),
647-
publisher.sendCommand(['CLIENT', 'KILL', 'SKIPME', 'yes'])
655+
publisher.clientKill({
656+
filter: ClientKillFilters.SKIP_ME,
657+
skipMe: true
658+
})
648659
]);
649660

650661
await once(subscriber, 'ready');

packages/client/lib/client/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export interface RedisClientOptions<M extends RedisModules, S extends RedisScrip
2020
socket?: RedisSocketOptions;
2121
username?: string;
2222
password?: string;
23+
name?: string;
2324
database?: number;
2425
commandsQueueMaxLength?: number;
2526
readonly?: boolean;
@@ -201,6 +202,15 @@ export default class RedisClient<M extends RedisModules, S extends RedisScripts>
201202
);
202203
}
203204

205+
if (this.#options?.name) {
206+
promises.push(
207+
this.#queue.addCommand(
208+
COMMANDS.CLIENT_SETNAME.transformArguments(this.#options.name),
209+
{ asap: true }
210+
)
211+
);
212+
}
213+
204214
if (this.#options?.username || this.#options?.password) {
205215
promises.push(
206216
this.#queue.addCommand(
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { strict as assert } from 'assert';
2+
import { transformArguments } from './CLIENT_CACHING';
3+
4+
describe('CLIENT CACHING', () => {
5+
describe('transformArguments', () => {
6+
it('true', () => {
7+
assert.deepEqual(
8+
transformArguments(true),
9+
['CLIENT', 'CACHING', 'YES']
10+
);
11+
});
12+
13+
it('false', () => {
14+
assert.deepEqual(
15+
transformArguments(false),
16+
['CLIENT', 'CACHING', 'NO']
17+
);
18+
});
19+
});
20+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { RedisCommandArguments } from '.';
2+
3+
export function transformArguments(value: boolean): RedisCommandArguments {
4+
return [
5+
'CLIENT',
6+
'CACHING',
7+
value ? 'YES' : 'NO'
8+
];
9+
}
10+
11+
export declare function transformReply(): 'OK';
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { strict as assert } from 'assert';
2+
import { transformArguments } from './CLIENT_GETNAME';
3+
4+
describe('CLIENT GETNAME', () => {
5+
it('transformArguments', () => {
6+
assert.deepEqual(
7+
transformArguments(),
8+
['CLIENT', 'GETNAME']
9+
);
10+
});
11+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { RedisCommandArguments } from '.';
2+
3+
export function transformArguments(): RedisCommandArguments {
4+
return ['CLIENT', 'GETNAME'];
5+
}
6+
7+
export declare function transformReply(): string | null;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { strict as assert } from 'assert';
2+
import { transformArguments } from './CLIENT_GETREDIR';
3+
4+
describe('CLIENT GETREDIR', () => {
5+
it('transformArguments', () => {
6+
assert.deepEqual(
7+
transformArguments(),
8+
['CLIENT', 'GETREDIR']
9+
);
10+
});
11+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { RedisCommandArguments } from '.';
2+
3+
export function transformArguments(): RedisCommandArguments {
4+
return ['CLIENT', 'GETREDIR'];
5+
}
6+
7+
export declare function transformReply(): number;

0 commit comments

Comments
 (0)