@nxtedition/lib 23.9.18 → 23.10.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 (3) hide show
  1. package/app.js +75 -2
  2. package/couch.js +6 -1
  3. 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
@@ -479,6 +480,65 @@ export function makeApp(appConfig, onTerminate) {
479
480
  compiler = makeTemplateCompiler({ ds, logger, ...appConfig.compiler })
480
481
  }
481
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
+
482
542
  const monitorProviders = {}
483
543
 
484
544
  let stats$
@@ -568,9 +628,11 @@ export function makeApp(appConfig, onTerminate) {
568
628
  }
569
629
  }),
570
630
  ),
631
+ undici$,
571
632
  ),
572
- rx.map(([serviceStats, appStats]) => ({
633
+ rx.map(([serviceStats, appStats, undiciStats]) => ({
573
634
  ...appStats,
635
+ undici: undici$,
574
636
  [serviceName]: serviceStats,
575
637
  })),
576
638
  rx.retryWhen((err$) =>
@@ -636,6 +698,17 @@ export function makeApp(appConfig, onTerminate) {
636
698
  rx.distinctUntilChanged(fp.isEqual),
637
699
  rx.repeatWhen((complete$) => complete$.pipe(rx.delay(10e3))),
638
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
+ ),
639
712
  toobusy?.appLag$.pipe(
640
713
  rx.map((lag) =>
641
714
  lag == null
@@ -1015,7 +1088,7 @@ export function makeApp(appConfig, onTerminate) {
1015
1088
  utilsBC.unref()
1016
1089
 
1017
1090
  function writeHeapSnapshot() {
1018
- 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`
1019
1092
  v8.writeHeapSnapshot(snapshotPath)
1020
1093
  logger.info({ snapshotPath }, 'heap snapshot')
1021
1094
  }
package/couch.js CHANGED
@@ -1052,6 +1052,11 @@ const defaultDispatcher = new Agent({
1052
1052
  connections: 8,
1053
1053
  connectTimeout: 2e3,
1054
1054
  })
1055
+ const blockingDispatcher = new Agent({
1056
+ pipelining: 1,
1057
+ connections: 1024,
1058
+ connectTimeout: 2e3,
1059
+ })
1055
1060
 
1056
1061
  export function request(url, opts) {
1057
1062
  if (typeof url === 'string') {
@@ -1101,7 +1106,7 @@ export function request(url, opts) {
1101
1106
  opts.body != null && typeof opts.body === 'object' ? JSON.stringify(opts.body) : opts.body,
1102
1107
  }
1103
1108
 
1104
- const dispatcher = opts.dispatcher ?? defaultDispatcher
1109
+ const dispatcher = opts.dispatcher ?? (ureq.blocking ? blockingDispatcher : defaultDispatcher)
1105
1110
  const signal = opts.signal
1106
1111
 
1107
1112
  if (opts.stream) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.9.18",
3
+ "version": "23.10.1",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",