@hapi/heavy
Advanced tools
Comparing version 7.0.1 to 8.0.0
129
lib/index.js
'use strict'; | ||
const PerfHooks = require('perf_hooks'); | ||
const Boom = require('@hapi/boom'); | ||
@@ -15,2 +16,3 @@ const Hoek = require('@hapi/hoek'); | ||
maxEventLoopDelay: Validate.number().min(0), | ||
maxEventLoopUtilization: Validate.number().min(0), | ||
maxRssBytes: Validate.number().min(0) | ||
@@ -25,92 +27,107 @@ }) | ||
maxRssBytes: 0, // Reject requests when process RSS is over size in bytes (zero is no max) | ||
maxEventLoopDelay: 0 // Milliseconds of delay after which requests are rejected (zero is no max) | ||
maxEventLoopDelay: 0, // Milliseconds of delay after which requests are rejected (zero is no max) | ||
maxEventLoopUtilization: 0 // Max event loop utilization value after which requests are rejected (zero is no max) | ||
}; | ||
exports = module.exports = internals.Heavy = function (options) { | ||
exports.Heavy = class Heavy { | ||
options = options || {}; | ||
constructor(options) { | ||
Validate.assert(options, internals.schema, 'Invalid load monitoring options'); | ||
this.settings = Hoek.applyToDefaults(internals.defaults, options); | ||
Hoek.assert(this.settings.sampleInterval || (!this.settings.maxEventLoopDelay && !this.settings.maxHeapUsedBytes && !this.settings.maxRssBytes), 'Load sample interval must be set to enable load limits'); | ||
options = options || {}; | ||
this._eventLoopTimer = null; | ||
this._loadBench = new Hoek.Bench(); | ||
this.load = { | ||
eventLoopDelay: 0, | ||
heapUsed: 0, | ||
rss: 0 | ||
}; | ||
}; | ||
Validate.assert(options, internals.schema, 'Invalid load monitoring options'); | ||
this.settings = Hoek.applyToDefaults(internals.defaults, options); | ||
Hoek.assert(this.settings.sampleInterval || (!this.settings.maxEventLoopDelay && !this.settings.maxHeapUsedBytes && !this.settings.maxRssBytes && !this.settings.maxEventLoopUtilization), 'Load sample interval must be set to enable load limits'); | ||
this._eventLoopTimer = null; | ||
this._eventLoopUtilization = PerfHooks.performance.eventLoopUtilization(); | ||
this._loadBench = new Hoek.Bench(); | ||
this.load = { | ||
eventLoopDelay: 0, | ||
eventLoopUtilization: 0, | ||
heapUsed: 0, | ||
rss: 0 | ||
}; | ||
} | ||
internals.Heavy.prototype.start = function () { | ||
start() { | ||
if (!this.settings.sampleInterval) { | ||
return; | ||
} | ||
if (!this.settings.sampleInterval) { | ||
return; | ||
} | ||
const loopSample = () => { | ||
const loopSample = () => { | ||
this._loadBench.reset(); | ||
const measure = () => { | ||
this._loadBench.reset(); | ||
const measure = () => { | ||
const mem = process.memoryUsage(); | ||
const mem = process.memoryUsage(); | ||
// Retain the same this.load object to keep external references valid | ||
// Retain the same this.load object to keep external references valid | ||
this.load.eventLoopDelay = (this._loadBench.elapsed() - this.settings.sampleInterval); | ||
this.load.heapUsed = mem.heapUsed; | ||
this.load.rss = mem.rss; | ||
this._eventLoopUtilization = PerfHooks.performance.eventLoopUtilization(this._eventLoopUtilization); | ||
loopSample(); | ||
this.load.eventLoopDelay = (this._loadBench.elapsed() - this.settings.sampleInterval); | ||
this.load.eventLoopUtilization = this._eventLoopUtilization.utilization; | ||
this.load.heapUsed = mem.heapUsed; | ||
this.load.rss = mem.rss; | ||
loopSample(); | ||
}; | ||
this._eventLoopTimer = setTimeout(measure, this.settings.sampleInterval); | ||
}; | ||
this._eventLoopTimer = setTimeout(measure, this.settings.sampleInterval); | ||
}; | ||
loopSample(); | ||
} | ||
loopSample(); | ||
}; | ||
stop() { | ||
clearTimeout(this._eventLoopTimer); | ||
this._eventLoopTimer = null; | ||
} | ||
internals.Heavy.prototype.stop = function () { | ||
check() { | ||
clearTimeout(this._eventLoopTimer); | ||
this._eventLoopTimer = null; | ||
}; | ||
if (!this.settings.sampleInterval) { | ||
return; | ||
} | ||
Hoek.assert(this._eventLoopTimer, 'Cannot check load when sampler is not started'); | ||
internals.Heavy.prototype.check = function () { | ||
const elapsed = this._loadBench.elapsed(); | ||
const load = this.load; | ||
if (!this.settings.sampleInterval) { | ||
return; | ||
} | ||
if (elapsed > this.settings.sampleInterval) { | ||
this._eventLoopUtilization = PerfHooks.performance.eventLoopUtilization(this._eventLoopUtilization); | ||
Hoek.assert(this._eventLoopTimer, 'Cannot check load when sampler is not started'); | ||
load.eventLoopDelay = Math.max(load.eventLoopDelay, elapsed - this.settings.sampleInterval); | ||
load.eventLoopUtilization = this._eventLoopUtilization.utilization; | ||
} | ||
const elapsed = this._loadBench.elapsed(); | ||
const load = this.load; | ||
if (this.settings.maxEventLoopDelay && | ||
load.eventLoopDelay > this.settings.maxEventLoopDelay) { | ||
if (elapsed > this.settings.sampleInterval) { | ||
load.eventLoopDelay = Math.max(load.eventLoopDelay, elapsed - this.settings.sampleInterval); | ||
} | ||
throw Boom.serverUnavailable('Server under heavy load (event loop)', load); | ||
} | ||
if (this.settings.maxEventLoopDelay && | ||
load.eventLoopDelay > this.settings.maxEventLoopDelay) { | ||
if (this.settings.maxEventLoopUtilization && | ||
load.eventLoopUtilization > this.settings.maxEventLoopUtilization) { | ||
throw Boom.serverUnavailable('Server under heavy load (event loop)', load); | ||
} | ||
throw Boom.serverUnavailable('Server under heavy load (event loop utilization)', load); | ||
} | ||
if (this.settings.maxHeapUsedBytes && | ||
load.heapUsed > this.settings.maxHeapUsedBytes) { | ||
if (this.settings.maxHeapUsedBytes && | ||
load.heapUsed > this.settings.maxHeapUsedBytes) { | ||
throw Boom.serverUnavailable('Server under heavy load (heap)', load); | ||
} | ||
throw Boom.serverUnavailable('Server under heavy load (heap)', load); | ||
} | ||
if (this.settings.maxRssBytes && | ||
load.rss > this.settings.maxRssBytes) { | ||
if (this.settings.maxRssBytes && | ||
load.rss > this.settings.maxRssBytes) { | ||
throw Boom.serverUnavailable('Server under heavy load (rss)', load); | ||
throw Boom.serverUnavailable('Server under heavy load (rss)', load); | ||
} | ||
} | ||
}; |
@@ -1,3 +0,4 @@ | ||
Copyright (c) 2013-2020, Sideway Inc, and project contributors | ||
Copyright (c) 2013-2014, Walmart. | ||
Copyright (c) 2013-2022, Project contributors | ||
Copyright (c) 2013-2020, Sideway Inc | ||
Copyright (c) 2013-2014, Walmart. | ||
All rights reserved. | ||
@@ -4,0 +5,0 @@ |
{ | ||
"name": "@hapi/heavy", | ||
"description": "Measure process load", | ||
"version": "7.0.1", | ||
"version": "8.0.0", | ||
"repository": "git://github.com/hapijs/heavy", | ||
@@ -17,10 +17,16 @@ "main": "lib/index.js", | ||
], | ||
"eslintConfig": { | ||
"extends": [ | ||
"plugin:@hapi/module" | ||
] | ||
}, | ||
"dependencies": { | ||
"@hapi/boom": "9.x.x", | ||
"@hapi/hoek": "9.x.x", | ||
"@hapi/validate": "1.x.x" | ||
"@hapi/boom": "^10.0.0", | ||
"@hapi/hoek": "^10.0.0", | ||
"@hapi/validate": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"@hapi/code": "8.x.x", | ||
"@hapi/lab": "23.x.x" | ||
"@hapi/code": "^9.0.0", | ||
"@hapi/eslint-plugin": "*", | ||
"@hapi/lab": "^25.0.1" | ||
}, | ||
@@ -27,0 +33,0 @@ "scripts": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
7763
91
3
2
+ Added@hapi/boom@10.0.1(transitive)
+ Added@hapi/hoek@10.0.111.0.7(transitive)
+ Added@hapi/topo@6.0.2(transitive)
+ Added@hapi/validate@2.0.1(transitive)
- Removed@hapi/boom@9.1.4(transitive)
- Removed@hapi/hoek@9.3.0(transitive)
- Removed@hapi/topo@5.1.0(transitive)
- Removed@hapi/validate@1.1.3(transitive)
Updated@hapi/boom@^10.0.0
Updated@hapi/hoek@^10.0.0
Updated@hapi/validate@^2.0.0