castrum 0.9.8 → 0.9.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.
- package/Cargo.lock +1 -1
- package/Cargo.toml +1 -1
- package/castrum.darwin-arm64.node +0 -0
- package/castrum.darwin-x64.node +0 -0
- package/castrum.linux-arm64-gnu.node +0 -0
- package/castrum.linux-arm64-musl.node +0 -0
- package/castrum.linux-x64-gnu.node +0 -0
- package/castrum.linux-x64-musl.node +0 -0
- package/castrum.linux-x64-v3-gnu.node +0 -0
- package/castrum.win32-x64-msvc.node +0 -0
- package/dist/index.d.ts +164 -4
- package/dist/index.js +434 -70
- package/docs/ENVIRONMENT.md +7 -0
- package/docs/RND-CONCURRENCY.md +20 -4
- package/docs/superpowers/plans/2026-09-18-elysia-learnings-adoption.md +634 -0
- package/index.ts +5 -0
- package/package.json +2 -1
- package/rust/ffi/task.rs +19 -18
- package/rust/ingress/api.rs +21 -5
- package/rust/ingress/mod.rs +7 -0
- package/rust/ingress/native_route.rs +4 -2
- package/rust/ingress/schema_cache.rs +266 -0
- package/rust/task/mod.rs +1 -0
- package/rust/task/runtime.rs +55 -7
- package/rust/task/tests.rs +98 -1
- package/src/ingress/abort.ts +48 -0
- package/src/ingress/body.ts +104 -8
- package/src/ingress/handlers.ts +1 -0
- package/src/ingress/health.ts +22 -0
- package/src/ingress/index.ts +3 -1
- package/src/ingress/router.ts +36 -1
- package/src/ingress/routes/common.ts +9 -1
- package/src/ingress/routes/echo.ts +10 -0
- package/src/ingress/routes/json-write.ts +11 -0
- package/src/ingress/routes/native.ts +24 -5
- package/src/ingress/routes/options.ts +6 -2
- package/src/ingress/routes/responder.ts +25 -5
- package/src/ingress/routes/safe-record.ts +57 -0
- package/src/ingress/server-error.ts +111 -0
- package/src/ingress/server-node.ts +102 -23
- package/src/ingress/server.ts +140 -32
- package/src/integration/pipeline.ts +6 -0
- package/src/native/ffi/build/task.ts +2 -1
- package/src/native/ffi/types.ts +6 -1
- package/src/native/types.ts +6 -0
- package/src/runtime/detect.ts +14 -0
- package/src/rust-ffi/context.ts +46 -3
- package/src/rust-ffi/scalar/factories.ts +6 -0
- package/src/rust-ffi/scalar/interface.ts +14 -0
- package/src/rust-ffi/text.ts +5 -4
- package/src/shared/buffer-pool.ts +10 -0
- package/src/shared/memory.ts +68 -0
- package/src/shared/metrics.ts +86 -3
- package/src/shared/runtime.ts +7 -0
- package/src/task/runtime.ts +52 -9
package/docs/ENVIRONMENT.md
CHANGED
|
@@ -38,8 +38,15 @@ for false; anything else is warned about and falls back to the default.
|
|
|
38
38
|
| Variable | Type | Default | Description |
|
|
39
39
|
|----------|------|---------|-------------|
|
|
40
40
|
| `CASTRUM_TASK_THREADS` | int | `max(1, cores-1)` | Worker threads for the off-thread task runtime (castrum Tasks). Read when the pool first starts — the pool is process-wide and cannot be resized afterwards (`tasks.stats().threads` reports the live count). |
|
|
41
|
+
| `CASTRUM_TASK_QUEUE_MAX` | int | `4096` | Admission bound on the off-thread task work queue. Read when the pool first starts (minimum 1). A submit that would exceed it is rejected rather than growing memory: the native submit returns `2` and the task promise rejects with a typed `OVERLOADED` error. |
|
|
41
42
|
| `CASTRUM_TASK_PIN_CORES` | (presence) | — | When set (Linux), pin each task worker to its own core (core 0 is skipped so the JS event loop keeps it). `CASTRUM_PIN_CORES` enables it too. Off by default: pinning competes with the JS/rayon threads for the same box. |
|
|
42
43
|
|
|
44
|
+
## Compiled-schema cache (`rust/ingress/schema_cache.rs`)
|
|
45
|
+
|
|
46
|
+
| Variable | Type | Default | Description |
|
|
47
|
+
|----------|------|---------|-------------|
|
|
48
|
+
| `CASTRUM_SCHEMA_CACHE_MAX` | int | `128` | Max distinct request-body schemas retained process-wide. `Ingress`/`NativeRoute` share one compiled `IngressSchema` per distinct schema byte string; this bounds that LRU. Read at first schema compile (the cache is process-wide and cannot be resized afterwards). Values below 1 (unset, unparseable, `0`) fall back to the default; values are clamped to `4096`. Call `flushMemory()` to drop the cache explicitly. |
|
|
49
|
+
|
|
43
50
|
## Native transport selection (`src/native/ffi.ts`)
|
|
44
51
|
|
|
45
52
|
`bun:ffi` is the PRIMARY transport under Bun; NAPI is the fallback (Node,
|
package/docs/RND-CONCURRENCY.md
CHANGED
|
@@ -69,7 +69,8 @@ documented `JSTypedArrayBytesDeallocator` hook crashes the process (fault at
|
|
|
69
69
|
|
|
70
70
|
```
|
|
71
71
|
rust/task/runtime.rs condvar pool, N = cores−1 (CASTRUM_TASK_THREADS), auto-start on submit,
|
|
72
|
-
batch dequeue + bounded spin-then-park, optional core pinning
|
|
72
|
+
batch dequeue + bounded spin-then-park, optional core pinning,
|
|
73
|
+
HARD admission bound on the queue (CASTRUM_TASK_QUEUE_MAX)
|
|
73
74
|
rust/task/completion.rs result ring + ARMED coalescing doorbell + needed-size drain layout
|
|
74
75
|
rust/task/ops.rs op dispatch (header + payload split), cancellation set, catch_unwind
|
|
75
76
|
containment, `_into` (zero-copy output) and slice (zero-copy input) forms
|
|
@@ -121,6 +122,17 @@ right one is picked per op:
|
|
|
121
122
|
copied and the payload is read in place, so a large argument never crosses the
|
|
122
123
|
boundary. This one was forced by a measurement, not symmetry (§7b).
|
|
123
124
|
|
|
125
|
+
**Bounded admission (backpressure).** The work queue is HARD-bounded by
|
|
126
|
+
`CASTRUM_TASK_QUEUE_MAX` (default 4096, minimum 1; read once when the pool is
|
|
127
|
+
created). A submit that would exceed the bound is rejected instead of growing
|
|
128
|
+
memory: `castrum_task_submit{,_out,_slice}` return `2 = overloaded`, and the JS
|
|
129
|
+
runtime rejects the task promise with a typed `code: 'OVERLOADED'` error. The
|
|
130
|
+
existing return meanings are unchanged — `1` accepted, `0` invalid args / panic.
|
|
131
|
+
A rejected zero-copy submit drops its `output`/`keep` reference exactly like any
|
|
132
|
+
other rejection, so a full queue cannot leak buffers. (The richer M2 design — a
|
|
133
|
+
`WouldBlock` + re-queue-behind-drain policy and priority levels — remains a
|
|
134
|
+
later milestone; v1 is a hard cap with a distinct overload outcome.)
|
|
135
|
+
|
|
124
136
|
**Measured comparison** (`bun run bench:task`, Bun 1.4.2, 12 cores):
|
|
125
137
|
|
|
126
138
|
| variant | time | loop stall | note |
|
|
@@ -466,9 +478,12 @@ rust/task/
|
|
|
466
478
|
as in rayon/Go. Deliberately **separate from the rayon global pool**: rayon
|
|
467
479
|
stays the batch executor for `par_iter`; the task runtime owns offloaded ops.
|
|
468
480
|
`CASTRUM_TASK_THREADS` sizes it (default `cores − 1`).
|
|
469
|
-
- **Admission/backpressure
|
|
470
|
-
|
|
471
|
-
|
|
481
|
+
- **Admission/backpressure** (v1 shipped): the queue is hard-bounded by
|
|
482
|
+
`CASTRUM_TASK_QUEUE_MAX` (default 4096, min 1, read once at pool creation).
|
|
483
|
+
`submit` returns `false` when full; the C ABI reports `2 = overloaded` and JS
|
|
484
|
+
rejects that promise with a typed `OVERLOADED` error — no unbounded growth
|
|
485
|
+
(fixes F6). M2 replaces this hard cap with `WouldBlock` + requeue-behind-drain
|
|
486
|
+
and priority levels; v1 has neither.
|
|
472
487
|
- **Cancellation**: each task carries an `Arc<CancelToken>` (atomic + reason).
|
|
473
488
|
Ops check it at safepoints (between chunks: gzip blocks, schema subtrees,
|
|
474
489
|
Argon2 is atomic and reports "not cancellable"). Scope cancellation cascades
|
|
@@ -618,6 +633,7 @@ Each of these removes syscalls, lock traffic, or reallocations from the hot path
|
|
|
618
633
|
| **Zero-copy INPUT** (`castrum_task_submit_slice` + the header/payload split in `ops.rs`) | Forced by a measurement: packing the payload into the args blob copied the whole input on the JS thread, making offloaded `gzipCompress` of 24 MiB **18.6 ms / 11.7 ms stall against 5.6 ms synchronous** — a net loss on both axes. Reading the payload in place: **7.3 ms / 2.1 ms**. The split also simplified every op arm (no more `args[4..]` offset arithmetic). |
|
|
619
634
|
| **No BigInt, no per-completion DataView** | The drain loop reads ids and lengths as two `u32` reads at an offset — `getBigUint64` boxes a BigInt and `readU64` allocated a DataView *per completion*. Exact for any value under 2^53 (ids are a JS counter, sizes are byte counts). A zero-copy completion's body is never materialized at all. |
|
|
620
635
|
| **Reusable args scratch** (copy path only) | Rust copies args on submit, so one growable buffer serves every synchronous submission. Deliberately NOT used for secret-bearing ops (pbkdf2/argon2) — a shared buffer would keep plaintext passwords alive far longer than a per-call allocation — nor for zero-copy retries, which re-submit after an `await` and must own their memory. |
|
|
636
|
+
| **Bounded admission** (`CASTRUM_TASK_QUEUE_MAX`, default 4096) | A hard cap turns sustained overload into an explicit rejection (`2 = overloaded` → typed `OVERLOADED`) instead of unbounded queue/`inflight` growth. The bound is a length check inside the same lock that pushes, so it is one comparison on a path that already locks. |
|
|
621
637
|
|
|
622
638
|
---
|
|
623
639
|
|