Skip to content

Commit 829ab22

Browse files
committed
Merge branch 'jackhftang-master'
2 parents d25d12e + ba234ed commit 829ab22

File tree

3 files changed

+145
-88
lines changed

3 files changed

+145
-88
lines changed

README.md

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,15 @@ resume sending when you get `drain`.
175175

176176
`client` will emit `idle` when there are no outstanding commands that are awaiting a response.
177177

178-
## redis.createClient(port, host, options)
178+
## redis.createClient()
179179

180-
Create a new client connection. `port` defaults to `6379` and `host` defaults
181-
to `127.0.0.1`. If you have `redis-server` running on the same computer as node, then the defaults for
180+
### overloading
181+
* redis.createClient() = redis.createClient(6379, '127.0.0.1', {})
182+
* redis.createClient(options) = redis.createClient(6379, '127.0.0.1', options)
183+
* redis.createClient(unix_socket, options)
184+
* redis.createClient(port, host, options)
185+
186+
If you have `redis-server` running on the same computer as node, then the defaults for
182187
port and host are probably fine. `options` in an object with the following possible properties:
183188

184189
* `parser`: which Redis protocol reply parser to use. Defaults to `hiredis` if that module is installed.
@@ -216,7 +221,7 @@ You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns mod
216221

217222
```js
218223
var redis = require("redis"),
219-
client = redis.createClient(null, null, {detect_buffers: true});
224+
client = redis.createClient({detect_buffers: true});
220225

221226
client.set("foo_rand000000000000", "OK");
222227

@@ -234,24 +239,6 @@ You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns mod
234239

235240
`createClient()` returns a `RedisClient` object that is named `client` in all of the examples here.
236241

237-
### Unix Domain Socket
238-
239-
You can also create a connection to Redis server via the unix domain socket if the server
240-
has it enabled:
241-
242-
```js
243-
var redis = require("redis");
244-
var client = redis.createClient("/tmp/redis.sock");
245-
```
246-
247-
Sample `redis.conf` configuration to enable unix domain socket listening:
248-
249-
```conf
250-
unixsocket /tmp/redis.sock
251-
unixsocketperm 755
252-
```
253-
254-
See [issue #204](https://github.com/mranney/node_redis/issues/204) for more information.
255242

256243
## client.auth(password, callback)
257244

index.js

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ RedisClient.prototype.flush_and_error = function (message) {
178178
};
179179

180180
RedisClient.prototype.on_error = function (msg) {
181-
var message = "Redis connection to " + this.host + ":" + this.port + " failed - " + msg;
181+
var message = "Redis connection to " + this.address + " failed - " + msg;
182182

183183
if (this.closing) {
184184
return;
@@ -203,7 +203,7 @@ RedisClient.prototype.do_auth = function () {
203203
var self = this;
204204

205205
if (exports.debug_mode) {
206-
console.log("Sending auth to " + self.host + ":" + self.port + " id " + self.connection_id);
206+
console.log("Sending auth to " + self.address + " id " + self.connection_id);
207207
}
208208
self.send_anyway = true;
209209
self.send_command("auth", [this.auth_pass], function (err, res) {
@@ -227,7 +227,7 @@ RedisClient.prototype.do_auth = function () {
227227
return self.emit("error", new Error("Auth failed: " + res.toString()));
228228
}
229229
if (exports.debug_mode) {
230-
console.log("Auth succeeded " + self.host + ":" + self.port + " id " + self.connection_id);
230+
console.log("Auth succeeded " + self.address + " id " + self.connection_id);
231231
}
232232
if (self.auth_callback) {
233233
self.auth_callback(err, res);
@@ -249,7 +249,7 @@ RedisClient.prototype.do_auth = function () {
249249

250250
RedisClient.prototype.on_connect = function () {
251251
if (exports.debug_mode) {
252-
console.log("Stream connected " + this.host + ":" + this.port + " id " + this.connection_id);
252+
console.log("Stream connected " + this.address + " id " + this.connection_id);
253253
}
254254

255255
this.connected = true;
@@ -532,15 +532,15 @@ RedisClient.prototype.connection_gone = function (why) {
532532
return;
533533
}
534534

535-
self.stream = net.createConnection(self.port, self.host);
535+
self.stream = net.createConnection(self.connectionOption);
536536
self.install_stream_listeners();
537537
self.retry_timer = null;
538538
}, this.retry_delay);
539539
};
540540

541541
RedisClient.prototype.on_data = function (data) {
542542
if (exports.debug_mode) {
543-
console.log("net read " + this.host + ":" + this.port + " id " + this.connection_id + ": " + data.toString());
543+
console.log("net read " + this.address + " id " + this.connection_id + ": " + data.toString());
544544
}
545545

546546
try {
@@ -852,7 +852,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
852852
command_str += "$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n";
853853
}
854854
if (exports.debug_mode) {
855-
console.log("send " + this.host + ":" + this.port + " id " + this.connection_id + ": " + command_str);
855+
console.log("send " + this.address + " id " + this.connection_id + ": " + command_str);
856856
}
857857
buffered_writes += !stream.write(command_str);
858858
} else {
@@ -1213,28 +1213,64 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () {
12131213
};
12141214

12151215

1216-
exports.createClient = function (port_arg, host_arg, options) {
1216+
exports.createClient = function(arg0, arg1, arg2){
1217+
if( arguments.length === 0 ){
12171218

1218-
var cnxFamily;
1219-
1220-
if (options && options.family) {
1221-
cnxFamily = (options.family == 'IPv6' ? 6 : 4);
1222-
}
1223-
1219+
// createClient()
1220+
return createClient_tcp(default_port, default_host, {});
1221+
1222+
} else if( typeof arg0 === 'number' ||
1223+
typeof arg0 === 'string' && arg0.match(/^\d+$/) ){
1224+
1225+
// createClient( 3000, host, options)
1226+
// createClient('3000', host, options)
1227+
return createClient_tcp(arg0, arg1, arg2);
1228+
1229+
} else if( typeof arg0 === 'string' ){
1230+
1231+
// createClient( '/tmp/redis.sock', options)
1232+
return createClient_unix(arg0,arg1);
1233+
1234+
} else if( arg0 !== null && typeof arg0 === 'object' ){
1235+
1236+
// createClient(options)
1237+
return createClient_tcp(default_port, default_host, arg0 );
1238+
1239+
} else if( arg0 === null && arg1 === null ){
1240+
1241+
// for backward compatibility
1242+
// createClient(null,null,options)
1243+
return createClient_tcp(default_port, default_host, arg2);
1244+
1245+
} else {
1246+
throw new Error('unknown type of connection in createClient()');
1247+
}
1248+
}
1249+
1250+
var createClient_unix = function(path, options){
12241251
var cnxOptions = {
1225-
'port' : port_arg || default_port,
1226-
'host' : host_arg || default_host,
1227-
'family' : cnxFamily || '4'
1252+
path: path
12281253
};
1254+
var net_client = net.createConnection(cnxOptions);
1255+
var redis_client = new RedisClient(net_client, options || {});
12291256

1230-
var redis_client, net_client;
1257+
redis_client.connectionOption = cnxOptions;
1258+
redis_client.address = path;
12311259

1232-
net_client = net.createConnection(cnxOptions);
1260+
return redis_client;
1261+
}
12331262

1234-
redis_client = new RedisClient(net_client, options);
1263+
var createClient_tcp = function (port_arg, host_arg, options) {
1264+
var cnxOptions = {
1265+
'port' : port_arg || default_port,
1266+
'host' : host_arg || default_host,
1267+
'family' : (options && options.family === 'IPv6') ? 'IPv6' : 'IPv4'
1268+
};
1269+
var net_client = net.createConnection(cnxOptions);
1270+
var redis_client = new RedisClient(net_client, options || {});
12351271

1236-
redis_client.port = cnxOptions.port;
1237-
redis_client.host = cnxOptions.host;
1272+
redis_client.connectionOption = cnxOptions;
1273+
redis_client.address = cnxOptions.host + ':' + cnxOptions.port;
12381274

12391275
return redis_client;
12401276
};

test.js

Lines changed: 78 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -116,49 +116,78 @@ next = function next(name) {
116116
// Tests are run in the order they are defined, so FLUSHDB should always be first.
117117

118118
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-
});
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.address + ", 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+
});
138138
}
139139

140140
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-
});
141+
if (!server_version_at_least(client, [2, 8, 0])) {
142+
console.log("Skipping IPV6 for old Redis server version < 2.8.0");
143+
return run_next_test();
144+
}
145+
var ipv6Client = redis.createClient( PORT, "::1", { "family" : "IPv6" } );
146+
147+
ipv6Client.once("ready", function start_tests() {
148+
console.log("Connected to " + ipv6Client.address + ", Redis server version " + ipv6Client.server_info.redis_version + "\n");
149+
console.log("Using reply parser " + ipv6Client.reply_parser.name);
150+
151+
ipv6Client.quit();
152+
run_next_test();
153+
});
154+
155+
ipv6Client.on('end', function () {
156+
157+
});
158+
159+
// Exit immediately on connection failure, which triggers "exit", below, which fails the test
160+
ipv6Client.on("error", function (err) {
161+
console.error("client: " + err.stack);
162+
process.exit();
163+
});
160164
}
161165

166+
tests.UNIX_SOCKET = function () {
167+
var unixClient = redis.createClient('/tmp/redis.sock');
168+
169+
// if this fails, check the permission of unix socket.
170+
// unixsocket /tmp/redis.sock
171+
// unixsocketperm 777
172+
173+
unixClient.once('ready', function start_tests(){
174+
console.log("Connected to " + unixClient.address + ", Redis server version " + unixClient.server_info.redis_version + "\n");
175+
console.log("Using reply parser " + unixClient.reply_parser.name);
176+
177+
unixClient.quit();
178+
run_next_test();
179+
});
180+
181+
unixClient.on( 'end', function(){
182+
183+
});
184+
185+
// Exit immediately on connection failure, which triggers "exit", below, which fails the test
186+
unixClient.on("error", function (err) {
187+
console.error("client: " + err.stack);
188+
process.exit();
189+
});
190+
}
162191

163192
tests.FLUSHDB = function () {
164193
var name = "FLUSHDB";
@@ -610,11 +639,16 @@ tests.CLIENT_LIST = function() {
610639
return next(name);
611640
}
612641

642+
var pattern = /^addr=/;
643+
if ( server_version_at_least(client, [2, 8, 12])) {
644+
pattern = /^id=\d+ addr=/;
645+
}
646+
613647
function checkResult(result) {
614648
var lines = result.toString().split('\n').slice(0, -1);
615649
assert.strictEqual(lines.length, 4);
616650
assert(lines.every(function(line) {
617-
return line.match(/^addr=/);
651+
return line.match(pattern);
618652
}));
619653
}
620654

@@ -683,7 +717,7 @@ tests.WATCH_TRANSACTION = function () {
683717

684718

685719
tests.detect_buffers = function () {
686-
var name = "detect_buffers", detect_client = redis.createClient(null, null, {detect_buffers: true});
720+
var name = "detect_buffers", detect_client = redis.createClient({detect_buffers: true});
687721

688722
detect_client.on("ready", function () {
689723
// single Buffer or String
@@ -750,9 +784,9 @@ tests.detect_buffers = function () {
750784
tests.socket_nodelay = function () {
751785
var name = "socket_nodelay", c1, c2, c3, ready_count = 0, quit_count = 0;
752786

753-
c1 = redis.createClient(null, null, {socket_nodelay: true});
754-
c2 = redis.createClient(null, null, {socket_nodelay: false});
755-
c3 = redis.createClient(null, null);
787+
c1 = redis.createClient({socket_nodelay: true});
788+
c2 = redis.createClient({socket_nodelay: false});
789+
c3 = redis.createClient();
756790

757791
function quit_check() {
758792
quit_count++;
@@ -2194,7 +2228,7 @@ run_next_test = function run_next_test() {
21942228
};
21952229

21962230
client.once("ready", function start_tests() {
2197-
console.log("Connected to " + client.host + ":" + client.port + ", Redis server version " + client.server_info.redis_version + "\n");
2231+
console.log("Connected to " + client.address + ", Redis server version " + client.server_info.redis_version + "\n");
21982232
console.log("Using reply parser " + client.reply_parser.name);
21992233

22002234
run_next_test();

0 commit comments

Comments
 (0)