Skip to content

Commit 9e72b74

Browse files
committed
refactor: cleanup handlers
1 parent 257f679 commit 9e72b74

File tree

1 file changed

+24
-39
lines changed

1 file changed

+24
-39
lines changed

index.js

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ class ErrorHandler {
219219
this.resOrSocket = null
220220
this.callback = null
221221

222+
this._release = this._release.bind(this)
222223
this._handle = this._handle.bind(this)
223224
this._handle.requestTimeout = this._requestTimeout.bind(this)
224225
}
@@ -246,28 +247,27 @@ class ErrorHandler {
246247

247248
if (this.callback) {
248249
this.callback(err, this.req, this.resOrSocket)
249-
ErrorHandler.release(this)
250250
} else {
251-
ErrorHandler.release(this)
252251
throw err
253252
}
254253
}
255254

256-
static create (req, resOrSocket, callback) {
257-
const errorHandler = ErrorHandler.pool.pop() || new ErrorHandler()
258-
errorHandler.hasError = false
259-
errorHandler.req = req
260-
errorHandler.resOrSocket = resOrSocket
261-
errorHandler.callback = callback
262-
return errorHandler._handle
255+
_release () {
256+
this.hasError = false
257+
this.req = null
258+
this.resOrSocket = null
259+
this.callback = null
260+
ErrorHandler.pool.push(this)
263261
}
264262

265-
static release (obj) {
266-
obj.hasError = false
267-
obj.req = null
268-
obj.resOrSocket = null
269-
obj.callback = null
270-
ErrorHandler.pool.push(obj)
263+
static create (req, resOrSocket, callback) {
264+
const handler = ErrorHandler.pool.pop() || new ErrorHandler()
265+
handler.hasError = false
266+
handler.req = req
267+
handler.resOrSocket = resOrSocket
268+
handler.callback = callback
269+
handler.req.on('close', handler._release)
270+
return handler._handle
271271
}
272272
}
273273
ErrorHandler.pool = []
@@ -279,18 +279,12 @@ class ProxyErrorHandler {
279279
this.proxyReq = null
280280
this.onError = null
281281

282-
this._handle = this._handle.bind(this)
283282
this._release = this._release.bind(this)
283+
this._handle = this._handle.bind(this)
284284
this._handle.gatewayTimeout = this._gatewayTimeout.bind(this)
285285
this._handle.socketHangup = this._socketHangup.bind(this)
286286
}
287287

288-
_abort () {
289-
if (!this.proxyReq.aborted) {
290-
this.proxyReq.abort()
291-
}
292-
}
293-
294288
_handle (err) {
295289
if (this.hasError) {
296290
return
@@ -322,12 +316,14 @@ class ProxyErrorHandler {
322316
this._handle(createError('socket hang up', 'ECONNRESET', 502))
323317
}
324318

325-
_release () {
326-
this.req.removeListener('close', this._release)
327-
328-
if (this.hasError) {
329-
this._abort()
319+
_abort () {
320+
if (!this.proxyReq.aborted) {
321+
this.proxyReq.abort()
330322
}
323+
}
324+
325+
_release () {
326+
this._abort()
331327

332328
this.hasError = null
333329
this.req = null
@@ -406,8 +402,6 @@ class ProxyResponseHandler {
406402
}
407403

408404
_release () {
409-
this.req.removeListener('close', this._release)
410-
411405
this.req = null
412406
this.resOrSocket = null
413407
this.onRes = null
@@ -426,10 +420,6 @@ class ProxyResponseHandler {
426420
handler.req.on('close', handler._release)
427421
return handler._handle
428422
}
429-
430-
static release (obj) {
431-
ProxyResponseHandler.pool.push(obj)
432-
}
433423
}
434424
ProxyResponseHandler.pool = []
435425

@@ -441,8 +431,8 @@ class ProxyUpgradeHandler {
441431
this.proxyRes = null
442432
this.proxySocket = null
443433

444-
this._handle = this._handle.bind(this)
445434
this._release = this._release.bind(this)
435+
this._handle = this._handle.bind(this)
446436
}
447437

448438
_handle (proxyRes, proxySocket, proxyHead) {
@@ -486,7 +476,6 @@ class ProxyUpgradeHandler {
486476
}
487477

488478
_release () {
489-
this.req.removeListener('close', this._release)
490479
this.proxyRes.destroy()
491480
this.proxySocket.destroy()
492481

@@ -506,9 +495,5 @@ class ProxyUpgradeHandler {
506495
handler.req.on('close', handler._release)
507496
return handler._handle
508497
}
509-
510-
static release (obj) {
511-
ProxyUpgradeHandler.pool.push(obj)
512-
}
513498
}
514499
ProxyUpgradeHandler.pool = []

0 commit comments

Comments
 (0)