Skip to content

Commit

Permalink
fix: handle connecting immediately after "end" event (#929)
Browse files Browse the repository at this point in the history
Introduce a connection epoch to distinguish different connections.

Close #928
  • Loading branch information
luin authored Jul 15, 2019
1 parent 7defb6b commit 7bcd8a8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 15 deletions.
30 changes: 16 additions & 14 deletions lib/connectors/StandaloneConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,25 @@ export default class StandaloneConnector extends AbstractConnector {
// Should use the provided promise in the next major
// version and do not connect before resolved.
return new Promise<NetStream>((resolve, reject) => {
if (!this.connecting) {
reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
return;
}
process.nextTick(() => {
if (!this.connecting) {
reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
return;
}

try {
if (options.tls) {
this.stream = createTLSConnection(connectionOptions);
} else {
this.stream = createConnection(connectionOptions);
try {
if (options.tls) {
this.stream = createTLSConnection(connectionOptions);
} else {
this.stream = createConnection(connectionOptions);
}
} catch (err) {
reject(err);
return;
}
} catch (err) {
reject(err);
return;
}

resolve(this.stream);
resolve(this.stream);
});
});
}
}
7 changes: 7 additions & 0 deletions lib/redis/event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ export function connectHandler(self) {

// AUTH command should be processed before any other commands
let flushed = false;
const { connectionEpoch } = self;
if (self.condition.auth) {
self.auth(self.condition.auth, function(err) {
if (connectionEpoch !== self.connectionEpoch) {
return;
}
if (err) {
if (err.message.indexOf("no password is set") === -1) {
flushed = true;
Expand Down Expand Up @@ -50,6 +54,9 @@ export function connectHandler(self) {

if (self.options.enableReadyCheck) {
self._readyCheck(function(err, info) {
if (connectionEpoch !== self.connectionEpoch) {
return;
}
if (err) {
if (!flushed) {
self.recoverFromFatalError(
Expand Down
3 changes: 3 additions & 0 deletions lib/redis/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ function Redis() {
this.resetCommandQueue();
this.resetOfflineQueue();

this.connectionEpoch = 0;

if (this.options.Connector) {
this.connector = new this.options.Connector(this.options);
} else if (this.options.sentinels) {
Expand Down Expand Up @@ -259,6 +261,7 @@ Redis.prototype.connect = function(callback) {
reject(new Error("Redis is already connecting/connected"));
return;
}
this.connectionEpoch += 1;
this.setStatus("connecting");

const { options } = this;
Expand Down
23 changes: 23 additions & 0 deletions test/functional/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,29 @@ describe("connection", function() {
});
});

it("connects successfully immediately after end", done => {
const redis = new Redis();
redis.once("end", async () => {
await redis.connect();
done();
});

redis.quit();
});

it("connects successfully immediately after quit", done => {
const redis = new Redis();
redis.once("end", async () => {
await redis.connect();
done();
});

// process.nextTick ensures the connection is being made.
process.nextTick(() => {
redis.quit();
});
});

describe("connectTimeout", () => {
it("should close the connection when timeout", function(done) {
var redis = new Redis(6379, "192.0.0.0", {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/connectors/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe("StandaloneConnector", () => {
port: 6379,
tls: { ca: "on" }
});
connector.connect(() => {});
await connector.connect(() => {});
expect(spy.calledOnce).to.eql(true);
expect(spy.firstCall.args[0]).to.eql({ port: 6379, ca: "on" });
connector.disconnect();
Expand Down

0 comments on commit 7bcd8a8

Please sign in to comment.