@nxtedition/lib 23.17.7 → 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 (4) hide show
  1. package/app.js +26 -20
  2. package/couch.d.ts +1 -0
  3. package/http.js +42 -40
  4. 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/http.js CHANGED
@@ -42,9 +42,6 @@ export class Context {
42
42
  #res
43
43
  #ac
44
44
  #logger
45
-
46
- // deprecated fields
47
- #userAgent
48
45
  #query
49
46
  #target
50
47
 
@@ -52,15 +49,14 @@ export class Context {
52
49
  assert(req)
53
50
  assert(res)
54
51
 
55
- this.#id = req.headers['request-id'] || req.headers['Request-Id'] || genReqId()
56
- this.#userAgent = req.headers['user-agent'] || req.headers['User-Agent'] || ''
52
+ this.#id = req.id || req.headers['request-id'] || req.headers['Request-Id'] || genReqId()
57
53
  this.#req = req
58
54
  this.#res = res
59
- this.#logger = logger.child({ rid: this.#id, userAgent: this.#userAgent })
55
+ this.#logger = logger.child({ rid: this.#id })
60
56
  }
61
57
 
62
58
  get id() {
63
- return this.#req.id ?? this.#id
59
+ return this.#id
64
60
  }
65
61
 
66
62
  get logger() {
@@ -75,6 +71,38 @@ export class Context {
75
71
  return this.#res
76
72
  }
77
73
 
74
+ get target() {
75
+ if (this.#req.target) {
76
+ return this.#req.target
77
+ }
78
+
79
+ this.#target ??= requestTarget(this.#req)
80
+
81
+ if (!this.#target) {
82
+ throw new createError.BadRequest('invalid url')
83
+ }
84
+
85
+ return this.#target
86
+ }
87
+
88
+ get query() {
89
+ if (this.#req.query) {
90
+ return this.#req.query
91
+ }
92
+
93
+ this.#target ??= requestTarget(this.#req)
94
+ this.#query ??=
95
+ this.#target.search.length > 1
96
+ ? Object.freeze(querystring.parse(this.#target.search.slice(1)))
97
+ : {}
98
+
99
+ return this.#query
100
+ }
101
+
102
+ get userAgent() {
103
+ return this.#req.headers['user-agent'] || this.#req.headers['User-Agent'] || ''
104
+ }
105
+
78
106
  get [kAbortController]() {
79
107
  return this.#ac
80
108
  }
@@ -84,11 +112,6 @@ export class Context {
84
112
  return this.#ac.signal
85
113
  }
86
114
 
87
- /** @deprecated */
88
- get userAgent() {
89
- return this.#req.userAgent ?? this.#userAgent
90
- }
91
-
92
115
  /** @deprecated */
93
116
  get method() {
94
117
  return this.#req.method
@@ -96,17 +119,7 @@ export class Context {
96
119
 
97
120
  /** @deprecated */
98
121
  get url() {
99
- if (this.#req.target) {
100
- return this.#req.target
101
- }
102
-
103
- this.#target ??= requestTarget(this.#req)
104
-
105
- if (!this.#target) {
106
- throw new createError.BadRequest('invalid url')
107
- }
108
-
109
- return this.#target
122
+ return this.target
110
123
  }
111
124
 
112
125
  /** @deprecated */
@@ -120,21 +133,6 @@ export class Context {
120
133
  this.#query = undefined
121
134
  }
122
135
 
123
- /** @deprecated */
124
- get query() {
125
- if (this.#req.query) {
126
- return this.#req.query
127
- }
128
-
129
- this.#target ??= requestTarget(this.#req)
130
- this.#query ??=
131
- this.#target.search.length > 1
132
- ? Object.freeze(querystring.parse(this.#target.search.slice(1)))
133
- : {}
134
-
135
- return this.#query
136
- }
137
-
138
136
  /** @deprecated */
139
137
  set logger(logger) {
140
138
  this.#logger = logger.child({ rid: this.#id })
@@ -349,15 +347,17 @@ export class IncomingMessage extends http.IncomingMessage {
349
347
  #socket
350
348
  #target
351
349
 
350
+ #id
352
351
  #search
353
352
  #query
354
353
 
355
354
  constructor(...args) {
356
355
  super(...args)
356
+ this.#id = this.headers['request-id'] || this.headers['Request-Id'] || genReqId()
357
357
  }
358
358
 
359
359
  get id() {
360
- return this.headers['request-id'] || this.headers['Request-Id']
360
+ return this.#id
361
361
  }
362
362
 
363
363
  get target() {
@@ -493,15 +493,17 @@ export class Http2ServerRequest extends http2.Http2ServerRequest {
493
493
  #socket
494
494
  #target
495
495
 
496
+ #id
496
497
  #search
497
498
  #query
498
499
 
499
500
  constructor(...args) {
500
501
  super(...args)
502
+ this.#id = this.headers['request-id'] || this.headers['Request-Id'] || genReqId()
501
503
  }
502
504
 
503
505
  get id() {
504
- return this.headers['request-id'] || this.headers['Request-Id']
506
+ return this.#id
505
507
  }
506
508
 
507
509
  get target() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "23.17.7",
3
+ "version": "23.17.9",
4
4
  "license": "MIT",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",