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

fix(cluster): check correct Upstash host #1195

Merged
merged 3 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"cron-parser": "^4.2.1",
"get-port": "^5.1.1",
"glob": "^7.2.0",
"ioredis": "^4.28.2",
"ioredis": "^4.28.5",
"lodash": "^4.17.21",
"msgpackr": "^1.4.6",
"semver": "^6.3.0",
Expand Down
26 changes: 21 additions & 5 deletions src/classes/redis-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,30 @@ export class RedisConnection extends EventEmitter {
if (this.blocking) {
this.opts.maxRetriesPerRequest = null;
}

this.checkUpstashHost(this.opts.host);
} else {
this._client = opts;

if (isRedisCluster(this._client)) {
const hosts = (<any>this._client).startupNodes.map(
(node: { host: string }) => node.host,
);
this.checkUpstashHost(this._client.options.redisOptions?.host || hosts);
} else {
let options = <IORedis.RedisOptions>this._client.options;
if ((<IORedis.ClusterOptions>options)?.redisOptions) {
options = (<IORedis.ClusterOptions>options).redisOptions;
}
this.checkUpstashHost(this.opts.host);
}
this.opts = isRedisCluster(this._client)
? this._client.options.redisOptions
? (<IORedis.Cluster>this._client).options.redisOptions
: this._client.options;

this.checkBlockingOptions(deprecationMessage, this.opts);
}

this.checkUpstashHost(this.opts.host);

this.handleClientError = (err: Error): void => {
this.emit('error', err);
};
Expand All @@ -83,8 +96,11 @@ export class RedisConnection extends EventEmitter {
}
}

private checkUpstashHost(host: string | undefined) {
if (host?.endsWith('upstash.io')) {
private checkUpstashHost(host: string[] | string | undefined) {
const includesUpstash = Array.isArray(host)
? host.some(node => node.endsWith('upstash.io'))
: host?.endsWith('upstash.io');
if (includesUpstash) {
throw new Error(upstashMessage);
}
}
Expand Down
35 changes: 35 additions & 0 deletions tests/test_connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,41 @@ describe('connection', () => {
'BullMQ: Upstash is not compatible with BullMQ.',
);
});

describe('when using Cluster instance', async () => {
it('throws an error', async () => {
const connection = new IORedis.Cluster([
{
host: 'https://upstash.io',
},
]);

expect(() => new QueueBase(queueName, { connection })).to.throw(
'BullMQ: Upstash is not compatible with BullMQ.',
);
});
});

describe('when using redisOptions', async () => {
it('throws an error', async () => {
const connection = new IORedis.Cluster(
[
{
host: 'localhost',
},
],
{
redisOptions: {
host: 'https://upstash.io',
},
},
);

expect(() => new QueueBase(queueName, { connection })).to.throw(
'BullMQ: Upstash is not compatible with BullMQ.',
);
});
});
});

it('should recover from a connection loss', async () => {
Expand Down
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3436,7 +3436,7 @@ into-stream@^6.0.0:
from2 "^2.3.0"
p-is-promise "^3.0.0"

ioredis@^4.28.2:
ioredis@^4.28.5:
version "4.28.5"
resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-4.28.5.tgz#5c149e6a8d76a7f8fa8a504ffc85b7d5b6797f9f"
integrity sha512-3GYo0GJtLqgNXj4YhrisLaNNvWSNwSS2wS4OELGfGxH8I69+XfNdnmV1AyN+ZqMh0i7eX+SWjrwFKDBDgfBC1A==
Expand Down