@nxtedition/lib 23.6.7 → 23.6.8

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,11 @@ 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
+ }
350
+
345
351
  if (appConfig.couchdb || appConfig.couch) {
346
352
  const couchConfig = fp.mergeAll(
347
353
  [
@@ -536,6 +542,7 @@ export function makeApp(appConfig, onTerminate) {
536
542
  couch: couch?.stats,
537
543
  lag: toobusy?.lag(),
538
544
  version: process.version,
545
+ underPressure: underPressure?.isUnderPressure() ? underPressure.getPressure() : null,
539
546
  cpu,
540
547
  memory,
541
548
  totalMemory,
@@ -627,6 +634,20 @@ export function makeApp(appConfig, onTerminate) {
627
634
  ],
628
635
  ),
629
636
  ) ?? rxjs.of([]),
637
+ underPressure.pressure$.pipe(
638
+ rx.map((pressure) =>
639
+ pressure == null
640
+ ? []
641
+ : [
642
+ {
643
+ id: 'app:under_pressure',
644
+ level: 40,
645
+ code: 'NXT_PRESSURE',
646
+ msg: `Under Pressure`,
647
+ },
648
+ ],
649
+ ),
650
+ ) ?? rxjs.of([]),
630
651
  couch
631
652
  ? rxjs.timer(0, 10e3).pipe(
632
653
  rx.exhaustMap(async () => {
@@ -1105,6 +1126,7 @@ export function makeApp(appConfig, onTerminate) {
1105
1126
  nxt,
1106
1127
  logger,
1107
1128
  toobusy,
1129
+ underPressure,
1108
1130
  destroyers,
1109
1131
  couch,
1110
1132
  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.8",
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
  },