@@ -13,6 +13,8 @@ var PG = function(clientConstructor) {
13
13
this . pools = pool ;
14
14
this . Connection = Connection ;
15
15
this . types = require ( 'pg-types' ) ;
16
+ this . num_connections = 0 ;
17
+ this . max_connections = this . defaults . max_connections ;
16
18
} ;
17
19
18
20
util . inherits ( PG , EventEmitter ) ;
@@ -35,28 +37,26 @@ PG.prototype.end = function() {
35
37
} ) ;
36
38
} ;
37
39
38
-
39
40
PG . prototype . connect = function ( config , callback ) {
40
41
if ( typeof config == "function" ) {
41
42
callback = config ;
42
43
config = null ;
43
44
}
45
+ if ( this . max_connections && this . num_connections >= this . max_connections ) {
46
+ return callback && callback ( Error ( "Connection limit" , this . max_connections , "reached" ) ) ;
47
+ }
44
48
var client = new this . Client ( config ) ;
49
+ // Max connections waiting to connect
50
+ this . num_connections ++ ;
45
51
client . connect ( function ( err ) {
52
+ this . num_connections -- ;
46
53
if ( err ) {
47
54
return callback && callback ( err ) ;
48
55
}
49
56
return callback && callback ( null , client , function ( ) {
50
57
client . end ( ) ;
51
58
} ) ;
52
- // client.query()
53
59
} ) ;
54
- /* var pool = this.pools.getOrCreate(config);
55
- pool.connect(callback);
56
- if(!pool.listeners('error').length) {
57
- //propagate errors up to pg object
58
- pool.on('error', this.emit.bind(this, 'error'));
59
- } */
60
60
} ;
61
61
62
62
// cancel the query runned by the given client
0 commit comments