@nxtedition/lib 23.6.5 → 23.6.7

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 +38 -33
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.6.5",
3
+ "version": "23.6.7",
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,47 +59,54 @@ 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(getPressure())
73
+ }
74
+
75
+ timer.refresh()
75
76
  }
76
77
 
77
- return {
78
- get isUnderPressure() {
79
- if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
80
- return true
81
- }
78
+ function isUnderPressure() {
79
+ if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
80
+ return true
81
+ }
82
82
 
83
- if (checkMaxHeapUsedBytes && heapUsedBytes > maxHeapUsedBytes) {
84
- return true
85
- }
83
+ if (checkMaxHeapUsedBytes && heapUsedBytes > maxHeapUsedBytes) {
84
+ return true
85
+ }
86
86
 
87
- if (checkMaxRssBytes && rssBytes > maxRssBytes) {
88
- return true
89
- }
87
+ if (checkMaxRssBytes && rssBytes > maxRssBytes) {
88
+ return true
89
+ }
90
90
 
91
- return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
92
- },
93
- get stats() {
94
- return {
95
- eventLoopDelay,
96
- maxEventLoopDelay,
97
- rssBytes,
98
- maxRssBytes,
99
- heapUsedBytes,
100
- maxHeapUsedBytes,
101
- eventLoopUtilized,
102
- maxEventLoopUtilization,
103
- }
104
- },
91
+ return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
92
+ }
93
+
94
+ function getPressure() {
95
+ return {
96
+ eventLoopDelay,
97
+ maxEventLoopDelay,
98
+ rssBytes,
99
+ maxRssBytes,
100
+ heapUsedBytes,
101
+ maxHeapUsedBytes,
102
+ eventLoopUtilized,
103
+ maxEventLoopUtilization,
104
+ }
105
+ }
106
+
107
+ return {
108
+ isUnderPressure,
109
+ getPressure,
105
110
  [Symbol.dispose]() {
106
111
  clearTimeout(timer)
107
112
  },