@nxtedition/lib 23.5.13 → 23.6.0
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.
- package/couch.js +4 -0
- package/package.json +3 -2
- package/under-pressure.js +105 -0
package/couch.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "23.
|
|
3
|
+
"version": "23.6.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -43,7 +43,8 @@
|
|
|
43
43
|
"timeline.js",
|
|
44
44
|
"transcript.js",
|
|
45
45
|
"docker-secrets.js",
|
|
46
|
-
"wordwrap.js"
|
|
46
|
+
"wordwrap.js",
|
|
47
|
+
"under-pressure.js"
|
|
47
48
|
],
|
|
48
49
|
"scripts": {
|
|
49
50
|
"prepublishOnly": "pinst --disable",
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
// Based on: https://github.com/fastify/under-pressure/blob/main/index.js
|
|
2
|
+
import { monitorEventLoopDelay } from 'node:perf_hooks'
|
|
3
|
+
|
|
4
|
+
function getSampleInterval(value, eventLoopResolution) {
|
|
5
|
+
const sampleInterval = value || 1000
|
|
6
|
+
|
|
7
|
+
return Math.max(eventLoopResolution, sampleInterval)
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export default function (opts = {}) {
|
|
11
|
+
const resolution = 10
|
|
12
|
+
const sampleInterval = getSampleInterval(opts.sampleInterval, resolution)
|
|
13
|
+
const maxEventLoopDelay = opts.maxEventLoopDelay || 0
|
|
14
|
+
const maxHeapUsedBytes = opts.maxHeapUsedBytes || 0
|
|
15
|
+
const maxRssBytes = opts.maxRssBytes || 0
|
|
16
|
+
const healthCheck = opts.healthCheck || false
|
|
17
|
+
const maxEventLoopUtilization = opts.maxEventLoopUtilization || 0
|
|
18
|
+
|
|
19
|
+
const checkMaxEventLoopDelay = maxEventLoopDelay > 0
|
|
20
|
+
const checkMaxHeapUsedBytes = maxHeapUsedBytes > 0
|
|
21
|
+
const checkMaxRssBytes = maxRssBytes > 0
|
|
22
|
+
const checkMaxEventLoopUtilization = maxEventLoopUtilization > 0
|
|
23
|
+
|
|
24
|
+
let heapUsed = 0
|
|
25
|
+
let rssBytes = 0
|
|
26
|
+
let eventLoopDelay = 0
|
|
27
|
+
let elu
|
|
28
|
+
let eventLoopUtilized = 0
|
|
29
|
+
|
|
30
|
+
const histogram = monitorEventLoopDelay({ resolution })
|
|
31
|
+
histogram.enable()
|
|
32
|
+
|
|
33
|
+
if (performance.eventLoopUtilization) {
|
|
34
|
+
elu = performance.eventLoopUtilization()
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const timer = setTimeout(beginMemoryUsageUpdate, sampleInterval)
|
|
38
|
+
timer.unref()
|
|
39
|
+
|
|
40
|
+
if (
|
|
41
|
+
checkMaxEventLoopUtilization === false &&
|
|
42
|
+
checkMaxEventLoopDelay === false &&
|
|
43
|
+
checkMaxHeapUsedBytes === false &&
|
|
44
|
+
checkMaxRssBytes === false &&
|
|
45
|
+
healthCheck === false
|
|
46
|
+
) {
|
|
47
|
+
return
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function updateEventLoopDelay() {
|
|
51
|
+
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
|
|
52
|
+
if (Number.isNaN(eventLoopDelay)) eventLoopDelay = Infinity
|
|
53
|
+
histogram.reset()
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function updateEventLoopUtilization() {
|
|
57
|
+
if (elu) {
|
|
58
|
+
eventLoopUtilized = performance.eventLoopUtilization(elu).utilization
|
|
59
|
+
} else {
|
|
60
|
+
eventLoopUtilized = 0
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function beginMemoryUsageUpdate() {
|
|
65
|
+
updateMemoryUsage()
|
|
66
|
+
timer.refresh()
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function updateMemoryUsage() {
|
|
70
|
+
const mem = process.memoryUsage()
|
|
71
|
+
heapUsed = mem.heapUsed
|
|
72
|
+
rssBytes = mem.rss
|
|
73
|
+
updateEventLoopDelay()
|
|
74
|
+
updateEventLoopUtilization()
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return {
|
|
78
|
+
isUnderPressure() {
|
|
79
|
+
if (checkMaxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
|
|
80
|
+
return true
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (checkMaxHeapUsedBytes && heapUsed > maxHeapUsedBytes) {
|
|
84
|
+
return true
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (checkMaxRssBytes && rssBytes > maxRssBytes) {
|
|
88
|
+
return true
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return checkMaxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization
|
|
92
|
+
},
|
|
93
|
+
memoryUsage() {
|
|
94
|
+
return {
|
|
95
|
+
eventLoopDelay,
|
|
96
|
+
rssBytes,
|
|
97
|
+
heapUsed,
|
|
98
|
+
eventLoopUtilized,
|
|
99
|
+
}
|
|
100
|
+
},
|
|
101
|
+
[Symbol.dispose]() {
|
|
102
|
+
clearTimeout(timer)
|
|
103
|
+
},
|
|
104
|
+
}
|
|
105
|
+
}
|