@nxtedition/lib 19.9.0 → 19.9.2
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 +29 -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,25 @@ 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
|
+
}
|
|
263
|
+
appLag$.next(appLag)
|
|
245
264
|
}, interval).unref()
|
|
246
265
|
|
|
266
|
+
destroyers.unshift(() => {
|
|
267
|
+
clearInterval(intervalHandle)
|
|
268
|
+
bc.postMessage({ threadId, currentLag: null })
|
|
269
|
+
})
|
|
270
|
+
|
|
247
271
|
toobusy = () => currentLag$.value > maxLag
|
|
248
272
|
toobusy.lag = () => currentLag$.value
|
|
249
273
|
toobusy.lag$ = currentLag$
|
|
274
|
+
toobusy.appLag = () => appLag$.value
|
|
275
|
+
toobusy.appLag$ = appLag$
|
|
250
276
|
toobusy.maxLag = maxLag
|
|
251
277
|
}
|
|
252
278
|
|
|
@@ -473,7 +499,7 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
473
499
|
rx.distinctUntilChanged(fp.isEqual),
|
|
474
500
|
rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
|
|
475
501
|
),
|
|
476
|
-
toobusy?.
|
|
502
|
+
toobusy?.appLag$.pipe(
|
|
477
503
|
rx.map((lag) => [
|
|
478
504
|
{
|
|
479
505
|
id: 'app:toobusy_lag',
|