@nxtedition/lib 23.6.4 → 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 +38 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.6.4",
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,13 +15,14 @@ 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
21
22
  const checkMaxRssBytes = maxRssBytes > 0
22
23
  const checkMaxEventLoopUtilization = maxEventLoopUtilization > 0
23
24
 
24
- let heapUsed = 0
25
+ let heapUsedBytes = 0
25
26
  let rssBytes = 0
26
27
  let eventLoopDelay = 0
27
28
  let elu
@@ -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,41 +59,57 @@ 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
- heapUsed = mem.heapUsed
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
- 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 && heapUsed > 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
+ }
90
95
 
91
- return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
96
+ return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
97
+ }
98
+
99
+ return {
100
+ get isUnderPressure() {
101
+ return isUnderPressure()
92
102
  },
93
- memoryUsage() {
103
+ get stats() {
94
104
  return {
95
105
  eventLoopDelay,
106
+ maxEventLoopDelay,
96
107
  rssBytes,
97
- heapUsed,
108
+ maxRssBytes,
109
+ heapUsedBytes,
110
+ maxHeapUsedBytes,
98
111
  eventLoopUtilized,
112
+ maxEventLoopUtilization,
99
113
  }
100
114
  },
101
115
  [Symbol.dispose]() {