diff --git a/lib/plugins/cpuUsageThrottle.js b/lib/plugins/cpuUsageThrottle.js index bf6b2077c..c7a2f870c 100644 --- a/lib/plugins/cpuUsageThrottle.js +++ b/lib/plugins/cpuUsageThrottle.js @@ -105,6 +105,7 @@ function cpuUsageThrottle (opts) { // use to gather CPU load averages, this allows us to cancel the timeout // when shutting down restify. self._timeout = null; + // self._timeoutDelta represents the amount of time between when we _should_ // have run updateReject and the actual time it was invoked. This allows // us to monitor lag caused by both the event loop and pidusage.stat @@ -115,6 +116,10 @@ function cpuUsageThrottle (opts) { // usage between two invocations of updateReject. function updateReject() { pidusage.stat(process.pid, function (e, stat) { + // Requeue an updateReject irrespective of whether or not pidusage + // encountered an error + self._timeout = setTimeout(updateReject, self._interval); + // If we were unable to get cpu usage, don't make any new decisions. if (!stat || typeof stat.cpu !== 'number' || @@ -132,7 +137,10 @@ function cpuUsageThrottle (opts) { // negative and we will never shed load self._reject = (self._cpu - self._limit) / (self._max - self._limit); - self._timeout = setTimeout(updateReject, self._interval); + + // Calculate how long it took between when our interval should have + // updated the _reject value and how long it actually took. This + // metric accounts for the misbehaviour of pidusage.stat var now = Date.now(); self._timeoutDelta = now - self._timeoutStart; self._timeoutStart = now;