@isoloop/bus 0.0.0-next.5bbb503 → 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.
@@ -8,11 +8,12 @@ const isRecord$1 = (x) => typeof x === "object" && x !== null && !Array.isArray(
8
8
  const isOptionalString = (x) => x === void 0 || typeof x === "string";
9
9
  /**
10
10
  * True for a well-formed envelope this build can read: `v` is a supported
11
- * version, `t` is a string, `id`/`re` are absent or strings, and `p` is present.
11
+ * version, `t` is a string, `id`/`re`/`isolate` are absent or strings, and `p`
12
+ * is present.
12
13
  * Says nothing about whether `t` is known; see the per-port guards.
13
14
  */
14
15
  function isEnvelope(x) {
15
- return isRecord$1(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]);
16
+ return isRecord$1(x) && x["v"] === PROTOCOL.v && typeof x["t"] === "string" && "p" in x && isOptionalString(x["id"]) && isOptionalString(x["re"]) && isOptionalString(x["isolate"]);
16
17
  }
17
18
  /**
18
19
  * Builds a guard for one port's union from its message types. Each side drops
@@ -2327,9 +2328,11 @@ var TsEngine = class {
2327
2328
  /** Drops every record below `offset` (last-n). */
2328
2329
  #dropBelow(t, offset) {
2329
2330
  if (offset <= t.tail) return;
2330
- t.live -= this.#countLive(t, t.tail, offset);
2331
+ const from = t.tail;
2332
+ t.live -= this.#countLive(t, from, offset);
2331
2333
  t.tail = offset;
2332
2334
  this.#freeChunks(t);
2335
+ this.#clearBelowTail(t, from);
2333
2336
  }
2334
2337
  /** Records in `[from, to)` still held. Last-n topics have no holes, so this is `to - from` there. */
2335
2338
  #countLive(t, from, to) {
@@ -2340,6 +2343,7 @@ var TsEngine = class {
2340
2343
  }
2341
2344
  /** Drops records from the tail while the oldest is older than `cutoff` (time). */
2342
2345
  #expire(t, cutoff) {
2346
+ const from = t.tail;
2343
2347
  let o = t.tail;
2344
2348
  while (o < t.evictedTo) {
2345
2349
  const s = t.evicted[0];
@@ -2350,6 +2354,7 @@ var TsEngine = class {
2350
2354
  if (s.maxTs >= cutoff) {
2351
2355
  t.tail = o;
2352
2356
  this.#freeChunks(t);
2357
+ this.#clearBelowTail(t, from);
2353
2358
  return;
2354
2359
  }
2355
2360
  t.live -= s.count;
@@ -2364,6 +2369,20 @@ var TsEngine = class {
2364
2369
  }
2365
2370
  t.tail = Math.min(o, t.head);
2366
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
+ }
2367
2386
  }
2368
2387
  /** Drops records superseded by a later record with the same key; keyless records stay. */
2369
2388
  #compactKeys(t) {
@@ -2437,6 +2456,12 @@ var TsEngine = class {
2437
2456
  chunkCount(topic) {
2438
2457
  return this.#topic(topic).chunks.length;
2439
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
+ }
2440
2465
  };
2441
2466
  /** Notes that `producer` appended `id` at `offset`; a new epoch forgets the old one's. */
2442
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.5bbb503",
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.5bbb503",
31
- "@isoloop/protocol": "0.0.0-next.5bbb503"
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",