@nxtedition/lib 19.8.21 → 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.
Files changed (2) hide show
  1. package/app.js +44 -24
  2. 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'
@@ -223,13 +223,25 @@ export function makeApp(appConfig, onTerminate) {
223
223
  const interval = 500
224
224
  const maxLag = 70
225
225
 
226
- let currentLag = 0
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
- setInterval(() => {
232
- currentLag = Math.max(0, histogram.mean / 1e6 - resolution)
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(() => {
244
+ let currentLag = Math.max(0, histogram.mean / 1e6 - resolution)
233
245
  if (Number.isNaN(currentLag)) {
234
246
  currentLag = null
235
247
  }
@@ -240,10 +252,26 @@ export function makeApp(appConfig, onTerminate) {
240
252
  } else if (currentLag > maxLag) {
241
253
  logger?.warn({ currentLag }, 'lag')
242
254
  }
255
+
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
+ }
243
263
  }, interval).unref()
244
264
 
245
- toobusy = () => currentLag > maxLag
246
- toobusy.lag = () => currentLag
265
+ destroyers.unshift(() => {
266
+ clearInterval(intervalHandle)
267
+ bc.postMessage({ threadId, currentLag: null })
268
+ })
269
+
270
+ toobusy = () => currentLag$.value > maxLag
271
+ toobusy.lag = () => currentLag$.value
272
+ toobusy.lag$ = currentLag$
273
+ toobusy.appLag$ = appLag$
274
+ toobusy.maxLag = maxLag
247
275
  }
248
276
 
249
277
  if (appConfig.couchdb || appConfig.couch) {
@@ -469,24 +497,16 @@ export function makeApp(appConfig, onTerminate) {
469
497
  rx.distinctUntilChanged(fp.isEqual),
470
498
  rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
471
499
  ),
472
- toobusy
473
- ? rxjs.timer(0, 1e3).pipe(
474
- rx.map(() =>
475
- toobusy.lag() > 1e3
476
- ? [
477
- {
478
- id: 'app:toobusy_lag',
479
- level: 40,
480
- code: 'NXT_LAG',
481
- msg: `lag: ${toobusy.lag()}`,
482
- },
483
- ]
484
- : [],
485
- ),
486
- rx.startWith([]),
487
- rx.distinctUntilChanged(fp.isEqual),
488
- )
489
- : rxjs.of({}),
500
+ toobusy?.allLag$.pipe(
501
+ rx.map((lag) => [
502
+ {
503
+ id: 'app:toobusy_lag',
504
+ level: lag > 1e3 ? 50 : lag > toobusy.maxLAge ? 40 : 30,
505
+ code: 'NXT_LAG',
506
+ msg: `Lag: ${lag.toFixed(2)} ms`,
507
+ },
508
+ ]),
509
+ ) ?? rxjs.of([]),
490
510
  couch
491
511
  ? rxjs.timer(0, 10e3).pipe(
492
512
  rx.exhaustMap(async () => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "19.8.21",
3
+ "version": "19.9.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",