Browse Source

Merge branch 'jackhftang-master'

gh-pages
Bryce Baril 10 years ago
parent
commit
829ab228f0
  1. 31
      README.md
  2. 80
      index.js
  3. 122
      test.js

31
README.md

@ -175,10 +175,15 @@ resume sending when you get `drain`.
`client` will emit `idle` when there are no outstanding commands that are awaiting a response.
## redis.createClient(port, host, options)
## redis.createClient()
Create a new client connection. `port` defaults to `6379` and `host` defaults
to `127.0.0.1`. If you have `redis-server` running on the same computer as node, then the defaults for
### overloading
* redis.createClient() = redis.createClient(6379, '127.0.0.1', {})
* redis.createClient(options) = redis.createClient(6379, '127.0.0.1', options)
* redis.createClient(unix_socket, options)
* redis.createClient(port, host, options)
If you have `redis-server` running on the same computer as node, then the defaults for
port and host are probably fine. `options` in an object with the following possible properties:
* `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
```js
var redis = require("redis"),
client = redis.createClient(null, null, {detect_buffers: true});
client = redis.createClient({detect_buffers: true});
client.set("foo_rand000000000000", "OK");
@ -234,24 +239,6 @@ You can force an IPv6 if you set the family to 'IPv6'. See nodejs net or dns mod
`createClient()` returns a `RedisClient` object that is named `client` in all of the examples here.
### Unix Domain Socket
You can also create a connection to Redis server via the unix domain socket if the server
has it enabled:
```js
var redis = require("redis");
var client = redis.createClient("/tmp/redis.sock");
```
Sample `redis.conf` configuration to enable unix domain socket listening:
```conf
unixsocket /tmp/redis.sock
unixsocketperm 755
```
See [issue #204](https://github.com/mranney/node_redis/issues/204) for more information.
## client.auth(password, callback)

80
index.js

@ -178,7 +178,7 @@ RedisClient.prototype.flush_and_error = function (message) {
};
RedisClient.prototype.on_error = function (msg) {
var message = "Redis connection to " + this.host + ":" + this.port + " failed - " + msg;
var message = "Redis connection to " + this.address + " failed - " + msg;
if (this.closing) {
return;
@ -203,7 +203,7 @@ RedisClient.prototype.do_auth = function () {
var self = this;
if (exports.debug_mode) {
console.log("Sending auth to " + self.host + ":" + self.port + " id " + self.connection_id);
console.log("Sending auth to " + self.address + " id " + self.connection_id);
}
self.send_anyway = true;
self.send_command("auth", [this.auth_pass], function (err, res) {
@ -227,7 +227,7 @@ RedisClient.prototype.do_auth = function () {
return self.emit("error", new Error("Auth failed: " + res.toString()));
}
if (exports.debug_mode) {
console.log("Auth succeeded " + self.host + ":" + self.port + " id " + self.connection_id);
console.log("Auth succeeded " + self.address + " id " + self.connection_id);
}
if (self.auth_callback) {
self.auth_callback(err, res);
@ -249,7 +249,7 @@ RedisClient.prototype.do_auth = function () {
RedisClient.prototype.on_connect = function () {
if (exports.debug_mode) {
console.log("Stream connected " + this.host + ":" + this.port + " id " + this.connection_id);
console.log("Stream connected " + this.address + " id " + this.connection_id);
}
this.connected = true;
@ -532,7 +532,7 @@ RedisClient.prototype.connection_gone = function (why) {
return;
}
self.stream = net.createConnection(self.port, self.host);
self.stream = net.createConnection(self.connectionOption);
self.install_stream_listeners();
self.retry_timer = null;
}, this.retry_delay);
@ -540,7 +540,7 @@ RedisClient.prototype.connection_gone = function (why) {
RedisClient.prototype.on_data = function (data) {
if (exports.debug_mode) {
console.log("net read " + this.host + ":" + this.port + " id " + this.connection_id + ": " + data.toString());
console.log("net read " + this.address + " id " + this.connection_id + ": " + data.toString());
}
try {
@ -852,7 +852,7 @@ RedisClient.prototype.send_command = function (command, args, callback) {
command_str += "$" + Buffer.byteLength(arg) + "\r\n" + arg + "\r\n";
}
if (exports.debug_mode) {
console.log("send " + this.host + ":" + this.port + " id " + this.connection_id + ": " + command_str);
console.log("send " + this.address + " id " + this.connection_id + ": " + command_str);
}
buffered_writes += !stream.write(command_str);
} else {
@ -1213,28 +1213,64 @@ RedisClient.prototype.eval = RedisClient.prototype.EVAL = function () {
};
exports.createClient = function (port_arg, host_arg, options) {
exports.createClient = function(arg0, arg1, arg2){
if( arguments.length === 0 ){
var cnxFamily;
if (options && options.family) {
cnxFamily = (options.family == 'IPv6' ? 6 : 4);
}
// createClient()
return createClient_tcp(default_port, default_host, {});
} else if( typeof arg0 === 'number' ||
typeof arg0 === 'string' && arg0.match(/^\d+$/) ){
// createClient( 3000, host, options)
// createClient('3000', host, options)
return createClient_tcp(arg0, arg1, arg2);
} else if( typeof arg0 === 'string' ){
// createClient( '/tmp/redis.sock', options)
return createClient_unix(arg0,arg1);
} else if( arg0 !== null && typeof arg0 === 'object' ){
// createClient(options)
return createClient_tcp(default_port, default_host, arg0 );
} else if( arg0 === null && arg1 === null ){
// for backward compatibility
// createClient(null,null,options)
return createClient_tcp(default_port, default_host, arg2);
} else {
throw new Error('unknown type of connection in createClient()');
}
}
var createClient_unix = function(path, options){
var cnxOptions = {
'port' : port_arg || default_port,
'host' : host_arg || default_host,
'family' : cnxFamily || '4'
path: path
};
var net_client = net.createConnection(cnxOptions);
var redis_client = new RedisClient(net_client, options || {});
var redis_client, net_client;
redis_client.connectionOption = cnxOptions;
redis_client.address = path;
net_client = net.createConnection(cnxOptions);
return redis_client;
}
redis_client = new RedisClient(net_client, options);
var createClient_tcp = function (port_arg, host_arg, options) {
var cnxOptions = {
'port' : port_arg || default_port,
'host' : host_arg || default_host,
'family' : (options && options.family === 'IPv6') ? 'IPv6' : 'IPv4'
};
var net_client = net.createConnection(cnxOptions);
var redis_client = new RedisClient(net_client, options || {});
redis_client.port = cnxOptions.port;
redis_client.host = cnxOptions.host;
redis_client.connectionOption = cnxOptions;
redis_client.address = cnxOptions.host + ':' + cnxOptions.port;
return redis_client;
};

122
test.js

@ -116,49 +116,78 @@ 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();
});
var ipv4Client = redis.createClient( PORT, "127.0.0.1", { "family" : "IPv4" } );
ipv4Client.once("ready", function start_tests() {
console.log("Connected to " + ipv4Client.address + ", 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();
});
if (!server_version_at_least(client, [2, 8, 0])) {
console.log("Skipping IPV6 for old Redis server version < 2.8.0");
return run_next_test();
}
var ipv6Client = redis.createClient( PORT, "::1", { "family" : "IPv6" } );
ipv6Client.once("ready", function start_tests() {
console.log("Connected to " + ipv6Client.address + ", 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.UNIX_SOCKET = function () {
var unixClient = redis.createClient('/tmp/redis.sock');
// if this fails, check the permission of unix socket.
// unixsocket /tmp/redis.sock
// unixsocketperm 777
unixClient.once('ready', function start_tests(){
console.log("Connected to " + unixClient.address + ", Redis server version " + unixClient.server_info.redis_version + "\n");
console.log("Using reply parser " + unixClient.reply_parser.name);
unixClient.quit();
run_next_test();
});
unixClient.on( 'end', function(){
});
// Exit immediately on connection failure, which triggers "exit", below, which fails the test
unixClient.on("error", function (err) {
console.error("client: " + err.stack);
process.exit();
});
}
tests.FLUSHDB = function () {
var name = "FLUSHDB";
@ -610,11 +639,16 @@ tests.CLIENT_LIST = function() {
return next(name);
}
var pattern = /^addr=/;
if ( server_version_at_least(client, [2, 8, 12])) {
pattern = /^id=\d+ addr=/;
}
function checkResult(result) {
var lines = result.toString().split('\n').slice(0, -1);
assert.strictEqual(lines.length, 4);
assert(lines.every(function(line) {
return line.match(/^addr=/);
return line.match(pattern);
}));
}
@ -683,7 +717,7 @@ tests.WATCH_TRANSACTION = function () {
tests.detect_buffers = function () {
var name = "detect_buffers", detect_client = redis.createClient(null, null, {detect_buffers: true});
var name = "detect_buffers", detect_client = redis.createClient({detect_buffers: true});
detect_client.on("ready", function () {
// single Buffer or String
@ -750,9 +784,9 @@ tests.detect_buffers = function () {
tests.socket_nodelay = function () {
var name = "socket_nodelay", c1, c2, c3, ready_count = 0, quit_count = 0;
c1 = redis.createClient(null, null, {socket_nodelay: true});
c2 = redis.createClient(null, null, {socket_nodelay: false});
c3 = redis.createClient(null, null);
c1 = redis.createClient({socket_nodelay: true});
c2 = redis.createClient({socket_nodelay: false});
c3 = redis.createClient();
function quit_check() {
quit_count++;
@ -2194,7 +2228,7 @@ run_next_test = function run_next_test() {
};
client.once("ready", function start_tests() {
console.log("Connected to " + client.host + ":" + client.port + ", Redis server version " + client.server_info.redis_version + "\n");
console.log("Connected to " + client.address + ", Redis server version " + client.server_info.redis_version + "\n");
console.log("Using reply parser " + client.reply_parser.name);
run_next_test();

Loading…
Cancel
Save