@nxtedition/lib 23.9.9 → 23.9.11
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/app.js +1 -1
- package/cache.js +19 -2
- package/package.json +3 -2
- package/under-pressure.js +12 -15
- package/yield.js +23 -0
package/app.js
CHANGED
|
@@ -314,7 +314,7 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
314
314
|
setInterval(() => {
|
|
315
315
|
let currentLag = Math.max(0, histogram.mean / 1e6 - resolution)
|
|
316
316
|
if (Number.isNaN(currentLag)) {
|
|
317
|
-
currentLag =
|
|
317
|
+
currentLag = Infinity
|
|
318
318
|
}
|
|
319
319
|
histogram.reset()
|
|
320
320
|
|
package/cache.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { LRUCache } from 'lru-cache'
|
|
2
2
|
|
|
3
3
|
export class AsyncCache {
|
|
4
|
+
/** @type LRUCache<string, { expire: Number, value: any } **/
|
|
4
5
|
#lru
|
|
5
6
|
#valueSelector
|
|
6
7
|
#keySelector
|
|
@@ -77,12 +78,11 @@ export class AsyncCache {
|
|
|
77
78
|
if (!promise) {
|
|
78
79
|
promise = this.#valueSelector(...args).then(
|
|
79
80
|
(value) => {
|
|
80
|
-
this.#dedupe.delete(key)
|
|
81
81
|
this.set(key, value)
|
|
82
82
|
return [null, value]
|
|
83
83
|
},
|
|
84
84
|
(err) => {
|
|
85
|
-
this
|
|
85
|
+
this.delete(key)
|
|
86
86
|
return [err, null]
|
|
87
87
|
},
|
|
88
88
|
)
|
|
@@ -103,6 +103,10 @@ export class AsyncCache {
|
|
|
103
103
|
return value
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
+
/**
|
|
107
|
+
* @param {string} key
|
|
108
|
+
* @param {any} value
|
|
109
|
+
*/
|
|
106
110
|
set(key, value) {
|
|
107
111
|
if (typeof key !== 'string' || key.length === 0) {
|
|
108
112
|
throw new TypeError('key must be a non-empty string')
|
|
@@ -114,6 +118,19 @@ export class AsyncCache {
|
|
|
114
118
|
}
|
|
115
119
|
|
|
116
120
|
this.#lru.set(key, cached)
|
|
121
|
+
this.#dedupe.delete(key)
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* @param {string} key
|
|
126
|
+
*/
|
|
127
|
+
delete(key) {
|
|
128
|
+
if (typeof key !== 'string' || key.length === 0) {
|
|
129
|
+
throw new TypeError('key must be a non-empty string')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this.#lru.delete(key)
|
|
133
|
+
this.#dedupe.delete(key)
|
|
117
134
|
}
|
|
118
135
|
|
|
119
136
|
clear() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/lib",
|
|
3
|
-
"version": "23.9.
|
|
3
|
+
"version": "23.9.11",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "Robert Nagy <robert.nagy@boffins.se>",
|
|
6
6
|
"type": "module",
|
|
@@ -44,7 +44,8 @@
|
|
|
44
44
|
"transcript.js",
|
|
45
45
|
"docker-secrets.js",
|
|
46
46
|
"wordwrap.js",
|
|
47
|
-
"under-pressure.js"
|
|
47
|
+
"under-pressure.js",
|
|
48
|
+
"yield.js"
|
|
48
49
|
],
|
|
49
50
|
"scripts": {
|
|
50
51
|
"prepublishOnly": "pinst --disable",
|
package/under-pressure.js
CHANGED
|
@@ -16,10 +16,6 @@ export default function (opts = {}) {
|
|
|
16
16
|
const maxRssBytes = opts.maxRssBytes || 0
|
|
17
17
|
const maxEventLoopUtilization = opts.maxEventLoopUtilization || 0
|
|
18
18
|
|
|
19
|
-
const checkMaxEventLoopDelay = maxEventLoopDelay > 0
|
|
20
|
-
const checkMaxHeapUsedBytes = maxHeapUsedBytes > 0
|
|
21
|
-
const checkMaxRssBytes = maxRssBytes > 0
|
|
22
|
-
const checkMaxEventLoopUtilization = maxEventLoopUtilization > 0
|
|
23
19
|
const pressure$ = new rxjs.BehaviorSubject(null)
|
|
24
20
|
|
|
25
21
|
let heapUsedBytes = 0
|
|
@@ -35,18 +31,15 @@ export default function (opts = {}) {
|
|
|
35
31
|
elu = performance.eventLoopUtilization()
|
|
36
32
|
}
|
|
37
33
|
|
|
38
|
-
if (
|
|
39
|
-
checkMaxEventLoopUtilization === false &&
|
|
40
|
-
checkMaxEventLoopDelay === false &&
|
|
41
|
-
checkMaxHeapUsedBytes === false &&
|
|
42
|
-
checkMaxRssBytes === false
|
|
43
|
-
) {
|
|
34
|
+
if (!maxEventLoopDelay && !maxHeapUsedBytes && !maxRssBytes && !maxEventLoopUtilization) {
|
|
44
35
|
return
|
|
45
36
|
}
|
|
46
37
|
|
|
47
38
|
function updateEventLoopDelay() {
|
|
48
39
|
eventLoopDelay = Math.max(0, histogram.mean / 1e6 - resolution)
|
|
49
|
-
if (Number.isNaN(eventLoopDelay))
|
|
40
|
+
if (Number.isNaN(eventLoopDelay)) {
|
|
41
|
+
eventLoopDelay = Infinity
|
|
42
|
+
}
|
|
50
43
|
histogram.reset()
|
|
51
44
|
}
|
|
52
45
|
|
|
@@ -71,19 +64,23 @@ export default function (opts = {}) {
|
|
|
71
64
|
}
|
|
72
65
|
|
|
73
66
|
function isUnderPressure() {
|
|
74
|
-
if (
|
|
67
|
+
if (maxEventLoopDelay && eventLoopDelay > maxEventLoopDelay) {
|
|
68
|
+
return true
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (maxHeapUsedBytes && heapUsedBytes > maxHeapUsedBytes) {
|
|
75
72
|
return true
|
|
76
73
|
}
|
|
77
74
|
|
|
78
|
-
if (
|
|
75
|
+
if (maxRssBytes && rssBytes > maxRssBytes) {
|
|
79
76
|
return true
|
|
80
77
|
}
|
|
81
78
|
|
|
82
|
-
if (
|
|
79
|
+
if (maxEventLoopUtilization && eventLoopUtilized > maxEventLoopUtilization) {
|
|
83
80
|
return true
|
|
84
81
|
}
|
|
85
82
|
|
|
86
|
-
return
|
|
83
|
+
return false
|
|
87
84
|
}
|
|
88
85
|
|
|
89
86
|
function getPressure() {
|
package/yield.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
let yieldTime = performance.now()
|
|
2
|
+
|
|
3
|
+
export function maybeYield(opts) {
|
|
4
|
+
const timeout = opts?.timeout ?? 50
|
|
5
|
+
if (performance.now() - yieldTime > timeout) {
|
|
6
|
+
return doYield(opts)
|
|
7
|
+
}
|
|
8
|
+
opts?.signal?.throwIfAborted()
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function doYield(opts) {
|
|
12
|
+
await new Promise((resolve) => setImmediate(resolve))
|
|
13
|
+
resetYield(opts)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function resetYield(opts) {
|
|
17
|
+
yieldTime = performance.now()
|
|
18
|
+
opts?.signal?.throwIfAborted()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
setInterval(() => {
|
|
22
|
+
yieldTime = performance.now()
|
|
23
|
+
}, 500).unref()
|