@nxtedition/lib 23.6.5 → 23.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/under-pressure.js +30 -20
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.6.5",
3
+ "version": "23.6.6",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/under-pressure.js CHANGED
@@ -15,6 +15,7 @@ export default function (opts = {}) {
15
15
  const maxRssBytes = opts.maxRssBytes || 0
16
16
  const healthCheck = opts.healthCheck || false
17
17
  const maxEventLoopUtilization = opts.maxEventLoopUtilization || 0
18
+ const onPressure = opts.onPressure || (() => {})
18
19
 
19
20
  const checkMaxEventLoopDelay = maxEventLoopDelay > 0
20
21
  const checkMaxHeapUsedBytes = maxHeapUsedBytes > 0
@@ -34,9 +35,6 @@ export default function (opts = {}) {
34
35
  elu = performance.eventLoopUtilization()
35
36
  }
36
37
 
37
- const timer = setTimeout(beginMemoryUsageUpdate, sampleInterval)
38
- timer.unref()
39
-
40
38
  if (
41
39
  checkMaxEventLoopUtilization === false &&
42
40
  checkMaxEventLoopDelay === false &&
@@ -61,34 +59,46 @@ export default function (opts = {}) {
61
59
  }
62
60
  }
63
61
 
64
- function beginMemoryUsageUpdate() {
65
- updateMemoryUsage()
66
- timer.refresh()
67
- }
62
+ const timer = setTimeout(update, sampleInterval).unref()
68
63
 
69
- function updateMemoryUsage() {
64
+ function update() {
70
65
  const mem = process.memoryUsage()
71
66
  heapUsedBytes = mem.heapUsed
72
67
  rssBytes = mem.rss
73
68
  updateEventLoopDelay()
74
69
  updateEventLoopUtilization()
70
+
71
+ if (isUnderPressure()) {
72
+ onPressure({
73
+ eventLoopDelay,
74
+ heapUsedBytes,
75
+ rssBytes,
76
+ eventLoopUtilized,
77
+ })
78
+ }
79
+
80
+ timer.refresh()
75
81
  }
76
82
 
77
- return {
78
- get isUnderPressure() {
79
- if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
80
- return true
81
- }
83
+ function isUnderPressure() {
84
+ if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
85
+ return true
86
+ }
82
87
 
83
- if (checkMaxHeapUsedBytes && heapUsedBytes > maxHeapUsedBytes) {
84
- return true
85
- }
88
+ if (checkMaxHeapUsedBytes && heapUsedBytes > maxHeapUsedBytes) {
89
+ return true
90
+ }
86
91
 
87
- if (checkMaxRssBytes && rssBytes > maxRssBytes) {
88
- return true
89
- }
92
+ if (checkMaxRssBytes && rssBytes > maxRssBytes) {
93
+ return true
94
+ }
95
+
96
+ return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
97
+ }
90
98
 
91
- return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
99
+ return {
100
+ get isUnderPressure() {
101
+ return isUnderPressure()
92
102
  },
93
103
  get stats() {
94
104
  return {