@@ -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 ) ;
@@ -39,28 +41,26 @@ PG.prototype.end = function() {
39
41
}
40
42
} ;
41
43
42
-
43
44
PG . prototype . connect = function ( config , callback ) {
44
45
if ( typeof config == "function" ) {
45
46
callback = config ;
46
47
config = null ;
47
48
}
49
+ if ( this . max_connections && this . num_connections >= this . max_connections ) {
50
+ return callback && callback ( Error ( "Connection limit" , this . max_connections , "reached" ) ) ;
51
+ }
48
52
var client = new this . Client ( config ) ;
53
+ // Max connections waiting to connect
54
+ this . num_connections ++ ;
49
55
client . connect ( function ( err ) {
56
+ this . num_connections -- ;
50
57
if ( err ) {
51
58
return callback && callback ( err ) ;
52
59
}
53
60
return callback && callback ( null , client , function ( ) {
54
61
client . end ( ) ;
55
62
} ) ;
56
- // client.query()
57
63
} ) ;
58
- /* var pool = this.pools.getOrCreate(config);
59
- pool.connect(callback);
60
- if(!pool.listeners('error').length) {
61
- //propagate errors up to pg object
62
- pool.on('error', this.emit.bind(this, 'error'));
63
- } */
64
64
} ;
65
65
66
66
// cancel the query runned by the given client
0 commit comments