@isoloop/bus 0.0.0-next.5aa1a15 → 0.0.0-next.61245db

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.
@@ -2328,9 +2328,11 @@ var TsEngine = class {
2328
2328
  /** Drops every record below `offset` (last-n). */
2329
2329
  #dropBelow(t, offset) {
2330
2330
  if (offset <= t.tail) return;
2331
- t.live -= this.#countLive(t, t.tail, offset);
2331
+ const from = t.tail;
2332
+ t.live -= this.#countLive(t, from, offset);
2332
2333
  t.tail = offset;
2333
2334
  this.#freeChunks(t);
2335
+ this.#clearBelowTail(t, from);
2334
2336
  }
2335
2337
  /** Records in `[from, to)` still held. Last-n topics have no holes, so this is `to - from` there. */
2336
2338
  #countLive(t, from, to) {
@@ -2341,6 +2343,7 @@ var TsEngine = class {
2341
2343
  }
2342
2344
  /** Drops records from the tail while the oldest is older than `cutoff` (time). */
2343
2345
  #expire(t, cutoff) {
2346
+ const from = t.tail;
2344
2347
  let o = t.tail;
2345
2348
  while (o < t.evictedTo) {
2346
2349
  const s = t.evicted[0];
@@ -2351,6 +2354,7 @@ var TsEngine = class {
2351
2354
  if (s.maxTs >= cutoff) {
2352
2355
  t.tail = o;
2353
2356
  this.#freeChunks(t);
2357
+ this.#clearBelowTail(t, from);
2354
2358
  return;
2355
2359
  }
2356
2360
  t.live -= s.count;
@@ -2365,6 +2369,20 @@ var TsEngine = class {
2365
2369
  }
2366
2370
  t.tail = Math.min(o, t.head);
2367
2371
  this.#freeChunks(t);
2372
+ this.#clearBelowTail(t, from);
2373
+ }
2374
+ /**
2375
+ * Clears the slots in `[from, tail)` that are still in a held chunk:
2376
+ * `#freeChunks` frees whole chunks only, so without this a slow topic keeps
2377
+ * its expired records referenced until the chunk holding its tail fills,
2378
+ * minutes at a few records a second (plant memory investigation, #162).
2379
+ * Each record is cleared once, as the tail passes it.
2380
+ */
2381
+ #clearBelowTail(t, from) {
2382
+ for (let o = Math.max(from, t.base * this.#size); o < t.tail; o++) {
2383
+ const chunk = this.#chunkOf(t, o);
2384
+ if (chunk) chunk[o % this.#size] = void 0;
2385
+ }
2368
2386
  }
2369
2387
  /** Drops records superseded by a later record with the same key; keyless records stay. */
2370
2388
  #compactKeys(t) {
@@ -2438,6 +2456,12 @@ var TsEngine = class {
2438
2456
  chunkCount(topic) {
2439
2457
  return this.#topic(topic).chunks.length;
2440
2458
  }
2459
+ /** Records held in memory for a topic, for tests and metrics. */
2460
+ heldCount(topic) {
2461
+ let n = 0;
2462
+ for (const chunk of this.#topic(topic).chunks) for (const r of chunk ?? []) if (r) n++;
2463
+ return n;
2464
+ }
2441
2465
  };
2442
2466
  /** Notes that `producer` appended `id` at `offset`; a new epoch forgets the old one's. */
2443
2467
  function remember(t, producer, id, offset) {
package/dist/index.d.ts CHANGED
@@ -195,6 +195,8 @@ export declare class TsEngine implements LogEngine {
195
195
  compact(topic: string): void;
196
196
  /** Chunks held for a topic, for tests and metrics. */
197
197
  chunkCount(topic: string): number;
198
+ /** Records held in memory for a topic, for tests and metrics. */
199
+ heldCount(topic: string): number;
198
200
  }
199
201
  /**
200
202
  * One isolate's bus connection, as the broker sees it. The bus worker wraps a
package/dist/index.js CHANGED
@@ -543,9 +543,11 @@ var TsEngine = class {
543
543
  /** Drops every record below `offset` (last-n). */
544
544
  #dropBelow(t, offset) {
545
545
  if (offset <= t.tail) return;
546
- t.live -= this.#countLive(t, t.tail, offset);
546
+ const from = t.tail;
547
+ t.live -= this.#countLive(t, from, offset);
547
548
  t.tail = offset;
548
549
  this.#freeChunks(t);
550
+ this.#clearBelowTail(t, from);
549
551
  }
550
552
  /** Records in `[from, to)` still held. Last-n topics have no holes, so this is `to - from` there. */
551
553
  #countLive(t, from, to) {
@@ -556,6 +558,7 @@ var TsEngine = class {
556
558
  }
557
559
  /** Drops records from the tail while the oldest is older than `cutoff` (time). */
558
560
  #expire(t, cutoff) {
561
+ const from = t.tail;
559
562
  let o = t.tail;
560
563
  while (o < t.evictedTo) {
561
564
  const s = t.evicted[0];
@@ -566,6 +569,7 @@ var TsEngine = class {
566
569
  if (s.maxTs >= cutoff) {
567
570
  t.tail = o;
568
571
  this.#freeChunks(t);
572
+ this.#clearBelowTail(t, from);
569
573
  return;
570
574
  }
571
575
  t.live -= s.count;
@@ -580,6 +584,20 @@ var TsEngine = class {
580
584
  }
581
585
  t.tail = Math.min(o, t.head);
582
586
  this.#freeChunks(t);
587
+ this.#clearBelowTail(t, from);
588
+ }
589
+ /**
590
+ * Clears the slots in `[from, tail)` that are still in a held chunk:
591
+ * `#freeChunks` frees whole chunks only, so without this a slow topic keeps
592
+ * its expired records referenced until the chunk holding its tail fills,
593
+ * minutes at a few records a second (plant memory investigation, #162).
594
+ * Each record is cleared once, as the tail passes it.
595
+ */
596
+ #clearBelowTail(t, from) {
597
+ for (let o = Math.max(from, t.base * this.#size); o < t.tail; o++) {
598
+ const chunk = this.#chunkOf(t, o);
599
+ if (chunk) chunk[o % this.#size] = void 0;
600
+ }
583
601
  }
584
602
  /** Drops records superseded by a later record with the same key; keyless records stay. */
585
603
  #compactKeys(t) {
@@ -653,6 +671,12 @@ var TsEngine = class {
653
671
  chunkCount(topic) {
654
672
  return this.#topic(topic).chunks.length;
655
673
  }
674
+ /** Records held in memory for a topic, for tests and metrics. */
675
+ heldCount(topic) {
676
+ let n = 0;
677
+ for (const chunk of this.#topic(topic).chunks) for (const r of chunk ?? []) if (r) n++;
678
+ return n;
679
+ }
656
680
  };
657
681
  /** Notes that `producer` appended `id` at `offset`; a new epoch forgets the old one's. */
658
682
  function remember(t, producer, id, offset) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@isoloop/bus",
3
- "version": "0.0.0-next.5aa1a15",
3
+ "version": "0.0.0-next.61245db",
4
4
  "description": "Log bus that runs in its own worker: topics, schemas, offsets, replay.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -27,8 +27,8 @@
27
27
  }
28
28
  },
29
29
  "dependencies": {
30
- "@isoloop/codec": "0.0.0-next.5aa1a15",
31
- "@isoloop/protocol": "0.0.0-next.5aa1a15"
30
+ "@isoloop/codec": "0.0.0-next.61245db",
31
+ "@isoloop/protocol": "0.0.0-next.61245db"
32
32
  },
33
33
  "devDependencies": {
34
34
  "fast-check": "4.10.2",