Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@hapi/heavy

Package Overview
Dependencies
Maintainers
6
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@hapi/heavy - npm Package Compare versions

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": {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc