Skip to content

Commit

Permalink
fix(connections): reused connections
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Oct 20, 2019
1 parent c3a71f9 commit 1e808d2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
24 changes: 13 additions & 11 deletions src/classes/redis-connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ export class RedisConnection extends EventEmitter {
super();

if (!(opts instanceof IORedis)) {
this.opts = Object.assign(
{
port: 6379,
host: '127.0.0.1',
retryStrategy: function(times: number) {
return Math.min(Math.exp(times), 20000);
},
this.opts = {
port: 6379,
host: '127.0.0.1',
retryStrategy: function(times: number) {
return Math.min(Math.exp(times), 20000);
},
opts,
);
...opts,
};
} else {
this._client = opts;
}
Expand Down Expand Up @@ -72,7 +70,7 @@ export class RedisConnection extends EventEmitter {

await RedisConnection.waitUntilReady(this._client);

if (opts.skipVersionCheck !== true) {
if (opts && opts.skipVersionCheck !== true) {
const version = await this.getRedisVersion();
if (semver.lt(version, RedisConnection.minimumVersion)) {
throw new Error(
Expand Down Expand Up @@ -111,7 +109,11 @@ export class RedisConnection extends EventEmitter {
return client.connect();
}

async close() {}
async close() {
if (this.opts) {
// return this._client.quit();
}
}

private async getRedisVersion() {
const doc = await this._client.info();
Expand Down
8 changes: 6 additions & 2 deletions src/classes/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as Bluebird from 'bluebird';
import fs from 'fs';
import { Redis } from 'ioredis';
import IORedis from 'ioredis';
import path from 'path';
import { Processor, WorkerOptions } from '../interfaces';
import { QueueBase, Repeat } from './';
Expand Down Expand Up @@ -44,7 +44,11 @@ export class Worker extends QueueBase {
...this.opts,
};

this.blockingConnection = new RedisConnection(opts.connection);
this.blockingConnection = new RedisConnection(
opts instanceof IORedis
? (<IORedis.Redis>opts.connection).duplicate()
: opts.connection,
);
this.blockingConnection.on('error', this.emit.bind(this));

if (typeof processor === 'function') {
Expand Down

0 comments on commit 1e808d2

Please sign in to comment.