Skip to content

Commit

Permalink
fix: handle connecting immediately after "end" event
Browse files Browse the repository at this point in the history
Close #928
  • Loading branch information
luin committed Jul 14, 2019
1 parent 90b1aec commit 818a582
Show file tree
Hide file tree
Showing 4 changed files with 27 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
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 818a582

Please sign in to comment.