1- var genericPool = require ( "generic-pool" ) ;
2- var redis = require ( "./redis" ) ;
3- var logger = require ( "./logger" ) ;
1+ "use strict" ;
42
5- var debug = require ( "debug" ) ( "simpleRedisPool" ) ;
3+ const genericPool = require ( "generic-pool" ) ;
4+ const redis = require ( "./redis" ) ;
5+ const logger = require ( "./logger" ) ;
66
7- function RedisPool ( name , redisOptions , poolOptions ) {
7+ const debug = require ( "debug" ) ( "simpleRedisPool" ) ;
88
9- this . _redisDb = redisOptions . db || 0 ;
10-
11- var _pool = null ;
12- var _poolOptions = { } ;
13- _poolOptions . name = name ;
14-
15- // Build new Redis database clients.
16- _poolOptions . create = function ( cb ) {
17- var client = redis . createClient ( redisOptions ) ;
18-
19- if ( [ "undefined" , null , "" ] . indexOf ( this . _redisDb ) !== - 1 ) {
20- logger . info ( "Selected Redis DB: " , this . _redisDb ) ;
21- debug ( "Selected Redis DB: " , this . _redisDb ) ;
22- client . select ( this . _redisDb ) ;
23- }
24-
25- // Handle client connection errors.
26- client . on ( "error" , function ( err ) {
27- logger . error ( "Redis pool: " , name , err ) ;
28- throw err ;
29- } ) ;
9+ function RedisPool ( name , redisOptions = { } , poolOptions = { } ) {
3010
31- // Register the authentication password if needed.
32- if ( redisOptions . auth_pass ) {
33- client . auth ( redisOptions . auth_pass ) ;
34- }
35-
36- cb ( null , client ) ;
37- } ;
38-
39- // The destroy function is called when client connection needs to be closed.
40- _poolOptions . destroy = function ( client ) {
41- try {
42- // Flush when closing.
43- client . end ( true ) ;
44- logger . info ( "Checking pool info after client destroyed. Available count : %s. Pool size: %s" , _pool . availableObjectsCount ( ) , _pool . getPoolSize ( ) ) ;
11+ this . _redisDb = redisOptions . db || 0 ;
4512
46- } catch ( err ) {
47- throw new Error ( "error" , "Error destroying redis client." ) ;
13+ let _pool = null ;
14+
15+ const factory = {
16+ create : ( ) => {
17+ return new Promise ( ( resolve , reject ) => {
18+
19+ const client = redis . createClient ( redisOptions ) ;
20+
21+ if ( [ "undefined" , null , "" ] . indexOf ( this . _redisDb ) !== - 1 ) {
22+ logger . info ( "Selected Redis DB: " , this . _redisDb ) ;
23+ debug ( "Selected Redis DB: " , this . _redisDb ) ;
24+ client . select ( this . _redisDb ) ;
25+ }
26+
27+ // Handle client connection errors.
28+ client . on ( "error" , err => {
29+ logger . error ( "Redis pool: " , name , err ) ;
30+ reject ( err ) ;
31+ } ) ;
32+
33+ // Register the authentication password if needed.
34+ if ( redisOptions . auth_pass ) {
35+ client . auth ( redisOptions . auth_pass ) ;
36+ }
37+
38+ resolve ( client ) ;
39+ } ) ;
40+ } ,
41+ destroy : ( client ) => {
42+ return new Promise ( ( resolve ) => {
43+
44+ try {
45+ // Flush when closing.
46+ client . end ( true ) ;
47+ logger . info ( "Checking pool info after client destroyed. Available count : %s. Pool size: %s" , _pool . availableObjectsCount ( ) , _pool . getPoolSize ( ) ) ;
48+ resolve ( ) ;
49+ } catch ( err ) {
50+ throw new Error ( "error" , "Error destroying redis client." ) ;
51+ }
52+ } ) ;
4853 }
4954 } ;
5055
51- if ( poolOptions ) {
52- Object . assign ( _poolOptions , poolOptions ) ;
53- }
54-
5556 // Now that the pool settings are ready create a pool instance.
56- _pool = new genericPool . Pool ( _poolOptions ) ;
57+ _pool = genericPool . createPool ( factory , poolOptions ) ;
58+
59+ // set name
60+ this . name = name ;
5761
5862 // Acquire a database connection and use an optional priority.
59- this . acquire = function ( cb , priority ) {
60- _pool . acquire ( cb , priority ) ;
63+ this . acquire = ( priority ) => {
64+ return _pool . acquire ( priority ) ;
6165 } ;
6266
63- this . acquireDb = function ( cb , db , priority ) {
64- _pool . acquire ( function ( err , client ) {
67+ this . acquireDb = ( cb , db , priority ) => {
68+ _pool . acquire ( ( err , client ) => {
6569 if ( ! err && client . _db_selected !== db ) {
6670 client [ "_db_selected" ] = db ;
6771 client . select ( db ) ;
@@ -73,7 +77,7 @@ function RedisPool (name, redisOptions, poolOptions) {
7377 } ;
7478
7579 // Release a database connection to the pool.
76- this . release = function ( client ) {
80+ this . release = ( client ) => {
7781 // Always reset the DB to the default. This prevents issues
7882 // if a user used the select command to change the DB.
7983
@@ -85,8 +89,8 @@ function RedisPool (name, redisOptions, poolOptions) {
8589 } ;
8690
8791 // Drains the connection pool and call the callback id provided.
88- this . drain = function ( cb ) {
89- _pool . drain ( function ( ) {
92+ this . drain = ( cb ) => {
93+ _pool . drain ( ( ) => {
9094 _pool . destroyAllNow ( ) ;
9195 if ( cb ) {
9296 cb ( ) ;
@@ -95,33 +99,33 @@ function RedisPool (name, redisOptions, poolOptions) {
9599 } ;
96100
97101 // Returns factory.name for this pool
98- this . getName = function ( ) {
99- return _pool . getName ( ) ;
102+ this . getName = ( ) => {
103+ return this . name ;
100104 } ;
101105
102- this . getDB = function ( ) {
106+ this . getDB = ( ) => {
103107 return this . _redisDb ;
104108 } ;
105109
106- this . getPoolSize = function ( ) {
107- return _pool . getPoolSize ( ) ;
110+ this . getPoolSize = ( ) => {
111+ return _pool . size ;
108112 } ;
109113
110- this . availableObjectsCount = function ( ) {
111- return _pool . availableObjectsCount ( ) ;
114+ this . availableObjectsCount = ( ) => {
115+ return _pool . available ;
112116 } ;
113117
114- this . waitingClientsCount = function ( ) {
115- return _pool . waitingClientsCount ( ) ;
118+ this . waitingClientsCount = ( ) => {
119+ return _pool . pending ;
116120 } ;
117121
118- this . status = function ( ) {
122+ this . status = ( ) => {
119123 return {
120- name : _pool . getName ( ) ,
121- size : _pool . getPoolSize ( ) ,
124+ name : this . name ,
125+ size : _pool . size ,
122126 db : this . _redisDb ,
123- avail : _pool . availableObjectsCount ( ) ,
124- waiting : _pool . waitingClientsCount ( )
127+ avail : _pool . available ,
128+ waiting : _pool . pending
125129 } ;
126130 } ;
127131}
0 commit comments