@nxtedition/lib 23.9.17 → 23.10.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/app.js +85 -10
- package/package.json +1 -1
package/app.js
CHANGED
|
@@ -37,6 +37,7 @@ import { LRUCache } from 'lru-cache'
|
|
|
37
37
|
import xuid from 'xuid'
|
|
38
38
|
import { isTimeBetween } from './time.js'
|
|
39
39
|
import makeUnderPressure from './under-pressure.js'
|
|
40
|
+
import undici from '@nxtedition/undici'
|
|
40
41
|
|
|
41
42
|
export function makeApp(appConfig, onTerminate) {
|
|
42
43
|
let ds
|
|
@@ -346,13 +347,15 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
346
347
|
let underPressure
|
|
347
348
|
if (appConfig.underPressure) {
|
|
348
349
|
underPressure = makeUnderPressure(appConfig.underPressure)
|
|
349
|
-
underPressure
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
350
|
+
if (underPressure) {
|
|
351
|
+
underPressure.pressure$.subscribe((pressure) => {
|
|
352
|
+
if (pressure) {
|
|
353
|
+
logger?.warn({ pressure }, 'pressure triggered')
|
|
354
|
+
} else {
|
|
355
|
+
logger?.debug('pressure cleared')
|
|
356
|
+
}
|
|
357
|
+
})
|
|
358
|
+
}
|
|
356
359
|
}
|
|
357
360
|
|
|
358
361
|
if (appConfig.couchdb || appConfig.couch) {
|
|
@@ -477,6 +480,65 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
477
480
|
compiler = makeTemplateCompiler({ ds, logger, ...appConfig.compiler })
|
|
478
481
|
}
|
|
479
482
|
|
|
483
|
+
const undiciDefaults = {
|
|
484
|
+
connected: 0,
|
|
485
|
+
disconnected: 0,
|
|
486
|
+
connections: 0,
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
const undici$ = new rxjs.BehaviorSubject(undiciDefaults)
|
|
490
|
+
{
|
|
491
|
+
function onConnect(origin, targets) {
|
|
492
|
+
const stats = { ...undici$.value }
|
|
493
|
+
stats.connected++
|
|
494
|
+
stats.connections = stats.connected - stats.disconnected
|
|
495
|
+
undici$.next(stats)
|
|
496
|
+
logger.debug({ url: origin, count: stats.connected }, 'upstream connect')
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
function onDisconnect(origin, targets, err) {
|
|
500
|
+
const stats = { ...undici$.value }
|
|
501
|
+
stats.connected--
|
|
502
|
+
stats.connections = stats.connected - stats.disconnected
|
|
503
|
+
undici$.next(stats)
|
|
504
|
+
logger.debug({ url: origin, count: stats.connected }, 'upstream disconnect')
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
let currentDispatcher
|
|
508
|
+
function maybeRegisterUndiciStats() {
|
|
509
|
+
const nextDispatcher = undici.getGlobalDispatcher()
|
|
510
|
+
|
|
511
|
+
if (nextDispatcher === currentDispatcher) {
|
|
512
|
+
return
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
if (currentDispatcher) {
|
|
516
|
+
currentDispatcher.off('connect', onConnect).off('disconnect', onDisconnect)
|
|
517
|
+
currentDispatcher = null
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
undici$.next(undiciDefaults)
|
|
521
|
+
|
|
522
|
+
nextDispatcher.on('connect', onConnect).on('disconnect', onDisconnect)
|
|
523
|
+
|
|
524
|
+
currentDispatcher = nextDispatcher
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
let undiciInterval = setInterval(() => maybeRegisterUndiciStats(), 10e3).unref()
|
|
528
|
+
|
|
529
|
+
destroyers.push(() => {
|
|
530
|
+
if (undiciInterval) {
|
|
531
|
+
clearInterval(undiciInterval)
|
|
532
|
+
undiciInterval = null
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
if (currentDispatcher) {
|
|
536
|
+
currentDispatcher.off('connect', onConnect).off('disconnect', onDisconnect)
|
|
537
|
+
currentDispatcher = null
|
|
538
|
+
}
|
|
539
|
+
})
|
|
540
|
+
}
|
|
541
|
+
|
|
480
542
|
const monitorProviders = {}
|
|
481
543
|
|
|
482
544
|
let stats$
|
|
@@ -566,9 +628,11 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
566
628
|
}
|
|
567
629
|
}),
|
|
568
630
|
),
|
|
631
|
+
undici$,
|
|
569
632
|
),
|
|
570
|
-
rx.map(([serviceStats, appStats]) => ({
|
|
633
|
+
rx.map(([serviceStats, appStats, undiciStats]) => ({
|
|
571
634
|
...appStats,
|
|
635
|
+
undici: undici$,
|
|
572
636
|
[serviceName]: serviceStats,
|
|
573
637
|
})),
|
|
574
638
|
rx.retryWhen((err$) =>
|
|
@@ -634,6 +698,17 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
634
698
|
rx.distinctUntilChanged(fp.isEqual),
|
|
635
699
|
rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
|
|
636
700
|
),
|
|
701
|
+
undici$.pipe(
|
|
702
|
+
rx.map((stats) => {
|
|
703
|
+
return [
|
|
704
|
+
{
|
|
705
|
+
id: 'app:undici_upstream_connections',
|
|
706
|
+
level: stats.connections > 8192 ? 50 : stats.connections > 4096 ? 40 : 30,
|
|
707
|
+
msg: `Undici: ${stats.connections} upstream connected`,
|
|
708
|
+
},
|
|
709
|
+
]
|
|
710
|
+
}),
|
|
711
|
+
),
|
|
637
712
|
toobusy?.appLag$.pipe(
|
|
638
713
|
rx.map((lag) =>
|
|
639
714
|
lag == null
|
|
@@ -648,7 +723,7 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
648
723
|
],
|
|
649
724
|
),
|
|
650
725
|
) ?? rxjs.of([]),
|
|
651
|
-
underPressure?.pressure
|
|
726
|
+
underPressure?.pressure$?.pipe(
|
|
652
727
|
rx.map((pressure) =>
|
|
653
728
|
pressure == null
|
|
654
729
|
? []
|
|
@@ -1013,7 +1088,7 @@ export function makeApp(appConfig, onTerminate) {
|
|
|
1013
1088
|
utilsBC.unref()
|
|
1014
1089
|
|
|
1015
1090
|
function writeHeapSnapshot() {
|
|
1016
|
-
const snapshotPath = `${os.tmpdir()}/heap-${Date.now()}-${serviceName}-${serviceModule}-${serviceInstanceId}-${serviceWorkerId}.heapsnapshot`
|
|
1091
|
+
const snapshotPath = `${os.tmpdir()}/heap-${Date.now()}-${serviceName}-${serviceModule}-${serviceInstanceId}-${serviceWorkerId}-${threadId}.heapsnapshot`
|
|
1017
1092
|
v8.writeHeapSnapshot(snapshotPath)
|
|
1018
1093
|
logger.info({ snapshotPath }, 'heap snapshot')
|
|
1019
1094
|
}
|