diff --git a/README.md b/README.md index 2d5d1238a0..d1ae32e26b 100644 --- a/README.md +++ b/README.md @@ -211,6 +211,8 @@ limits total time for client to reconnect. Value is provided in milliseconds and * `max_attempts` defaults to `null`. By default client will try reconnecting until connected. Setting `max_attempts` limits total amount of reconnects. * `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect. +* `family` defaults to `IPv4`. The client connects in IPv4 if not specified or if the DNS resolution returns an IPv4 address. +You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns modules how to use the family type. ```js var redis = require("redis"), diff --git a/index.js b/index.js index 67cbb5ff37..9674e7a528 100644 --- a/index.js +++ b/index.js @@ -1214,16 +1214,27 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () { exports.createClient = function (port_arg, host_arg, options) { - var port = port_arg || default_port, - host = host_arg || default_host, - redis_client, net_client; - net_client = net.createConnection(port, host); + var cnxFamily; + + if (options && options.family) { + cnxFamily = (options.family == 'IPv6' ? 6 : 4); + } + + var cnxOptions = { + 'port' : port_arg || default_port, + 'host' : host_arg || default_host, + 'family' : cnxFamily || '4' + }; + + var redis_client, net_client; + + net_client = net.createConnection(cnxOptions); redis_client = new RedisClient(net_client, options); - redis_client.port = port; - redis_client.host = host; + redis_client.port = cnxOptions.port; + redis_client.host = cnxOptions.host; return redis_client; }; diff --git a/test.js b/test.js index 44e97cde54..b9d915534b 100644 --- a/test.js +++ b/test.js @@ -115,6 +115,51 @@ next = function next(name) { // Tests are run in the order they are defined, so FLUSHDB should always be first. +tests.IPV4 = function () { + var ipv4Client = redis.createClient( PORT, "127.0.0.1", { "family" : "IPv4" } ); + + ipv4Client.once("ready", function start_tests() { + console.log("Connected to " + ipv4Client.host + ":" + ipv4Client.port + ", Redis server version " + ipv4Client.server_info.redis_version + "\n"); + console.log("Using reply parser " + ipv4Client.reply_parser.name); + + ipv4Client.quit(); + run_next_test(); + }); + + ipv4Client.on('end', function () { + + }); + + // Exit immediately on connection failure, which triggers "exit", below, which fails the test + ipv4Client.on("error", function (err) { + console.error("client: " + err.stack); + process.exit(); + }); +} + +tests.IPV6 = function () { + var ipv6Client = redis.createClient( PORT, "::1", { "family" : "IPv6" } ); + + ipv6Client.once("ready", function start_tests() { + console.log("Connected to " + ipv6Client.host + ":" + ipv6Client.port + ", Redis server version " + ipv6Client.server_info.redis_version + "\n"); + console.log("Using reply parser " + ipv6Client.reply_parser.name); + + ipv6Client.quit(); + run_next_test(); + }); + + ipv6Client.on('end', function () { + + }); + + // Exit immediately on connection failure, which triggers "exit", below, which fails the test + ipv6Client.on("error", function (err) { + console.error("client: " + err.stack); + process.exit(); + }); +} + + tests.FLUSHDB = function () { var name = "FLUSHDB"; client.select(test_db_num, require_string("OK", name));