Skip to content

Commit 064260d

Browse files
committed
improve the interface to better support unix socket.
delete client.host delete client.port add client.address add client.connectionOptions add tests.UNIX_SOCKET update all error message to use client.address update retry connection
1 parent d25d12e commit 064260d

File tree

2 files changed

+96
-30
lines changed

2 files changed

+96
-30
lines changed

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: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ tests.IPV4 = function () {
119119
var ipv4Client = redis.createClient( PORT, "127.0.0.1", { "family" : "IPv4" } );
120120

121121
ipv4Client.once("ready", function start_tests() {
122-
console.log("Connected to " + ipv4Client.host + ":" + ipv4Client.port + ", Redis server version " + ipv4Client.server_info.redis_version + "\n");
122+
console.log("Connected to " + ipv4Client.address + ", Redis server version " + ipv4Client.server_info.redis_version + "\n");
123123
console.log("Using reply parser " + ipv4Client.reply_parser.name);
124124

125125
ipv4Client.quit();
@@ -141,7 +141,7 @@ tests.IPV6 = function () {
141141
var ipv6Client = redis.createClient( PORT, "::1", { "family" : "IPv6" } );
142142

143143
ipv6Client.once("ready", function start_tests() {
144-
console.log("Connected to " + ipv6Client.host + ":" + ipv6Client.port + ", Redis server version " + ipv6Client.server_info.redis_version + "\n");
144+
console.log("Connected to " + ipv6Client.address + ", Redis server version " + ipv6Client.server_info.redis_version + "\n");
145145
console.log("Using reply parser " + ipv6Client.reply_parser.name);
146146

147147
ipv6Client.quit();
@@ -159,6 +159,31 @@ tests.IPV6 = function () {
159159
});
160160
}
161161

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

163188
tests.FLUSHDB = function () {
164189
var name = "FLUSHDB";
@@ -610,11 +635,16 @@ tests.CLIENT_LIST = function() {
610635
return next(name);
611636
}
612637

638+
var pattern = /^add=/;
639+
if ( server_version_at_least(client, [2, 8, 12])) {
640+
pattern = /^id=\d+ addr=/;
641+
}
642+
613643
function checkResult(result) {
614644
var lines = result.toString().split('\n').slice(0, -1);
615645
assert.strictEqual(lines.length, 4);
616646
assert(lines.every(function(line) {
617-
return line.match(/^addr=/);
647+
return line.match(pattern);
618648
}));
619649
}
620650

@@ -683,7 +713,7 @@ tests.WATCH_TRANSACTION = function () {
683713

684714

685715
tests.detect_buffers = function () {
686-
var name = "detect_buffers", detect_client = redis.createClient(null, null, {detect_buffers: true});
716+
var name = "detect_buffers", detect_client = redis.createClient({detect_buffers: true});
687717

688718
detect_client.on("ready", function () {
689719
// single Buffer or String
@@ -750,9 +780,9 @@ tests.detect_buffers = function () {
750780
tests.socket_nodelay = function () {
751781
var name = "socket_nodelay", c1, c2, c3, ready_count = 0, quit_count = 0;
752782

753-
c1 = redis.createClient(null, null, {socket_nodelay: true});
754-
c2 = redis.createClient(null, null, {socket_nodelay: false});
755-
c3 = redis.createClient(null, null);
783+
c1 = redis.createClient({socket_nodelay: true});
784+
c2 = redis.createClient({socket_nodelay: false});
785+
c3 = redis.createClient();
756786

757787
function quit_check() {
758788
quit_count++;
@@ -2194,7 +2224,7 @@ run_next_test = function run_next_test() {
21942224
};
21952225

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

22002230
run_next_test();

0 commit comments

Comments
 (0)