forked from joshuah/sol-redis-pool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·169 lines (143 loc) · 4.91 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var redis = require('redis');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var Pool = require('generic-pool').Pool;
var bluebird = require('bluebird');
try {
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
} catch (err) {
//swallow bluebird errors that happen after node promisifies the lib twice.
}
var SUPPORTED_REDIS_OPTIONS = [
'host', 'port', 'path', 'url', 'password',
'string_numbers', 'return_buffers', 'detect_buffers',
'socket_keepalive', 'no_ready_check', 'enable_offline_queue',
'retry_unfulfilled_commands', 'family', 'disable_resubscribing',
'rename_commands', 'auth_pass', 'db', 'retry_strategy', 'tls', 'prefix'
];
var SUPPORTED_POOL_OPTIONS = [
'name', 'max', 'min', 'refreshIdle', 'idleTimeoutMillis',
'reapIntervalMillis', 'returnToHead', 'priorityRange'
];
function isFunction(functionToCheck) {
var getType = {};
return functionToCheck && getType.toString.call(functionToCheck) === '[object Function]';
}
function copyAllowedKeys(allowed, source, destination) {
function copyAllowedKey(key) {
if (key in source) {
destination[key] = source[key];
}
}
allowed.forEach(copyAllowedKey);
return destination;
}
function RedisPool(redisOptions, poolOptions) {
var self = this;
self._pool = null;
self._redis_default_db = redisOptions.db || 0;
self._redis_options = copyAllowedKeys(SUPPORTED_REDIS_OPTIONS, redisOptions, {});
self._pool_options = copyAllowedKeys(SUPPORTED_POOL_OPTIONS, poolOptions, {});
}
util.inherits(RedisPool, EventEmitter);
// Initialize the RedisPool.
RedisPool.prototype._initialize = function() {
var self = this;
var redisSettings = self._redis_options;
var poolSettings = self._pool_options;
// The next connection id number for tracking.
var nextCid = 0;
// Build new Redis database clients.
poolSettings.create = function(cb) {
// Create a new redis client.
var client = redis.createClient(self._redis_options);
// Assign an unique connection id to this redis client.
client._sol_cid = ++nextCid;
// Handle client connection errors.
client.on('error', function(err) {
// Add the client connection id (cid) to the error data before it is emitted.
err["cid"] = client._sol_cid;
self.emit('error', err);
});
// Let the application know that a pool client is attempting to reconnect.
client.on('reconnecting', function(info) {
// Add the client connection id (cid) to the reconnecting event data before it is emitted.
info['cid'] = client._sol_cid;
self.emit('reconnecting', info);
});
cb(null, client);
};
// The destroy function is called when client connection needs to be closed.
poolSettings.destroy = function(client) {
var _cid = null;
var _err = null;
try {
_cid = client._sol_cid;
// Always flush when closing.
client.end(true);
} catch(err) {
_err = err;
client = null;
}
self.emit('destroy', _err, _cid);
};
// Now that the pool settings are ready create a pool instance.
self._pool = Pool(poolSettings);
return this;
};
// Acquire a database connection and use an optional priority.
RedisPool.prototype.acquire = function(cb, priority) {
this._pool.acquire(cb, priority);
};
RedisPool.prototype.acquireDb = function(cb, db, priority) {
this._pool.acquire(function(err, client) {
if (!err && client._db_selected !== db) {
client._db_selected = db;
client.select(db);
}
return cb(err, client);
}, priority);
};
// Release a database connection to the pool.
RedisPool.prototype.release = function(client) {
var self = this;
if (!client.connected) {
this._pool.destroy(client);
} else {
// Always reset the DB to the default. This prevents issues
// if a user used the select command to change the DB.
if (client._db_selected !== self._redis_default_db) {
client.select(self._redis_default_db);
}
this._pool.release(client);
}
};
// Drains the connection pool and call the callback id provided.
RedisPool.prototype.drain = function(cb) {
var self = this;
self._pool.drain(function() {
self._pool.destroyAllNow(cb);
});
};
// Returns factory.name for this pool
RedisPool.prototype.getName = function() {
return this._pool.getName();
};
// Returns number of resources in the pool regardless of
// whether they are free or in use
RedisPool.prototype.getPoolSize = function() {
return this._pool.getPoolSize();
};
// Returns number of unused resources in the pool
RedisPool.prototype.availableObjectsCount = function() {
return this._pool.availableObjectsCount();
};
// Returns number of callers waiting to acquire a resource
RedisPool.prototype.waitingClientsCount = function() {
return this._pool.waitingClientsCount();
};
// Export this module.
module.exports = function(redisOptions, poolOptions) {
return new RedisPool(redisOptions, poolOptions)._initialize();
};