Permalink
Browse files

Made a lot of changes. Changed how you use the module.

  • Loading branch information...
1 parent a4814a1 commit 9d8233878cdcb6e4ba37e68ad439056b8642276e @joshuah committed Dec 20, 2012
Showing with 142 additions and 128 deletions.
  1. +67 −40 README.md
  2. +23 −30 example1.js
  3. +0 −37 example2.js
  4. +51 −20 index.js
  5. +1 −1 package.json
View
107 README.md
@@ -1,39 +1,58 @@
sol-redis-pool
==============
-A simple Redis pool for node using generic-pool. There are two example included.
+A simple Redis pool for node using generic-pool. There are two example included. See example1.js for a demo.
### Install
npm install sol-redis-pool
### Example
- // Configure our pool settings.
- settings = {
- redis_port: 6379,
+ // Example 1: Using the acquireHelper.
+ // Our Settings
+ options = {
redis_host: '127.0.0.1',
+ redis_port: 6379,
}
-
- var Pool = require('sol-redis-pool');
+
+ var RedisPool = require('./index');
+ var pool = new RedisPool(options);
// Handle the error here...
function errorCallback(err) {}
-
- function successCallback(client) {
+
+ function clientCallback(client) {
// Use the client then release it back to the pool.
client.ping(function(err, result) {
- console.log(err, result);
- Pool.release(client);
+ console.log(err, result);
+ // Release the client...
+ pool.release(client);
+ // Drain the pool so the example will end.
+ pool.drain(function(){
+ console.log('Done...');
+ });
})
}
- Pool.AcquireHelper(errorCallback, successCallback);
+ console.log("If everything is working, you should see 'null 'PONG'.")
+ pool.acquireHelper(errorCallback, clientCallback);
+
+The output should be:
+
+ If everything is working you should see 'null 'PONG'.
+ null 'PONG'
## Methods
-### AcquireHelper(errorCallback, successCallback)
+### acquire(callback)
+Acquires a redis client from the pool. The callback is passed an **err** and **client**. The **client** object is a normal redis client. Make sure you release the **client** using the .release(client) method when you are done.
+
+### release(client)
+This method will release your client object back into the pool.
+
+### acquireHelper(errorCallback, clientCallback)
This method accepts two callbacks. The error callback is called if the pool
-cannot return a client. `errorCallback(err)`. The successCallback(client) returns the redis client connection.
+cannot return a client. `errorCallback(err)`. The clientCallback(client) returns the redis client connection. Note: You still need to **release** the client object.
### Drain(Pool, callback)
This method will drain the connection pool completely and execute the callback when finished. You should call this when you want to close your application. If you do not your application will continue to run forever.
@@ -43,55 +62,63 @@ The Pool argument should be your *Pool* object.
## Adjusting Pool Settings.
Combine any the settings you may need into one global named settings.
- // Create a global named settings.
- settings = {
- redis_max: 10,
- redis_min: 5
+ options = {
+ redis_host: '127.0.0.1',
+ redis_port: 6379,
+ redis_options: {},
+ redis_password: 'dingbats'
+ max_clients: 10,
+ min_clients: 2,
+ reapIntervalMillis: 5000,
+ idleTimeoutMillis: 30000,
+ logging: true
}
-
+
+## Redis Options
+
### Redis Server and Port
- settings = {
+ options = {
redis_port: 6379,
redis_host: '127.0.0.1'
}
The default settings are port `6379` and `127.0.0.1`.
-### Max Redis Clients
-Sets maximum number of resources to create at any given time.
+### Additional Redis Options
+You can pass any *options* you normally would pass to the **redis.createClient()** function in `node-redis`. See the [node-redis documentation](https://github.com/mranney/node_redis#rediscreateclientport-host-options) for more information.
- settings = {
- redis_max: 10
+ options = {
+ redis_options: redisoptions…
}
-
-The default value is 10.
-### Min Redis Clients
-Sets minimum number of resources to keep in pool at any given time.
+You can also provide the redis server password by setting *redis_password*. **NOTE: This still needs testing…**
- settings = {
- redis_min: 5
- }
+## Generic Pool Options
+These options are used to control the **generic-pool**. You will normally not need to use any of these options.
+### Number of Clients
+Optional minimum and maximum clients to have ready in the pool. The default values are 10 for the maximum and 2 for the minimum.
-The default value is 2.
+ options = {
+ max_clients: 10,
+ min_clients: 5
+ }
-## Advanced Settings.
### Generic Pool - Timeouts
-You can adjust the *reapIntervalMillis* by setting *redis_reap_timeout*. The *idleTimeoutMillis* can be set by setting *redis_timeout*.
+You can adjust the **generic-pool** *reapIntervalMillis* and *idleTimeoutMillis*.
- settings = {
- redis_reap_timeout: 5000,
- redis_timeout: 30000
+ options = {
+ reapIntervalMillis: 5000,
+ idleTimeoutMillis: 30000
}
-### Generic Pool Logging
-If you would like to see what the *generic-pool* module is doing your can enable console logging by setting *redis_log_pool* to `true`. This feature is off by default.
+### Logging
+If you would like to see what the *generic-pool* module is doing your can enable console logging by setting *logging* to `true`. This feature is off by default.
- settings = {
- redis_log_pool: true
+ options = {
+ logging: true
}
View
@@ -1,34 +1,27 @@
-/* This example uses the Pool.acquire function to
- * acquire a client connection. */
-
-// You can customize the connection settings like this.
-settings = {
- redis_port: 6379,
+// Example 1: Using the acquireHelper.
+// Our Settings
+options = {
redis_host: '127.0.0.1',
- redis_log_pool: false, // Turn on/off the generic-pool logging.
- redis_timeout: 2000,
- redis_min: 0
-}
-
-var Pool = require('sol-redis-pool'); //sol-redis-pool
-
-function Example(err, client) {
- // Use redis client like you normally would.
- console.log("You should see: null 'PONG'");
- client.ping(function(err, result){
- console.log(err, result);
- // When you are done with the client release it like this.
- Pool.release(client);
- });
+ redis_port: 6379,
}
-
-Pool.acquire(Example);
+
+var RedisPool = require('./index');
+var pool = new RedisPool(options);
-// Stop the example after 10 seconds.
-function stopExample() {
- Pool.Drain(Pool, function() {
- console.log('Pool drained, see ya.');
- });
-}
-setTimeout(stopExample, 10000);
+// Handle the error here...
+function errorCallback(err) {}
+function clientCallback(client) {
+ // Use the client then release it back to the pool.
+ client.ping(function(err, result) {
+ console.log(err, result);
+ // Release the client...
+ pool.release(client);
+ // Drain the pool so the example will end.
+ pool.drain(function(){
+ console.log('Done...');
+ });
+ })
+}
+console.log("If everything is working, you should see 'null 'PONG'.")
+pool.acquireHelper(errorCallback, clientCallback);
View
@@ -1,37 +0,0 @@
-/* This example uses the Pool.AcquireHelper function to
- * acquire a client connection. */
-
-// You can customize the connection settings like this.
-settings = {
- redis_port: 6379,
- redis_host: '127.0.0.1',
- redis_log_pool: false, // Turn on/off the generic-pool logging.
- redis_timeout: 2000,
- redis_min: 0
-}
-
-var Pool = require('sol-redis-pool');
-
-function errorCallback(err) {
- console.log('Error: ', err);
-}
-
-function successCallback(client) {
- // Use the client hereÉ
- client.ping(function(err, result) {
- console.log(err, result);
-
- // When done release the client.
- Pool.release(client)
- })
-}
-
-Pool.AcquireHelper(errorCallback, successCallback);
-
-// Stop the example after 10 seconds.
-function stopExample() {
- Pool.Drain(Pool, function() {
- console.log('Pool drained, see ya.');
- });
-}
-setTimeout(stopExample, 10000);
View
@@ -1,31 +1,62 @@
var redis = require('redis')
, Pool = require('generic-pool').Pool;
-var RedisPool = Pool({
- name: 'redis',
- create: function(callback) {
- callback(null, redis.createClient(global.settings.redis_port || 6379, global.settings.redis_host || '127.0.0.1'));
- },
- destroy: function(client) { client.end();},
- max: global.settings.redis_max || 10,
- min: global.settings.redis_min || 2,
- reapIntervalMillis: global.settings.redis_reap_timeout || 1000,
- idleTimeoutMillis: global.settings.redis_timeout || 30000,
- log: global.settings.redis_log_pool || false
-});
+function RedisPool(options) {
+ this.options = options;
+ this.pool = Pool({
+ name: 'redis',
+ create: function(callback) {
+ try {
+ var client = redis.createClient(
+ options.redis_port || 6379,
+ options.redis_host || '127.0.0.1',
+ options.redis_options || {}
+ )
+ // Handle the client authentication if provided.
+ if(options.password) {
+ client.auth(options.password)
+ }
+ callback(null, client);
+ } catch(e) {
+ callback(e, null)
+ }
+ },
+ destroy: function(client) {
+ client.end();
+ },
+ max: options.max_clients || 10,
+ min: options.min_clients || 2,
+ reapIntervalMillis: options.reapIntervalMillis || 1000,
+ idleTimeoutMillis: options.idleTimeoutMillis || 30000,
+ log: options.logging || false
+ })
+}
-// You can use this function to drain the pool.
-RedisPool.Drain = function(pool, callback) {
- pool.drain(function() {
- pool.destroyAllNow();
+// Drains the connection pool.
+RedisPool.prototype.drain = function(callback) {
+ var self = this;
+ self.pool.drain(function() {
+ self.pool.destroyAllNow();
callback();
})
}
-// The AcquireHelper is a shortcut for acquiring a client and handling errors.
-RedisPool.AcquireHelper = function AcquireHelper(errorCallback, successCallback) {
- RedisPool.acquire(function(err, client) {
+
+// Used to acquire a client connection.
+RedisPool.prototype.acquire = function(callback) {
+ this.pool.acquire(callback);
+}
+
+// Used to release a client connection.
+RedisPool.prototype.release = function(client) {
+ this.pool.release(client);
+}
+
+// Acquires a client and gives you two callbacks.
+RedisPool.prototype.acquireHelper = function(errorCallback, clientCallback) {
+ this.pool.acquire(function(err, client) {
if(err) { return errorCallback(err, false);}
- return successCallback(client);
+ return clientCallback(client);
})
}
+
module.exports = RedisPool;
View
@@ -1,7 +1,7 @@
{
"name": "sol-redis-pool",
"description": "Generic resource pooling for Node.JS",
- "version": "0.0.3",
+ "version": "0.1.0",
"author": "Joshua Heiks <joshuaheiks@gmail.com>",
"keywords": ["pool", "redis"],
"main": "index.js",

0 comments on commit 9d82338

Please sign in to comment.