@nxtedition/lib 23.6.7 → 23.6.9

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 CHANGED
@@ -36,6 +36,7 @@ import { monitorEventLoopDelay } from 'node:perf_hooks'
36
36
  import { LRUCache } from 'lru-cache'
37
37
  import xuid from 'xuid'
38
38
  import { isTimeBetween } from './time.js'
39
+ import makeUnderPressure from './under-pressure.js'
39
40
 
40
41
  export function makeApp(appConfig, onTerminate) {
41
42
  let ds
@@ -342,6 +343,18 @@ export function makeApp(appConfig, onTerminate) {
342
343
  toobusy.maxLag = maxLag
343
344
  }
344
345
 
346
+ let underPressure
347
+ if (appConfig.underPressure) {
348
+ underPressure = makeUnderPressure(appConfig.underPressure)
349
+ underPressure.pressure$.subscribe((pressure) => {
350
+ if (pressure) {
351
+ logger?.warn({ pressure }, 'pressure triggered')
352
+ } else {
353
+ logger?.debug('pressure cleared')
354
+ }
355
+ })
356
+ }
357
+
345
358
  if (appConfig.couchdb || appConfig.couch) {
346
359
  const couchConfig = fp.mergeAll(
347
360
  [
@@ -536,6 +549,7 @@ export function makeApp(appConfig, onTerminate) {
536
549
  couch: couch?.stats,
537
550
  lag: toobusy?.lag(),
538
551
  version: process.version,
552
+ underPressure: underPressure?.isUnderPressure() ? underPressure.getPressure() : null,
539
553
  cpu,
540
554
  memory,
541
555
  totalMemory,
@@ -627,6 +641,20 @@ export function makeApp(appConfig, onTerminate) {
627
641
  ],
628
642
  ),
629
643
  ) ?? rxjs.of([]),
644
+ underPressure.pressure$.pipe(
645
+ rx.map((pressure) =>
646
+ pressure == null
647
+ ? []
648
+ : [
649
+ {
650
+ id: 'app:under_pressure',
651
+ level: 40,
652
+ code: 'NXT_PRESSURE',
653
+ msg: `Under Pressure`,
654
+ },
655
+ ],
656
+ ),
657
+ ) ?? rxjs.of([]),
630
658
  couch
631
659
  ? rxjs.timer(0, 10e3).pipe(
632
660
  rx.exhaustMap(async () => {
@@ -1105,6 +1133,7 @@ export function makeApp(appConfig, onTerminate) {
1105
1133
  nxt,
1106
1134
  logger,
1107
1135
  toobusy,
1136
+ underPressure,
1108
1137
  destroyers,
1109
1138
  couch,
1110
1139
  server,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.6.7",
3
+ "version": "23.6.9",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
package/under-pressure.js CHANGED
@@ -1,5 +1,6 @@
1
1
  // Based on: https://github.com/fastify/under-pressure/blob/main/index.js
2
2
  import { monitorEventLoopDelay } from 'node:perf_hooks'
3
+ import * as rxjs from 'rxjs'
3
4
 
4
5
  function getSampleInterval(value, eventLoopResolution) {
5
6
  const sampleInterval = value || 1000
@@ -21,12 +22,14 @@ export default function (opts = {}) {
21
22
  const checkMaxHeapUsedBytes = maxHeapUsedBytes > 0
22
23
  const checkMaxRssBytes = maxRssBytes > 0
23
24
  const checkMaxEventLoopUtilization = maxEventLoopUtilization > 0
25
+ const pressure$ = new rxjs.BehaviorSubject(null)
24
26
 
25
27
  let heapUsedBytes = 0
26
28
  let rssBytes = 0
27
29
  let eventLoopDelay = 0
28
30
  let elu
29
31
  let eventLoopUtilized = 0
32
+ let wasUnderPressure = false
30
33
 
31
34
  const histogram = monitorEventLoopDelay({ resolution })
32
35
  histogram.enable()
@@ -69,7 +72,14 @@ export default function (opts = {}) {
69
72
  updateEventLoopUtilization()
70
73
 
71
74
  if (isUnderPressure()) {
72
- onPressure(getPressure())
75
+ if (!wasUnderPressure) {
76
+ wasUnderPressure = true
77
+ onPressure(getPressure())
78
+ pressure$.next(getPressure())
79
+ }
80
+ } else if (wasUnderPressure) {
81
+ wasUnderPressure = false
82
+ pressure$.next(null)
73
83
  }
74
84
 
75
85
  timer.refresh()
@@ -107,6 +117,9 @@ export default function (opts = {}) {
107
117
  return {
108
118
  isUnderPressure,
109
119
  getPressure,
120
+ get pressure$() {
121
+ return pressure$.asObservable()
122
+ },
110
123
  [Symbol.dispose]() {
111
124
  clearTimeout(timer)
112
125
  },