Skip to content

Commit

Permalink
feat: 🎸 bump dep and improve id retrieval
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Dec 13, 2023
1 parent dbb0d9e commit cc5be40
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"dependencies": {
"cluster-key-slot": "^1.1.2",
"json-joy": "^11.21.0",
"json-joy": "^11.21.1",
"thingies": "^1.15.0"
},
"devDependencies": {
Expand Down
11 changes: 5 additions & 6 deletions src/cluster/RedisCluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,16 @@ export class RedisCluster {
});
}

protected async createClient(config: RedisClusterNodeClientOpts): Promise<RedisClusterNodeClient> {
protected async createClient(config: RedisClusterNodeClientOpts, id?: string): Promise<RedisClusterNodeClient> {
const client = this.createClientRaw(config);
client.start();
const {user, pwd} = config;
const [, id] = await Promise.all([
const response = await Promise.all([
client.hello(3, pwd, user),
client.clusterMyId(),
id ? Promise.resolve() : client.clusterMyId(),
]);
client.id = id;
this.router.setClient(id, client);
client.id = id || response[1]!;
this.router.setClient(client);
return client;
}

Expand Down Expand Up @@ -160,7 +160,6 @@ export class RedisCluster {
call.redirects++;
if (call.redirects > call.maxRedirects) throw new Error('MAX_REDIRECTS');
const port = redirect[1];
console.log('redirect', host, port, call.redirects);
if (host === client.host && port === client.port) throw new Error('INVALID_REDIRECT');
const nextClient = await this.createClientForHost(host, port);
return this.callWithClient(call, nextClient);
Expand Down
5 changes: 3 additions & 2 deletions src/cluster/RedisClusterRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ export class RedisClusterRouter {
if (this.infos.has(id)) this.clients.set(id, client);
}

public setClient(id: string, client: RedisClusterNodeClient): void {
this.clients.set(id, client);
public setClient(client: RedisClusterNodeClient): void {
if (!client.id) throw new Error('NO_CLIENT_ID');
this.clients.set(client.id, client);
}

public getNodesForSlot(slot: number): RedisClusterNodeInfo[] {
Expand Down
9 changes: 6 additions & 3 deletions src/demo-cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ import {RedisCluster} from "./cluster/RedisCluster";

const main = async () => {
// const host = 'localhost';
const host = '127.0.0.1';
// const host = '127.0.0.1';
// const host = '172.17.0.2';
const port = 7000;
// const host = 'redis-15083.c28691.us-east-1-4.ec2.cloud.rlrcp.com';
const host = '54.166.70.167';
// const port = 7000;
const port = 15083;
const user = 'default';
const pwd = 'AoQhB7bNYljT8IiZ7nbgvSQSXiGHRwQX';
const pwd = '7UAmqOMRcZ0KFZUfzze2KaWW8w0Fe8pP';

const client = new RedisCluster({
seeds: [
Expand Down
11 changes: 9 additions & 2 deletions src/node/RedisClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,15 @@ export class RedisClient {
this.callFnf(callNoRes(args));
}

public clusterMyId(): Promise<string> {
return this.cmd(['CLUSTER', 'MYID'], {utf8Res: true}) as Promise<string>;
public async clusterMyId(): Promise<string> {
// `CLUSTER MYID` is not supported in a number of servers, for example,
// redis.com returns "ERR unknown subcommand 'myid'". Instead, we parse
// `CLUSTER NODES` output.
const reg = /^([^ ]+) .+myself/gm;
const nodes = await this.cmd(['CLUSTER', 'NODES']) as string;
const match = reg.exec(nodes);
if (!match) throw new Error('Failed to parse CLUSTER NODES output.');
return match[1];
}

public clusterShards(): Promise<RedisClusterShardsResponse> {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1860,10 +1860,10 @@ jsesc@^2.5.1:
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"
integrity sha512-OYu7XEzjkCQ3C5Ps3QIZsQfNpqoJyZZA99wd9aWd05NCtC5pWOkShK2mkL6HXQR6/Cy2lbNdPlZBpuQHXE63gA==

json-joy@^11.21.0:
version "11.21.0"
resolved "https://registry.yarnpkg.com/json-joy/-/json-joy-11.21.0.tgz#6fdb606d2d3dad097e72fe0a39740997759efe4b"
integrity sha512-JmrTkuxRPxCvzPhxPxOxrGg02jU7TPGNE8idWpQK7srsAAc776g9v8vyH6+HWN3ZPDoTwGPHLIc2V4/M5hSCAA==
json-joy@^11.21.1:
version "11.21.1"
resolved "https://registry.yarnpkg.com/json-joy/-/json-joy-11.21.1.tgz#ece2fc1b01166322e94f841e122fcb6075ca1eac"
integrity sha512-0eTiCiQlo59/PZ9OT8pSRJFkrL2pU0Ckr20W6syr1mJ3WUoNc/YUXqCgDRnfRg8NStHWM5d3AfcJLWttzfc8uA==
dependencies:
arg "^5.0.2"
hyperdyperid "^1.2.0"
Expand Down

0 comments on commit cc5be40

Please sign in to comment.