@nxtedition/lib 23.17.8 → 23.17.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.
Files changed (3) hide show
  1. package/app.js +26 -20
  2. package/couch.d.ts +1 -0
  3. package/package.json +1 -1
package/app.js CHANGED
@@ -33,11 +33,11 @@ import compose from 'koa-compose'
33
33
  import { createServer } from './http.js'
34
34
  import { json } from 'node:stream/consumers'
35
35
  import { monitorEventLoopDelay } from 'node:perf_hooks'
36
- import { LRUCache } from 'lru-cache'
37
36
  import xuid from 'xuid'
38
37
  import { isTimeBetween } from './time.js'
39
38
  import makeUnderPressure from './under-pressure.js'
40
39
  import undici from '@nxtedition/undici'
40
+ import { isPrimary } from 'node:cluster'
41
41
 
42
42
  export function makeApp(appConfig, onTerminate) {
43
43
  let ds
@@ -289,7 +289,6 @@ export function makeApp(appConfig, onTerminate) {
289
289
  const resolution = appConfig.toobusy.resolution ?? 10
290
290
  const interval = appConfig.toobusy.interval ?? 500
291
291
  const maxLag = appConfig.toobusy.maxLag ?? 100
292
- const id = xuid()
293
292
 
294
293
  const currentLag$ = new rxjs.BehaviorSubject(0)
295
294
  const appLag$ = new rxjs.BehaviorSubject(0)
@@ -297,21 +296,34 @@ export function makeApp(appConfig, onTerminate) {
297
296
  const histogram = monitorEventLoopDelay({ resolution })
298
297
  histogram.enable()
299
298
 
300
- const lagMap = new LRUCache({ ttl: 10e3, max: 1024 })
301
-
302
299
  const lagBC = new BroadcastChannel('nxt:lag')
303
300
  lagBC.unref()
304
301
 
305
- lagBC.onmessage = ({ data }) => {
306
- lagMap.set(data.id, data.currentLag)
307
- }
302
+ if (isMainThread && isPrimary) {
303
+ const lagMap = new Map()
308
304
 
309
- process.on('message', (data) => {
310
- if (data?.type === 'nxt:lag') {
305
+ lagBC.onmessage = ({ data }) => {
311
306
  lagMap.set(data.id, data.currentLag)
312
307
  }
313
- })
314
308
 
309
+ process.on('message', (data) => {
310
+ if (data?.type === 'nxt:lag') {
311
+ lagMap.set(data.id, data.currentLag)
312
+ }
313
+ })
314
+
315
+ setInterval(() => {
316
+ let appLag = currentLag$.value
317
+ for (const lag of lagMap.values()) {
318
+ appLag = Math.max(appLag, lag)
319
+ }
320
+ lagMap.clear()
321
+
322
+ appLag$.next(appLag)
323
+ }, 500).unref()
324
+ }
325
+
326
+ const lagId = (process.pid << 16) | threadId
315
327
  setInterval(() => {
316
328
  let currentLag = Math.max(0, histogram.mean / 1e6 - resolution)
317
329
  if (Number.isNaN(currentLag)) {
@@ -320,20 +332,14 @@ export function makeApp(appConfig, onTerminate) {
320
332
  histogram.reset()
321
333
 
322
334
  if (currentLag > 1e3) {
323
- logger?.error({ currentLag, elapsedTime: currentLag }, 'lag')
335
+ logger?.error({ currentLag, elapsedTime: currentLag }, 'Lag')
324
336
  } else if (currentLag > maxLag) {
325
- logger?.warn({ currentLag, elapsedTime: currentLag }, 'lag')
337
+ logger?.warn({ currentLag, elapsedTime: currentLag }, 'Lag')
326
338
  }
327
339
 
328
340
  currentLag$.next(currentLag)
329
- lagBC.postMessage({ id, currentLag })
330
- process.send?.({ type: 'nxt:lag', id, currentLag })
331
-
332
- let appLag = currentLag$.value ?? 0
333
- for (const lag of lagMap.values()) {
334
- appLag = Math.max(appLag, lag ?? 0)
335
- }
336
- appLag$.next(appLag)
341
+ lagBC.postMessage({ id: lagId, currentLag })
342
+ process.send?.({ type: 'nxt:lag', id: lagId, currentLag })
337
343
  }, interval).unref()
338
344
 
339
345
  toobusy = () => currentLag$.value > maxLag
package/couch.d.ts CHANGED
@@ -8,6 +8,7 @@ export interface CouchOptions {
8
8
  export function makeCouch(opts: string | CouchOptions): CouchClient
9
9
 
10
10
  export interface CouchRequestOptions<Stream extends boolean = false> {
11
+ id?: string
11
12
  url?: URL
12
13
  pathname?: string
13
14
  path?: string
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.17.8",
3
+ "version": "23.17.9",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",