Skip to content

Commit

Permalink
fix(cpuUsageThrottle): Always queue a new timeout (#1484)
Browse files Browse the repository at this point in the history
  • Loading branch information
William Blankenship committed Sep 11, 2017
1 parent 33b0e90 commit e4ffe43
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/plugins/cpuUsageThrottle.js
Expand Up @@ -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
Expand All @@ -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' ||
Expand All @@ -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;
Expand Down

0 comments on commit e4ffe43

Please sign in to comment.