-
Notifications
You must be signed in to change notification settings - Fork 968
feat(aggregator): Records Jetstream DO with PDS-bound ingestor #972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
75dc844
feat(aggregator): Records Jetstream DO with PDS-bound ingestor
ascorbic 9c528cd
fix(aggregator): adversarial review fixes for Jetstream DO
ascorbic 81ae4f4
fix(aggregator): C1 fix was incomplete — switch to closed-signal race
ascorbic fa8da07
fix(aggregator): copilot review fixes
ascorbic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /** | ||
| * Jetstream client abstraction. | ||
| * | ||
| * Production wraps `@atcute/jetstream`'s `JetstreamSubscription`. Tests bind | ||
| * `MockJetstream` from `@emdash-cms/atproto-test-utils`. The ingestor only | ||
| * depends on this interface, so the same code path runs in both worlds. | ||
| * | ||
| * The shape mirrors the subset of `JetstreamSubscription` we actually use: | ||
| * - async-iterable of commit events (we don't process identity/account | ||
| * events today), | ||
| * - a `cursor` getter exposing the time_us of the most recent event the | ||
| * iterator has yielded — used to persist the cursor for reconnection, | ||
| * - an explicit close. | ||
| * | ||
| * Open question we may revisit: real Jetstream emits identity + account | ||
| * events alongside commits. The ingestor narrows to commits today; if we | ||
| * grow to care about identity events for handle changes, widen the event | ||
| * type here and update the consumer. | ||
| */ | ||
|
|
||
| import { JetstreamSubscription } from "@atcute/jetstream"; | ||
|
|
||
| export interface JetstreamCommitEvent { | ||
| did: `did:${string}:${string}`; | ||
| time_us: number; | ||
| kind: "commit"; | ||
| commit: | ||
| | { | ||
| rev: string; | ||
| collection: string; | ||
| rkey: string; | ||
| operation: "create" | "update"; | ||
| cid: string; | ||
| record: Record<string, unknown>; | ||
| } | ||
| | { | ||
| rev: string; | ||
| collection: string; | ||
| rkey: string; | ||
| operation: "delete"; | ||
| }; | ||
| } | ||
|
|
||
| export interface JetstreamSubscribeOptions { | ||
| wantedCollections: readonly string[]; | ||
| cursor?: number; | ||
| } | ||
|
|
||
| export interface JetstreamSubscriptionHandle extends AsyncIterable<JetstreamCommitEvent> { | ||
| readonly cursor: number; | ||
| close(): void; | ||
| } | ||
|
|
||
| export interface JetstreamClient { | ||
| subscribe(opts: JetstreamSubscribeOptions): JetstreamSubscriptionHandle; | ||
| } | ||
|
|
||
| /** | ||
| * Production client backed by `@atcute/jetstream`. Filters non-commit events | ||
| * before yielding, so the ingestor doesn't have to switch on `kind` every | ||
| * iteration. | ||
| */ | ||
| export class RealJetstreamClient implements JetstreamClient { | ||
| constructor(private readonly url: string) {} | ||
|
|
||
| subscribe(opts: JetstreamSubscribeOptions): JetstreamSubscriptionHandle { | ||
| const sub = new JetstreamSubscription({ | ||
| url: this.url, | ||
| wantedCollections: [...opts.wantedCollections], | ||
| ...(opts.cursor !== undefined ? { cursor: opts.cursor } : {}), | ||
| }); | ||
| return wrapAtcuteSubscription(sub); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Minimum shape `wrapAtcuteSubscription` needs from its input: a `cursor` | ||
| * getter and an async iterable of events with a `kind` discriminator. Both | ||
| * `JetstreamSubscription` (production) and a stub-with-never-resolving-next | ||
| * (the C2 regression test) satisfy this without casts. | ||
| */ | ||
| export interface RawJetstreamSubscription<E extends { kind: string }> extends AsyncIterable<E> { | ||
| readonly cursor: number; | ||
| } | ||
|
|
||
| /** | ||
| * Exported so tests can drive the wrapper against a stub subscription with a | ||
| * never-resolving `next()` and verify that `close()` actually cancels the | ||
| * pending await. Production callers should construct via `RealJetstreamClient`. | ||
| */ | ||
| export function wrapAtcuteSubscription<E extends { kind: string }>( | ||
| sub: RawJetstreamSubscription<E>, | ||
| ): JetstreamSubscriptionHandle { | ||
| // Hoist the inner iterator so `close()` can reach it from outside the | ||
| // iterator factory. | ||
| let inner: AsyncIterator<E> | null = null; | ||
| // Shutdown signal raced against `inner.next()`. We can't rely on | ||
| // `inner.return()` to unblock a pending `next()` — `@mary-ext/event-iterator` | ||
| // drops its resolver on `return()` without invoking it (lib/index.ts:55-67), | ||
| // so a quiescent stream's pending `next()` Promise leaks. Racing against | ||
| // `closedSignal` lets the consumer wake regardless of the inner iterator's | ||
| // behaviour. The orphaned `it.next()` Promise is one of: | ||
| // - resolved later when an event arrives (harmless, garbage-collected). | ||
| // - leaked forever if Jetstream stays quiescent (no value held; only | ||
| // the Promise object is GC-rooted by the inner iterator's #resolve). | ||
| let resolveClosed: (() => void) | null = null; | ||
| const closedSignal = new Promise<void>((resolve) => { | ||
| resolveClosed = resolve; | ||
| }); | ||
| const fireClosed = () => { | ||
| if (resolveClosed) { | ||
| const r = resolveClosed; | ||
| resolveClosed = null; | ||
| r(); | ||
| } | ||
| }; | ||
|
|
||
| return { | ||
| get cursor() { | ||
| return sub.cursor; | ||
| }, | ||
| close: () => { | ||
| fireClosed(); | ||
| // `.catch` swallows rejections from the inner iterator's cleanup | ||
| // (an EventIterator's `return()` shouldn't reject, but a future | ||
| // implementation could). Without this, a rejection here would | ||
| // surface as an unhandled-promise warning in workerd. | ||
| inner?.return?.()?.catch(() => {}); | ||
| }, | ||
| [Symbol.asyncIterator](): AsyncIterator<JetstreamCommitEvent> { | ||
| inner ??= sub[Symbol.asyncIterator](); | ||
| const it = inner; | ||
| return { | ||
| async next(): Promise<IteratorResult<JetstreamCommitEvent>> { | ||
| for (;;) { | ||
| const result = await Promise.race([ | ||
| it.next(), | ||
| closedSignal.then((): IteratorResult<E> => ({ value: undefined, done: true })), | ||
| ]); | ||
| if (result.done) return { value: undefined, done: true }; | ||
| const event = result.value; | ||
| if (event.kind === "commit") { | ||
| // Cast within the function: by `kind === "commit"` we | ||
| // know the event is a commit; the generic `E` is too | ||
| // wide for the compiler to narrow automatically. | ||
| return { value: event as unknown as JetstreamCommitEvent, done: false }; | ||
| } | ||
| // Skip identity/account events; loop until next commit. | ||
| } | ||
| }, | ||
| async return(): Promise<IteratorResult<JetstreamCommitEvent>> { | ||
| fireClosed(); | ||
| await it.return?.(); | ||
| return { value: undefined, done: true }; | ||
| }, | ||
| }; | ||
| }, | ||
| }; | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in fa8da07 —
/_admin/startnow fires the DO fetch viactx.waitUntiland returns 204 with no body. Probing callers can't tell whether the DO was already running, just woke up, or is mid-startup. The DO's status body is still returned by its ownfetchhandler but is consumed only internally (cron pump fires-and-forgets too).