forked from Automattic/kue
/
pool.js
53 lines (41 loc) · 829 Bytes
/
pool.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
/*!
* kue - pool
* Copyright (c) 2010 LearnBoost <tj@learnboost.com>
* MIT Licensed
*/
/**
* Module dependencies.
*/
var redis = require('../redis');
/**
* Max connections in the pool.
*/
exports.maxConnections = 5;
/**
* Connection pool.
*/
exports.pool = [];
/**
* Allocate a redis connection.
*
* @return {RedisClient}
* @api private
*/
exports.alloc = function(){
var client;
if (exports.pool.length == exports.maxConnections) {
client = exports.pool[Math.random() * exports.maxConnections | 0];
} else {
exports.pool.push(client = redis.createClient());
}
return client;
};
/**
* Return the pubsub-specific redis client.
*
* @return {RedisClient}
* @api private
*/
exports.pubsubClient = function(){
return exports._pubsub || (exports._pubsub = redis.createClient());
};