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: ETIMEDOUT error with Bluebird when connecting. #925

Merged
merged 1 commit into from
Jul 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 1 addition & 3 deletions lib/connectors/SentinelConnector/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import SentinelIterator from "./SentinelIterator";
import { ISentinelAddress } from "./types";
import AbstractConnector, { ErrorEmitter } from "../AbstractConnector";
import { NetStream, CallbackFunction } from "../../types";
import * as PromiseContainer from "../../promiseContainer";
import Redis from "../../redis";

const debug = Debug("SentinelConnector");
Expand Down Expand Up @@ -87,10 +86,9 @@ export default class SentinelConnector extends AbstractConnector {
this.retryAttempts = 0;

let lastError;
const _Promise = PromiseContainer.get();

const connectToNext = () =>
new _Promise<NetStream>((resolve, reject) => {
new Promise<NetStream>((resolve, reject) => {
const endpoint = this.sentinelIterator.next();

if (endpoint.done) {
Expand Down
41 changes: 22 additions & 19 deletions lib/connectors/StandaloneConnector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createConnection, TcpNetConnectOpts, IpcNetConnectOpts } from "net";
import { connect as createTLSConnection, SecureContextOptions } from "tls";
import { CONNECTION_CLOSED_ERROR_MSG } from "../utils";
import AbstractConnector, { ErrorEmitter } from "./AbstractConnector";
import * as PromiseContainer from "../promiseContainer";
import { NetStream } from "../types";

export function isIIpcConnectionOptions(
Expand Down Expand Up @@ -52,27 +51,31 @@ export default class StandaloneConnector extends AbstractConnector {
Object.assign(connectionOptions, options.tls);
}

const _Promise = PromiseContainer.get();
return new _Promise<NetStream>((resolve, reject) => {
process.nextTick(() => {
if (!this.connecting) {
reject(new Error(CONNECTION_CLOSED_ERROR_MSG));
return;
}
// TODO:
// We use native Promise here since other Promise
// implementation may use different schedulers that
// cause issue when the stream is resolved in the
// next tick.
// 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;
}

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

resolve(this.stream);
});
resolve(this.stream);
});
}
}
25 changes: 20 additions & 5 deletions test/functional/connection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Socket } from "net";
import * as net from "net";
import Redis from "../../lib/redis";
import * as sinon from "sinon";
import { expect } from "chai";
import MockServer from "../helpers/mock_server";
import * as Bluebird from "bluebird";

describe("connection", function() {
it('should emit "connect" when connected', function(done) {
Expand Down Expand Up @@ -77,9 +78,9 @@ describe("connection", function() {
var set = false;

// TODO: use spy
// @ts-ignore
const stub = sinon
.stub(Socket.prototype, "setTimeout")
.stub(net.Socket.prototype, "setTimeout")
// @ts-ignore
.callsFake(timeout => {
if (timeout === connectTimeout) {
set = true;
Expand All @@ -103,9 +104,9 @@ describe("connection", function() {
let timedoutCalled = false;

// TODO: use spy
// @ts-ignore
sinon
.stub(Socket.prototype, "setTimeout")
.stub(net.Socket.prototype, "setTimeout")
// @ts-ignore
.callsFake((timeout, callback) => {
if (timeout === 0) {
if (!isReady) {
Expand Down Expand Up @@ -383,4 +384,18 @@ describe("connection", function() {
});
});
});

describe("sync connection", () => {
it("works with synchronous connection", done => {
// @ts-ignore
Redis.Promise = Bluebird;
const redis = new Redis("/data");
redis.on("error", () => {
// @ts-ignore
Redis.Promise = Promise;
redis.disconnect();
done();
});
});
});
});