Skip to content

Commit

Permalink
✨ Add Alchemy#setMaxEventLoopLag(max_lag) method
Browse files Browse the repository at this point in the history
  • Loading branch information
skerit committed Oct 16, 2023
1 parent 22fae4e commit f3f2d17
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 1.3.18 (WIP)

* Add `Alchemy#setMaxEventLoopLag(max_lag)` method

## 1.3.17 (2023-10-15)

* Fix `Alchemy#findModule()` assuming some package is a module too fast
Expand Down
34 changes: 29 additions & 5 deletions lib/init/alchemy.js
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ Alchemy.setMethod(function isTooBusyForRequests() {
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.1
* @version 1.3.2
* @version 1.3.18
*
* @return {Boolean}
*/
Expand All @@ -348,11 +348,11 @@ Alchemy.setMethod(function isTooBusyForAjax() {
return false;
}

let lag = this.lagInMs();

let max_event_loop_lag = alchemy.settings.max_event_loop_lag || alchemy.settings.toobusy;
let max_event_loop_lag = alchemy.settings.max_event_loop_lag ?? alchemy.settings.toobusy;

if (max_event_loop_lag) {
let lag = this.lagInMs();

if (lag > (max_event_loop_lag * 3)) {
return true;
}
Expand All @@ -361,6 +361,28 @@ Alchemy.setMethod(function isTooBusyForAjax() {
return false;
});

/**
* Set the maximum event loop lag before the server is considered overloaded
*
* @author Jelle De Loecker <jelle@elevenways.be>
* @since 1.3.18
* @version 1.3.18
*
* @param {Number} max_lag
*/
Alchemy.setMethod(function setMaxEventLoopLag(max_lag) {

if (typeof max_lag != 'number' || !isFinite(max_lag)) {
max_lag = null;
}

if (max_lag == null) {
max_lag = 70;
}

this.settings.max_event_loop_lag = max_lag;
});

/**
* Called after the server has started
*
Expand Down Expand Up @@ -588,7 +610,7 @@ Alchemy.setMethod(function printLog(level, args, options) {
*
* @author Jelle De Loecker <jelle@develry.be>
* @since 0.4.0
* @version 1.3.3
* @version 1.3.18
*/
Alchemy.setMethod(function loadSettings() {

Expand Down Expand Up @@ -780,6 +802,8 @@ Alchemy.setMethod(function loadSettings() {
this.doPreload();
}

this.setMaxEventLoopLag(settings.max_event_loop_lag);

// Set the debug value
global.DEBUG = settings.debug;
});
Expand Down
7 changes: 0 additions & 7 deletions lib/init/requirements.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,6 @@ alchemy.hawkejs = Classes.Hawkejs.Hawkejs.getInstance();
*/
alchemy.toobusy = alchemy.use('toobusy-js', 'toobusy');

let max_event_loop_lag = alchemy.settings.max_event_loop_lag || alchemy.settings.toobusy;

// If the config is a number, use that as the lag threshold
if (typeof max_event_loop_lag === 'number') {
alchemy.toobusy.maxLag(max_event_loop_lag);
}

/**
* Load Sputnik, the stage-based launcher
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "alchemymvc",
"description": "MVC framework for Node.js",
"version": "1.3.17",
"version": "1.3.18-alpha",
"author": "Jelle De Loecker <jelle@elevenways.be>",
"keywords": [
"alchemy",
Expand Down

0 comments on commit f3f2d17

Please sign in to comment.