diff --git a/lib/cluster/ClusterSubscriber.ts b/lib/cluster/ClusterSubscriber.ts index cbc6e3f3..8421e769 100644 --- a/lib/cluster/ClusterSubscriber.ts +++ b/lib/cluster/ClusterSubscriber.ts @@ -1,13 +1,11 @@ import { EventEmitter } from "events"; import ConnectionPool from "./ConnectionPool"; -import { getNodeKey } from "./util"; +import { getConnectionName, getNodeKey } from "./util"; import { sample, noop, Debug } from "../utils"; import Redis from "../redis"; const debug = Debug("cluster:subscriber"); -const SUBSCRIBER_CONNECTION_NAME = "ioredisClusterSubscriber"; - export default class ClusterSubscriber { private started = false; private subscriber: any = null; @@ -81,7 +79,7 @@ export default class ClusterSubscriber { username: options.username, password: options.password, enableReadyCheck: true, - connectionName: SUBSCRIBER_CONNECTION_NAME, + connectionName: getConnectionName("subscriber", options.connectionName), lazyConnect: true, tls: options.tls, }); diff --git a/lib/cluster/index.ts b/lib/cluster/index.ts index 8683600d..914cb87f 100644 --- a/lib/cluster/index.ts +++ b/lib/cluster/index.ts @@ -11,6 +11,7 @@ import { nodeKeyToRedisOptions, groupSrvRecords, weightSrvRecords, + getConnectionName, } from "./util"; import ClusterSubscriber from "./ClusterSubscriber"; import DelayQueue from "./DelayQueue"; @@ -791,7 +792,10 @@ class Cluster extends EventEmitter { enableOfflineQueue: true, enableReadyCheck: false, retryStrategy: null, - connectionName: "ioredisClusterRefresher", + connectionName: getConnectionName( + "refresher", + this.options.redisOptions && this.options.redisOptions.connectionName + ), }); // Ignore error events since we will handle diff --git a/lib/cluster/util.ts b/lib/cluster/util.ts index 57ea715e..8a83f2df 100644 --- a/lib/cluster/util.ts +++ b/lib/cluster/util.ts @@ -119,3 +119,8 @@ export function weightSrvRecords(recordsGroup: ISrvRecordsGroup): SrvRecord { } } } + +export function getConnectionName(component, nodeConnectionName) { + const prefix = `ioredis-cluster(${component})`; + return nodeConnectionName ? `${prefix}:${nodeConnectionName}` : prefix; +} diff --git a/test/functional/cluster/ClusterSubscriber.ts b/test/functional/cluster/ClusterSubscriber.ts index 5ee0d359..4f72658e 100644 --- a/test/functional/cluster/ClusterSubscriber.ts +++ b/test/functional/cluster/ClusterSubscriber.ts @@ -33,4 +33,46 @@ describe("ClusterSubscriber", () => { subscriber.stop(); pool.reset([]); }); + + it("sets correct connection name when connectionName is set", async () => { + const pool = new ConnectionPool({ connectionName: "test" }); + const subscriber = new ClusterSubscriber(pool, new EventEmitter()); + + const clientNames = []; + new MockServer(30000, (argv) => { + if (argv[0] === "client" && argv[1] === "setname") { + clientNames.push(argv[2]); + } + }); + + pool.findOrCreate({ host: "127.0.0.1", port: 30000 }); + + subscriber.start(); + await subscriber.getInstance().subscribe("foo"); + subscriber.stop(); + pool.reset([]); + + expect(clientNames).to.eql(["ioredis-cluster(subscriber):test"]); + }); + + it("sets correct connection name when connectionName is absent", async () => { + const pool = new ConnectionPool({}); + const subscriber = new ClusterSubscriber(pool, new EventEmitter()); + + const clientNames = []; + new MockServer(30000, (argv) => { + if (argv[0] === "client" && argv[1] === "setname") { + clientNames.push(argv[2]); + } + }); + + pool.findOrCreate({ host: "127.0.0.1", port: 30000 }); + + subscriber.start(); + await subscriber.getInstance().subscribe("foo"); + subscriber.stop(); + pool.reset([]); + + expect(clientNames).to.eql(["ioredis-cluster(subscriber)"]); + }); }); diff --git a/test/functional/cluster/pub_sub.ts b/test/functional/cluster/pub_sub.ts index 08b561b8..22868db9 100644 --- a/test/functional/cluster/pub_sub.ts +++ b/test/functional/cluster/pub_sub.ts @@ -21,7 +21,7 @@ describe("cluster:pub/sub", function () { const sub = new Cluster(options); sub.subscribe("test cluster", function () { - node1.write(node1.findClientByName("ioredisClusterSubscriber"), [ + node1.write(node1.findClientByName("ioredis-cluster(subscriber)"), [ "message", "test channel", "hi", @@ -61,7 +61,7 @@ describe("cluster:pub/sub", function () { } if (argv[0] === "subscribe") { expect(c.password).to.eql("abc"); - expect(getConnectionName(c)).to.eql("ioredisClusterSubscriber"); + expect(getConnectionName(c)).to.eql("ioredis-cluster(subscriber)"); } if (argv[0] === "cluster" && argv[1] === "slots") { return [[0, 16383, ["127.0.0.1", 30001]]];