1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

Auto detect ip family if a IP has been provided

This commit is contained in:
Ruben Bridgewater
2015-11-22 17:00:22 +01:00
parent 0903bba205
commit d3352bf550
2 changed files with 10 additions and 4 deletions

View File

@@ -47,7 +47,7 @@ function RedisClient(options) {
} else { } else {
cnx_options.port = options.port || default_port; cnx_options.port = options.port || default_port;
cnx_options.host = options.host || default_host; cnx_options.host = options.host || default_host;
cnx_options.family = options.family === 'IPv6' ? 6 : 4; cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4);
this.address = cnx_options.host + ':' + cnx_options.port; this.address = cnx_options.host + ':' + cnx_options.port;
} }
this.connection_option = cnx_options; this.connection_option = cnx_options;

View File

@@ -99,10 +99,12 @@ describe("connection tests", function () {
var options = { var options = {
host: 'somewhere', host: 'somewhere',
port: 6379, port: 6379,
family: ip,
max_attempts: 1 max_attempts: 1
}; };
client = redis.createClient(options); client = redis.createClient(options);
assert.strictEqual(Object.keys(options).length, 3); assert.strictEqual(client.connection_option.family, ip === 'IPv6' ? 6 : 4);
assert.strictEqual(Object.keys(options).length, 4);
var end = helper.callFuncAfter(done, 2); var end = helper.callFuncAfter(done, 2);
client.on('error', function (err) { client.on('error', function (err) {
@@ -133,13 +135,14 @@ describe("connection tests", function () {
var connect_timeout = 1000; // in ms var connect_timeout = 1000; // in ms
client = redis.createClient({ client = redis.createClient({
parser: parser, parser: parser,
host: '192.168.74.167', host: '192.168.74.167', // Should be auto detected as ipv4
connect_timeout: connect_timeout connect_timeout: connect_timeout
}); });
process.nextTick(function() { process.nextTick(function() {
assert(client.stream._events.timeout); assert(client.stream._events.timeout);
}); });
assert.strictEqual(client.address, '192.168.74.167:6379'); assert.strictEqual(client.address, '192.168.74.167:6379');
assert.strictEqual(client.connection_option.family, 4);
var time = Date.now(); var time = Date.now();
client.on("reconnecting", function (params) { client.on("reconnecting", function (params) {
@@ -156,8 +159,10 @@ describe("connection tests", function () {
it("use the system socket timeout if the connect_timeout has not been provided", function () { it("use the system socket timeout if the connect_timeout has not been provided", function () {
client = redis.createClient({ client = redis.createClient({
parser: parser, parser: parser,
host: '192.168.74.167' host: '2001:db8::ff00:42:8329' // auto detect ip v6
}); });
assert.strictEqual(client.address, '2001:db8::ff00:42:8329:6379');
assert.strictEqual(client.connection_option.family, 6);
process.nextTick(function() { process.nextTick(function() {
assert.strictEqual(client.stream._events.timeout, undefined); assert.strictEqual(client.stream._events.timeout, undefined);
}); });
@@ -235,6 +240,7 @@ describe("connection tests", function () {
it("connects with a port only", function (done) { it("connects with a port only", function (done) {
client = redis.createClient(6379); client = redis.createClient(6379);
assert.strictEqual(client.connection_option.family, 4);
client.on("error", done); client.on("error", done);
client.once("ready", function () { client.once("ready", function () {