-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
230 lines (197 loc) · 6.06 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var inherits = require('util').inherits
var EventEmitter = require('events').EventEmitter
var GenericPool = require('generic-pool').Pool
var chain = require('./lib/chain')
module.exports = ConnectionPool
inherits(ConnectionPool, EventEmitter)
function ConnectionPool(adapter, connParams, options) {
if (!(this instanceof ConnectionPool)) {
return new ConnectionPool(adapter, connParams, options)
}
EventEmitter.call(this)
options = options || {}
connParams = connParams || {}
if (options.create) {
console.warn("PoolConfig.create ignored, use PoolConfig.onConnect instead")
}
if (options.destroy) {
console.warn("PoolConfig.destroy ignored")
}
if (adapter.name == 'sqlite3'
&& /:memory:$/i.test(connParams.database)
&& (options.min > 1 || options.max > 1))
{
console.warn(
"Pools of more than 1 connection do not work for in-memory SQLite3 databases\n" +
"The specified minimum (%d) and maximum (%d) connections have been overridden",
options.min, options.max
)
if (options.min) options.min = 1
options.max = 1
}
var onConnect = options.onConnect || function (c, done) { done(null, c) }
var poolOpts = {
min: options.min || 0,
max: options.max || 10,
create: function (ready) {
adapter.createConnection(connParams, function (err, conn) {
if (err) return ready(err);
onConnect(conn, function (err, connection) {
if (err) return ready(err);
conn.on('error', self._handleIdleError)
ready(null, connection)
})
})
},
destroy: function (connection) {
connection.removeAllListeners()
connection.on('error', function () {})
connection.end()
},
log: options.log,
idleTimeoutMillis: options.idleTimeout,
reapIntervalMillis: options.reapInterval,
}
if (options.hasOwnProperty('refreshIdle')) {
poolOpts.refreshIdle = options.refreshIdle
}
this._pool = new GenericPool(poolOpts)
this._cancelledQueries = []
var resetSteps = []
if (adapter.reset) resetSteps.unshift(adapter.reset)
if (options.reset) resetSteps.unshift(options.reset)
this.adapter = adapter
this._reset = chain(resetSteps)
this._shouldDestroyConnection = function (err) {
if (err instanceof CancelledQueryError) {
return false
}
return options.shouldDestroyConnection ? options.shouldDestroyConnection(err) : true
}
var self = this
self._handleIdleError = function (err) {
var connection = this
self._maybeDestroy(connection, err)
self.emit('error', err, connection)
}
}
ConnectionPool.prototype.query = function (statement, params, callback) {
var self = this
, query = this.adapter.createQuery(statement, params, callback)
, connection = null
if (query.callback) {
callback = query.callback
query.callback = function (err, result) {
self._maybeDestroy(connection, err)
callback(err, result)
}
} else {
var finished = false
query.once('end', function () {
if (!finished) {
finished = true
self.release(connection)
}
})
query.once('error', function (err) {
if (!finished) {
finished = true
self._maybeDestroy(connection, err)
}
// If this was the only error listener, re-emit the error from the pool.
if (!this.listeners('error').length) {
self.emit('error', err, query)
}
})
}
/**
* if a connection cannot be acquired, or emits an 'error' event while a
* query is in progress, the error should be handled by the query object.
*/
function handleConnectionError (error) {
self._maybeDestroy(connection, error)
forwardError(error)
}
function forwardError (error) {
if (query.callback) {
query.callback(error)
} else {
query.emit('error', error)
}
}
this.acquire(function (err, connection_) {
if (err) {
return handleConnectionError(err)
}
err = self._checkCancellation(query)
if (err) {
return forwardError(err)
}
// expose the connection to everything else in the outer scope
connection = connection_
// attach error event listener to the connection
connection.on('error', handleConnectionError)
query.once('end', function () {
connection.removeListener('error', handleConnectionError)
})
connection.query(query);
self.emit('query', query)
})
return query
}
ConnectionPool.prototype.cancel = function (query) {
this._cancelledQueries.push(query)
}
ConnectionPool.prototype.acquire = function (callback) {
var self = this
self._pool.acquire(function (err, connection) {
if (err) return callback(err);
connection.removeListener('error', self._handleIdleError)
self.emit('acquire', connection)
callback(null, connection)
});
}
ConnectionPool.prototype.release = function (connection) {
var self = this
self.emit('release', connection)
connection.removeAllListeners()
self._reset(connection, function (err) {
if (err) return self.destroy(connection);
connection.on('error', self._handleIdleError)
self._pool.release(connection)
})
}
ConnectionPool.prototype.destroy = function (connection) {
this._pool.destroy(connection)
}
ConnectionPool.prototype.close = function (callback) {
var self = this
this._pool.drain(function () {
self._pool.destroyAllNow(function () {
self.emit('close')
if (callback) callback()
})
})
}
ConnectionPool.prototype._maybeDestroy = function (connection, error) {
if (connection) {
if (error && this._shouldDestroyConnection(error)) {
this.destroy(connection)
} else {
this.release(connection)
}
}
}
ConnectionPool.prototype._checkCancellation = function (query) {
var cancelIndex = this._cancelledQueries.indexOf(query)
if (cancelIndex >= 0) {
this._cancelledQueries.splice(cancelIndex, 1)
return new CancelledQueryError()
}
}
inherits(CancelledQueryError, Error)
function CancelledQueryError() {
Error.call(this)
this.message = "Query was cancelled before connection was acquired"
this.name = "CancelledQueryError"
}