Skip to content

Commit d3352bf

Browse files
author
Ruben Bridgewater
committed
Auto detect ip family if a IP has been provided
1 parent 0903bba commit d3352bf

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function RedisClient(options) {
4747
} else {
4848
cnx_options.port = options.port || default_port;
4949
cnx_options.host = options.host || default_host;
50-
cnx_options.family = options.family === 'IPv6' ? 6 : 4;
50+
cnx_options.family = (!options.family && net.isIP(cnx_options.host)) || (options.family === 'IPv6' ? 6 : 4);
5151
this.address = cnx_options.host + ':' + cnx_options.port;
5252
}
5353
this.connection_option = cnx_options;

test/connection.spec.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,12 @@ describe("connection tests", function () {
9999
var options = {
100100
host: 'somewhere',
101101
port: 6379,
102+
family: ip,
102103
max_attempts: 1
103104
};
104105
client = redis.createClient(options);
105-
assert.strictEqual(Object.keys(options).length, 3);
106+
assert.strictEqual(client.connection_option.family, ip === 'IPv6' ? 6 : 4);
107+
assert.strictEqual(Object.keys(options).length, 4);
106108
var end = helper.callFuncAfter(done, 2);
107109

108110
client.on('error', function (err) {
@@ -133,13 +135,14 @@ describe("connection tests", function () {
133135
var connect_timeout = 1000; // in ms
134136
client = redis.createClient({
135137
parser: parser,
136-
host: '192.168.74.167',
138+
host: '192.168.74.167', // Should be auto detected as ipv4
137139
connect_timeout: connect_timeout
138140
});
139141
process.nextTick(function() {
140142
assert(client.stream._events.timeout);
141143
});
142144
assert.strictEqual(client.address, '192.168.74.167:6379');
145+
assert.strictEqual(client.connection_option.family, 4);
143146
var time = Date.now();
144147

145148
client.on("reconnecting", function (params) {
@@ -156,8 +159,10 @@ describe("connection tests", function () {
156159
it("use the system socket timeout if the connect_timeout has not been provided", function () {
157160
client = redis.createClient({
158161
parser: parser,
159-
host: '192.168.74.167'
162+
host: '2001:db8::ff00:42:8329' // auto detect ip v6
160163
});
164+
assert.strictEqual(client.address, '2001:db8::ff00:42:8329:6379');
165+
assert.strictEqual(client.connection_option.family, 6);
161166
process.nextTick(function() {
162167
assert.strictEqual(client.stream._events.timeout, undefined);
163168
});
@@ -235,6 +240,7 @@ describe("connection tests", function () {
235240

236241
it("connects with a port only", function (done) {
237242
client = redis.createClient(6379);
243+
assert.strictEqual(client.connection_option.family, 4);
238244
client.on("error", done);
239245

240246
client.once("ready", function () {

0 commit comments

Comments
 (0)