Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(cache): run Store reads and producer work on the scheduling …
…dispatcher

Rebuilds the cache's scheduling on the dispatcher prototype of
`@zingage/scheduling` (`createDispatcher`, `fifoBatches`,
`concurrencyLimit`, `Sharing`, `Item`), in place of `Batcher`,
`CollapsedTaskCreator` and the experimental `ConcurrencyLimiter`.

- `producerScheduler.ts`: a dispatcher (`fifoBatches(producerBatching)` for
  `produceMany`, no waiting set for `produce`, `concurrencyLimit` as
  admission when `maxConcurrentProducerCalls` is set) with a `Sharing` in
  front. One `canJoin` holds the sharing rule.
- `producerClient.ts`: each slot requests its own producer work, and a call
  with `autoBatch.producer: false` expedites it. The cohort starter and its
  whole-set wait are gone. Every slot reads with its own `Cache.get`.
- `Cache.ts`: one read dispatcher replaces the read `Batcher`, the limiter
  field and `runLimited`. `autoBatch: false` is submit-then-expedite.
- `utils.ts`: `runLimited` and the `limit` parameter are gone;
  `queueWaitMs` and `cancelWaitingOnAbort` are new.

Intended behaviour changes against PR 27 with the agreed API:

- `autoBatch: false` only skips the accumulation wait. The work is still
  split at `maxBatchSize`, and one Store or producer call can carry several
  callers' work that is ready in the same turn.
- A wrapped `getMany` with `autoBatch.producer: false` no longer waits for
  all of its cache reads before it produces.
- A request joins producer work that waits; it joins running work only
  without `joinInFlight: false` and inside the sharing window; and it joins
  work that its own public call started.
- `autoBatched` in diagnostics is false only when every request of the call
  was expedited. A Store call gets a caller's signal only when it is not
  automatic and all of its reads carry that signal.
- Batched calls start in a microtask after their work is ready. Queue waits
  are measured, so never exactly 0. `preserveAsyncContext` binds one
  context per accumulation, and a `close()` runs its calls in the closer's
  context. The "enqueue called after close" warning is gone.

Tests that asserted the old unsplit or synchronous behaviour now assert the
new behaviour. The scheduler tests of `wrapSingleProducer.test.ts` and
`wrapBulkProducer.test.ts` moved into `producerScheduler.test.ts`, because
the per-wrapper scheduler functions no longer exist.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
  • Loading branch information
ethanresnick and claude committed Nov 23, 2026
commit 111f8129b8d85793e14f7790cec33bec504766c5
39 changes: 28 additions & 11 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Dates below are npm publication dates (UTC). Version 4.0.0 is unreleased.
`naiveGetMany`, because a Store no longer needs a fallback `getMany`.
- `Cache.get` and `Cache.getMany` join automatic accumulation by default, so
reads from many calls share Store calls, and the batch limit can split a
`getMany` set. Pass `{ autoBatch: false }` to either method to send the
call's requests to the Store as one call of their own, whatever the batch
limit.
`getMany` set. Pass `{ autoBatch: false }` to either method to skip the
accumulation wait. The batch limit still splits a large set, and a Store
call can still carry other callers' reads that are ready at the same time.
- `Cache.getMany` returns positional results containing successes or
`Error` values. Handle each slot or use `throwIfAnyError` to reject partial
failure.
Expand Down Expand Up @@ -59,10 +59,26 @@ Dates below are npm publication dates (UTC). Version 4.0.0 is unreleased.
- Separate Store-read and producer queues automatically batch bulk-capable
operations. The default maximum batch size is 100 for Store reads and 20
for producers. By default, batches run concurrently, with no limit.
Single-only capabilities dispatch promptly. Wrapper calls can disable Store and
producer accumulation independently with `autoBatch` options. The batch
limit applies only to accumulated work: a direct call reaches the Store or
producer as one invocation.
Single-only capabilities dispatch promptly. Wrapper calls can skip the Store
and the producer accumulation wait independently with `autoBatch` options.
The batch limit applies to every Store and producer call, so a call with
`autoBatch: false` is not guaranteed to be one invocation, and its work can
share an invocation with other callers' work that is ready at the same
time. A wrapped `getMany` with `autoBatch.producer: false` sends each
request's producer work as soon as that request's cache read settles. It
does not wait for the whole set.
- Batching, sharing and call limits are built on the dispatcher of
`@zingage/scheduling` (`createDispatcher`, `Sharing`, `concurrencyLimit`).
A batched Store or producer call now starts in a microtask after its work
becomes ready, not synchronously inside the schedule callback or `close()`.
With `preserveAsyncContext`, every Store or producer call of one
accumulation runs in the async context of the request that opened the
accumulation, and a call that `close()` starts runs in the closer's
context. Queue waits in diagnostics are measured times, so a call that
nothing held back reports a wait close to zero, not exactly zero. Work
with `autoBatch: false` still calls `batchScheduleFn`, whose callback then
finds nothing to do, because the work is submitted before it is expedited.
Work that is submitted after `close()` no longer logs a warning.
- A package contract violation in one producer result slot, or in one
`bulkProducerByIdType` slice, rejects only the callers of the affected
requests. Automatic batching merges unrelated callers into one invocation,
Expand All @@ -73,15 +89,16 @@ Dates below are npm publication dates (UTC). Version 4.0.0 is unreleased.
- `maxConcurrentStoreReads` (Cache) and `maxConcurrentProducerCalls`
(producer clients) limit the Store read calls and the producer calls that
run at one time, for single and bulk capabilities. Work that waits for a
slot stays queued: other requests can join it, it can be cancelled, an
automatic batch keeps filling, and the wait counts as queue time in
diagnostics.
slot stays queued: other requests can join it, it can be cancelled, the
call that gets the slot takes all the work that is ready then, up to the
batch limit, and the wait counts as queue time in diagnostics.
- The sharing window of `collapseOverlappingRequestsTime` now runs from the
start of the producer call, not from the request that queued the work.
Work that still waits, in a batch or for a slot, can always be joined.
- Per-call `joinInFlight: false` keeps a wrapped read from joining a producer
call that has already started, for reads that must see a change the caller
just made.
just made. The call's own requests with the same key still join the work
that the call started.
- A wrapped `get` no longer runs through `getMany`: it runs the same per-slot
steps for its one request, without the per-call arrays.
- `Cache.get` and `Cache.getMany` also find the resources of every
Expand Down
Loading
Loading