@nxtedition/lib 19.9.0 → 19.9.1
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 +27 -3
- package/package.json +1 -1
package/app.js
CHANGED
|
@@ -6,7 +6,7 @@ import stream from 'node:stream'
|
|
|
6
6
|
import { Buffer } from 'node:buffer'
|
|
7
7
|
import { getDockerSecretsSync } from './docker-secrets.js'
|
|
8
8
|
import fp from 'lodash/fp.js'
|
|
9
|
-
import { isMainThread, parentPort, threadId } from 'node:worker_threads'
|
|
9
|
+
import { isMainThread, parentPort, threadId, BroadcastChannel } from 'node:worker_threads'
|
|
10
10
|
import deepstream from '@nxtedition/deepstream.io-client-js'
|
|
11
11
|
import { createLogger } from './logger.js'
|
|
12
12
|
import nconf from 'nconf'
|
|
@@ -224,11 +224,23 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
224
224
|
const maxLag = 70
|
|
225
225
|
|
|
226
226
|
const currentLag$ = new rxjs.BehaviorSubject(0)
|
|
227
|
+
const appLag$ = new rxjs.BehaviorSubject(0)
|
|
227
228
|
|
|
228
229
|
const histogram = monitorEventLoopDelay({ resolution })
|
|
229
230
|
histogram.enable()
|
|
230
231
|
|
|
231
|
-
|
|
232
|
+
const bc = new BroadcastChannel('nxt:lag')
|
|
233
|
+
|
|
234
|
+
const lagMap = new Map()
|
|
235
|
+
bc.onmessage = ({ data }) => {
|
|
236
|
+
if (data.currentLag) {
|
|
237
|
+
lagMap.set(data.threadId, data.currentLag)
|
|
238
|
+
} else {
|
|
239
|
+
lagMap.delete(data.threadId)
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const intervalHandle = setInterval(() => {
|
|
232
244
|
let currentLag = Math.max(0, histogram.mean / 1e6 - resolution)
|
|
233
245
|
if (Number.isNaN(currentLag)) {
|
|
234
246
|
currentLag = null
|
|
@@ -242,11 +254,23 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
242
254
|
}
|
|
243
255
|
|
|
244
256
|
currentLag$.next(currentLag)
|
|
257
|
+
bc.postMessage({ threadId, currentLag })
|
|
258
|
+
|
|
259
|
+
let appLag = currentLag$.value
|
|
260
|
+
for (const lag of lagMap.values()) {
|
|
261
|
+
appLag = Math.max(appLag, lag)
|
|
262
|
+
}
|
|
245
263
|
}, interval).unref()
|
|
246
264
|
|
|
265
|
+
destroyers.unshift(() => {
|
|
266
|
+
clearInterval(intervalHandle)
|
|
267
|
+
bc.postMessage({ threadId, currentLag: null })
|
|
268
|
+
})
|
|
269
|
+
|
|
247
270
|
toobusy = () => currentLag$.value > maxLag
|
|
248
271
|
toobusy.lag = () => currentLag$.value
|
|
249
272
|
toobusy.lag$ = currentLag$
|
|
273
|
+
toobusy.appLag$ = appLag$
|
|
250
274
|
toobusy.maxLag = maxLag
|
|
251
275
|
}
|
|
252
276
|
|
|
@@ -473,7 +497,7 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
473
497
|
rx.distinctUntilChanged(fp.isEqual),
|
|
474
498
|
rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
|
|
475
499
|
),
|
|
476
|
-
toobusy?.
|
|
500
|
+
toobusy?.allLag$.pipe(
|
|
477
501
|
rx.map((lag) => [
|
|
478
502
|
{
|
|
479
503
|
id: 'app:toobusy_lag',
|