From e648d491151484f17263c6774678f1e7ac2fa188 Mon Sep 17 00:00:00 2001 From: Peter Marton Date: Fri, 13 Dec 2019 13:43:30 -0800 Subject: [PATCH] fix(server): address domain performance regression with Node v12.x (#1809) --- lib/server.js | 10 +++++++++- package.json | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/server.js b/lib/server.js index d3da14442..cf53b477e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -2,7 +2,6 @@ 'use strict'; -var domain = require('domain'); var EventEmitter = require('events').EventEmitter; var http = require('http'); var https = require('https'); @@ -27,6 +26,7 @@ var customErrorTypes = require('./errorTypes'); var patchRequest = require('./request'); var patchResponse = require('./response'); +var domain; var http2; patchResponse(http.ServerResponse); @@ -930,6 +930,14 @@ Server.prototype._onRequest = function _onRequest(req, res) { // It has significant negative performance impact // Warning: this feature depends on the deprecated domains module if (self.handleUncaughtExceptions) { + // In Node v12.x requiring the domain module has a negative performance + // impact. As using domains in restify is optional and only required + // with the `handleUncaughtExceptions` options, we apply a singleton + // pattern to avoid any performance regression in the default scenario. + if (!domain) { + domain = require('domain'); + } + var handlerDomain = domain.create(); handlerDomain.add(req); handlerDomain.add(res); diff --git a/package.json b/package.json index e384b9d44..494328b2d 100644 --- a/package.json +++ b/package.json @@ -108,7 +108,7 @@ "once": "^1.4.0", "pidusage": "^2.0.17", "qs": "^6.7.0", - "restify-errors": "^8.0.0", + "restify-errors": "^8.0.2", "semver": "^6.1.1", "send": "^0.16.2", "spdy": "^4.0.0",