Skip to content

Commit 2e3cb99

Browse files
committed
Merge branch 'master' of https://github.com/migounette/node_redis into migounette-master
2 parents 4148a9b + 8e0dcc0 commit 2e3cb99

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ limits total time for client to reconnect. Value is provided in milliseconds and
211211
* `max_attempts` defaults to `null`. By default client will try reconnecting until connected. Setting `max_attempts`
212212
limits total amount of reconnects.
213213
* `auth_pass` defaults to `null`. By default client will try connecting without auth. If set, client will run redis auth command on connect.
214+
* `family` defaults to `IPv4`. The client connects in IPv4 if not specified or if the DNS resolution returns an IPv4 address.
215+
You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns modules how to use the family type.
214216

215217
```js
216218
var redis = require("redis"),

index.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,16 +1214,27 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () {
12141214

12151215

12161216
exports.createClient = function (port_arg, host_arg, options) {
1217-
var port = port_arg || default_port,
1218-
host = host_arg || default_host,
1219-
redis_client, net_client;
12201217

1221-
net_client = net.createConnection(port, host);
1218+
var cnxFamily;
1219+
1220+
if (options && options.family) {
1221+
cnxFamily = (options.family == 'IPv6' ? 6 : 4);
1222+
}
1223+
1224+
var cnxOptions = {
1225+
'port' : port_arg || default_port,
1226+
'host' : host_arg || default_host,
1227+
'family' : cnxFamily || '4'
1228+
};
1229+
1230+
var redis_client, net_client;
1231+
1232+
net_client = net.createConnection(cnxOptions);
12221233

12231234
redis_client = new RedisClient(net_client, options);
12241235

1225-
redis_client.port = port;
1226-
redis_client.host = host;
1236+
redis_client.port = cnxOptions.port;
1237+
redis_client.host = cnxOptions.host;
12271238

12281239
return redis_client;
12291240
};

test.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,51 @@ next = function next(name) {
115115

116116
// Tests are run in the order they are defined, so FLUSHDB should always be first.
117117

118+
tests.IPV4 = function () {
119+
var ipv4Client = redis.createClient( PORT, "127.0.0.1", { "family" : "IPv4" } );
120+
121+
ipv4Client.once("ready", function start_tests() {
122+
console.log("Connected to " + ipv4Client.host + ":" + ipv4Client.port + ", Redis server version " + ipv4Client.server_info.redis_version + "\n");
123+
console.log("Using reply parser " + ipv4Client.reply_parser.name);
124+
125+
ipv4Client.quit();
126+
run_next_test();
127+
});
128+
129+
ipv4Client.on('end', function () {
130+
131+
});
132+
133+
// Exit immediately on connection failure, which triggers "exit", below, which fails the test
134+
ipv4Client.on("error", function (err) {
135+
console.error("client: " + err.stack);
136+
process.exit();
137+
});
138+
}
139+
140+
tests.IPV6 = function () {
141+
var ipv6Client = redis.createClient( PORT, "::1", { "family" : "IPv6" } );
142+
143+
ipv6Client.once("ready", function start_tests() {
144+
console.log("Connected to " + ipv6Client.host + ":" + ipv6Client.port + ", Redis server version " + ipv6Client.server_info.redis_version + "\n");
145+
console.log("Using reply parser " + ipv6Client.reply_parser.name);
146+
147+
ipv6Client.quit();
148+
run_next_test();
149+
});
150+
151+
ipv6Client.on('end', function () {
152+
153+
});
154+
155+
// Exit immediately on connection failure, which triggers "exit", below, which fails the test
156+
ipv6Client.on("error", function (err) {
157+
console.error("client: " + err.stack);
158+
process.exit();
159+
});
160+
}
161+
162+
118163
tests.FLUSHDB = function () {
119164
var name = "FLUSHDB";
120165
client.select(test_db_num, require_string("OK", name));

0 commit comments

Comments
 (0)