@nxtedition/lib 28.0.20 → 28.0.21

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 +1 -1
  2. package/cache.js +20 -4
  3. package/memory.js +10 -2
  4. package/package.json +3 -2
package/app.js CHANGED
@@ -810,7 +810,7 @@ export function makeApp(appConfig, onTerminateOrMeta, metaOrNull) {
810
810
  messages.push({
811
811
  id: 'app:container_memory_usage',
812
812
  level: usagePercent > 90 ? 50 : usagePercent > 70 ? 40 : 30,
813
- msg: `Memory Usage: ${usagePercent.toFixed(2)}%`,
813
+ msg: `Memory Usage: ${usagePercent.toFixed(2)}% (${(memory.containerUsage / 1e9).toFixed(2)} GiB / ${(memory.containerLimit / 1e9).toFixed(2)} GiB)`,
814
814
  })
815
815
  }
816
816
 
package/cache.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { DatabaseSync } from 'node:sqlite'
2
2
  import { LRUCache } from 'lru-cache'
3
3
  import { fastNow } from './time.js'
4
+ import { doYield } from '@nxtedition/yield'
4
5
 
5
6
  function noop() {}
6
7
 
@@ -50,6 +51,8 @@ export class AsyncCache {
50
51
  #delQuery
51
52
  #purgeStaleQuery
52
53
 
54
+ #setQueue = []
55
+
53
56
  /**
54
57
  * @param {string} location
55
58
  * @param {((...args: any[]) => Promise<V>)|undefined} [valueSelector]
@@ -304,10 +307,23 @@ export class AsyncCache {
304
307
 
305
308
  this.#lru?.set(key, { ttl, stale, value })
306
309
 
307
- try {
308
- this.#setQuery?.run(key, JSON.stringify(value), ttl, stale)
309
- } catch {
310
- // Do nothing...
310
+ this.#setQueue.push({ key, value, ttl, stale })
311
+ if (this.#setQueue.length === 1) {
312
+ doYield(this.#flushSetQueue)
313
+ }
314
+ }
315
+
316
+ #flushSetQueue = () => {
317
+ for (const { key, value, ttl, stale } of this.#setQueue.splice(0, 64)) {
318
+ try {
319
+ this.#setQuery?.run(key, JSON.stringify(value), ttl, stale)
320
+ } catch {
321
+ // Do nothing...
322
+ }
323
+ }
324
+
325
+ if (this.#setQueue.length > 0) {
326
+ doYield(this.#flushSetQueue)
311
327
  }
312
328
  }
313
329
 
package/memory.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import { readFileSync } from 'node:fs'
2
2
 
3
+ const INACTIVE_FILE_REGEX = /^inactive_file\s+(\d+)/m
4
+
3
5
  function readFile(path) {
4
6
  try {
5
7
  return readFileSync(path, 'utf8').trim()
@@ -30,13 +32,19 @@ export function getContainerMemoryUsage() {
30
32
  // cgroups v2
31
33
  const v2Usage = readFile('/sys/fs/cgroup/memory.current')
32
34
  if (v2Usage) {
33
- return Number(v2Usage)
35
+ const usage = Number(v2Usage)
36
+ const stat = readFile('/sys/fs/cgroup/memory.stat')
37
+ const inactiveFile = Number(stat?.match(INACTIVE_FILE_REGEX)?.[1] ?? 0)
38
+ return Math.max(0, usage - inactiveFile)
34
39
  }
35
40
 
36
41
  // cgroups v1
37
42
  const v1Usage = readFile('/sys/fs/cgroup/memory/memory.usage_in_bytes')
38
43
  if (v1Usage) {
39
- return Number(v1Usage)
44
+ const usage = Number(v1Usage)
45
+ const stat = readFile('/sys/fs/cgroup/memory/memory.stat')
46
+ const inactiveFile = Number(stat?.match(INACTIVE_FILE_REGEX)?.[1] ?? 0)
47
+ return Math.max(0, usage - inactiveFile)
40
48
  }
41
49
 
42
50
  return undefined
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nxtedition/lib",
3
- "version": "28.0.20",
3
+ "version": "28.0.21",
4
4
  "license": "UNLICENSED",
5
5
  "author": "Robert Nagy <robert.nagy@boffins.se>",
6
6
  "type": "module",
@@ -54,6 +54,7 @@
54
54
  "@nxtedition/sched": "^1.0.2",
55
55
  "@nxtedition/template": "^1.0.10",
56
56
  "@nxtedition/weak-cache": "^1.0.2",
57
+ "@nxtedition/yield": "^1.0.2",
57
58
  "diff": "5.2.0",
58
59
  "fast-querystring": "^1.1.2",
59
60
  "flamegraph-middleware": "^1.0.0",
@@ -92,5 +93,5 @@
92
93
  "pino": ">=7.0.0",
93
94
  "rxjs": "^7.0.0"
94
95
  },
95
- "gitHead": "b33dfcfc5f68626f846f364805f51a0e6de74cce"
96
+ "gitHead": "d6448b2a05077952c84d8fb8bcbcbf0c2d78fbd5"
96
97
  }