@ultimat3/notify 31.13.1 → 31.15.0
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/CLAUDE.md +3 -1
- package/package.json +8 -8
- package/src/fanout-pool.ts +26 -0
- package/src/fanout-walk.ts +8 -0
- package/src/fanout.ts +50 -13
- package/src/inbox-pg.ts +23 -4
- package/src/ledger.ts +10 -2
package/CLAUDE.md
CHANGED
|
@@ -52,7 +52,9 @@ A job body runs **before** its checkpoint lands, so both layers are load-bearing
|
|
|
52
52
|
re-reads). A retry replays a completed chunk from the step store; a chunk that failed part-way
|
|
53
53
|
re-runs whole, and layer 2 answers `sent` for whoever it already reached. Preferences are read
|
|
54
54
|
once per chunk (`PreferenceStore.allowsMany`, required since 31.0.0): 10k recipients is 20 reads
|
|
55
|
-
and 20 step rows a channel, not 20k (`fanout-chunks.test.ts`).
|
|
55
|
+
and 20 step rows a channel, not 20k (`fanout-chunks.test.ts`). Inside a chunk,
|
|
56
|
+
`NOTIFY_DELIVERY_WIDTH` (16) recipients are in flight; a failed one is thrown only after its
|
|
57
|
+
neighbours were tried (first retryable failure wins). A digest reads preferences per
|
|
56
58
|
chunk and sends per chunk (`digest-send:<channel>:chunk-<n>`, ledger-fenced the same way), but
|
|
57
59
|
its `digest:` append and `digest-drain:` steps stay ONE PER RECIPIENT: a chunked append re-runs
|
|
58
60
|
appends whose joined window may be drained by then (below), and a chunked drain widens the
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ultimat3/notify",
|
|
3
|
-
"version": "31.
|
|
3
|
+
"version": "31.15.0",
|
|
4
4
|
"description": "Notifications: one declaration, many channels — fan-out, preference gate, digest window, delivery ledger, in-app inbox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ultimate",
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"test": "bun test"
|
|
47
47
|
},
|
|
48
48
|
"dependencies": {
|
|
49
|
-
"@ultimat3/action": "31.
|
|
50
|
-
"@ultimat3/core": "31.
|
|
51
|
-
"@ultimat3/i18n": "31.
|
|
52
|
-
"@ultimat3/jobs": "31.
|
|
53
|
-
"@ultimat3/policy": "31.
|
|
54
|
-
"@ultimat3/schema": "31.
|
|
55
|
-
"@ultimat3/time": "31.
|
|
49
|
+
"@ultimat3/action": "31.15.0",
|
|
50
|
+
"@ultimat3/core": "31.15.0",
|
|
51
|
+
"@ultimat3/i18n": "31.15.0",
|
|
52
|
+
"@ultimat3/jobs": "31.15.0",
|
|
53
|
+
"@ultimat3/policy": "31.15.0",
|
|
54
|
+
"@ultimat3/schema": "31.15.0",
|
|
55
|
+
"@ultimat3/time": "31.15.0"
|
|
56
56
|
}
|
|
57
57
|
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// One chunk's sends at a bounded width that NEVER answers over a send in flight: on the run's
|
|
2
|
+
// abort (a drain) nothing new is claimed, and every claimed send settles its ledger row before the
|
|
3
|
+
// step answers. `mapBounded` rejects at once, which left rows `sending` behind an exiting process.
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `run` over `items`, `width` at a time, in input order. An item `run` never started (the signal
|
|
7
|
+
* aborted first) has no outcome; the caller asks `signal.aborted` and fails the step for its retry.
|
|
8
|
+
* `run` must not throw: each recipient's failure is its own outcome.
|
|
9
|
+
*/
|
|
10
|
+
export async function settledPool<T, R>(
|
|
11
|
+
items: readonly T[],
|
|
12
|
+
width: number,
|
|
13
|
+
signal: AbortSignal,
|
|
14
|
+
run: (item: T, signal: AbortSignal) => Promise<R>,
|
|
15
|
+
): Promise<readonly R[]> {
|
|
16
|
+
const outcomes: R[] = [];
|
|
17
|
+
let next = 0;
|
|
18
|
+
const worker = async (): Promise<void> => {
|
|
19
|
+
// Checked before each claim: an abort stops the pulling, never a send already started.
|
|
20
|
+
for (let index = next++; index < items.length && !signal.aborted; index = next++) {
|
|
21
|
+
outcomes[index] = await run(items[index] as T, signal);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
await Promise.allSettled(Array.from({ length: Math.min(width, items.length) }, worker));
|
|
25
|
+
return outcomes;
|
|
26
|
+
}
|
package/src/fanout-walk.ts
CHANGED
|
@@ -47,3 +47,11 @@ export function* chunksOf<T>(items: readonly T[]): Generator<readonly T[]> {
|
|
|
47
47
|
yield items.slice(start, start + NOTIFY_CHUNK_SIZE);
|
|
48
48
|
}
|
|
49
49
|
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Recipients of ONE chunk in flight at once — ledger claim, provider call, settle. Serial, a 10k
|
|
53
|
+
* audience was 20k database round trips and 10k provider calls back to back (a 50 ms provider:
|
|
54
|
+
* 100 recipients took 5 s). 16 stays under a default connection pool and a provider's burst limit;
|
|
55
|
+
* the chunk is still ONE step, so at-least-once is what it was.
|
|
56
|
+
*/
|
|
57
|
+
export const NOTIFY_DELIVERY_WIDTH = 16;
|
package/src/fanout.ts
CHANGED
|
@@ -6,14 +6,16 @@
|
|
|
6
6
|
// recipient 40 and replays the other 39 from the step store in microseconds — the same guarantee a
|
|
7
7
|
// job-per-recipient buys, without N queue rows, N idempotency keys and N manifest entries.
|
|
8
8
|
|
|
9
|
+
import { classifyThrown } from '@ultimat3/core';
|
|
9
10
|
import type { JobRunArgs } from '@ultimat3/jobs';
|
|
10
11
|
import { attemptDelivery } from './attempt';
|
|
11
12
|
import type { NotifyChannel } from './channel';
|
|
12
13
|
import { isBulkChannel } from './channel';
|
|
13
14
|
import { NotifyFanoutTooWideError } from './errors';
|
|
14
15
|
import { flushDigest } from './fanout-digest';
|
|
16
|
+
import { settledPool } from './fanout-pool';
|
|
15
17
|
import type { Tally, Walk } from './fanout-walk';
|
|
16
|
-
import { chunksOf, countSend } from './fanout-walk';
|
|
18
|
+
import { chunksOf, countSend, NOTIFY_DELIVERY_WIDTH } from './fanout-walk';
|
|
17
19
|
import type { NotifyEvent, Recipient } from './notification';
|
|
18
20
|
import type { NotifyPayload, NotifyPlan, NotifyReport, ResolvedDelivery } from './plan';
|
|
19
21
|
import { notifyStores } from './stores';
|
|
@@ -148,10 +150,33 @@ async function deliverOne<Params>(
|
|
|
148
150
|
index += 1;
|
|
149
151
|
const allowed = await allowedBy(walk, channel.name, chunk);
|
|
150
152
|
if (allowed.length === 0) continue;
|
|
151
|
-
const counts = await walk.step.run(name,
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
153
|
+
const counts = await walk.step.run(name, (signal) => deliverChunk(walk, one, allowed, signal));
|
|
154
|
+
tally.delivered += counts.delivered;
|
|
155
|
+
tally.replayed += counts.replayed;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* One chunk's recipients, `NOTIFY_DELIVERY_WIDTH` at a time, each with its own ledger claim. A
|
|
161
|
+
* recipient's failure is held, never thrown at once: its neighbours still go out (and settle
|
|
162
|
+
* `sent`), so the retry re-sends the failed one alone. Then the step fails with the first
|
|
163
|
+
* RETRYABLE failure in audience order when there is one — a terminal one first would end the job
|
|
164
|
+
* with a recipient a retry could still have reached — else the first.
|
|
165
|
+
*/
|
|
166
|
+
async function deliverChunk<Params>(
|
|
167
|
+
walk: Walk<Params>,
|
|
168
|
+
channel: NotifyChannel<Params>,
|
|
169
|
+
allowed: readonly Recipient[],
|
|
170
|
+
signal: AbortSignal,
|
|
171
|
+
): Promise<{ delivered: number; replayed: number }> {
|
|
172
|
+
const { event, ctx } = walk;
|
|
173
|
+
const outcomes = await settledPool(
|
|
174
|
+
allowed,
|
|
175
|
+
NOTIFY_DELIVERY_WIDTH,
|
|
176
|
+
signal,
|
|
177
|
+
async (recipient, abort): Promise<{ readonly failed: unknown } | boolean> => {
|
|
178
|
+
try {
|
|
179
|
+
return await attemptDelivery(
|
|
155
180
|
{
|
|
156
181
|
ledger: notifyStores().ledger,
|
|
157
182
|
claim: {
|
|
@@ -164,18 +189,30 @@ async function deliverOne<Params>(
|
|
|
164
189
|
channel: channel.name,
|
|
165
190
|
recipients: 1,
|
|
166
191
|
ctx,
|
|
167
|
-
send: (
|
|
192
|
+
send: (stop) =>
|
|
193
|
+
channel.deliver({ ctx, recipient, event, batch: [event], signal: stop }),
|
|
168
194
|
},
|
|
169
|
-
|
|
195
|
+
abort,
|
|
170
196
|
);
|
|
171
|
-
|
|
172
|
-
|
|
197
|
+
} catch (error) {
|
|
198
|
+
return { failed: error };
|
|
173
199
|
}
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
200
|
+
},
|
|
201
|
+
);
|
|
202
|
+
// A drain: every claimed send has settled; the rest were never claimed. The retry re-runs the
|
|
203
|
+
// chunk, and the ledger answers `sent` for every recipient already reached.
|
|
204
|
+
if (signal.aborted) throw signal.reason;
|
|
205
|
+
const answer = { delivered: 0, replayed: 0 };
|
|
206
|
+
const failures: unknown[] = [];
|
|
207
|
+
for (const outcome of outcomes) {
|
|
208
|
+
if (outcome === true) answer.delivered += 1;
|
|
209
|
+
else if (outcome === false) answer.replayed += 1;
|
|
210
|
+
else failures.push(outcome.failed);
|
|
211
|
+
}
|
|
212
|
+
if (failures.length > 0) {
|
|
213
|
+
throw failures.find((error) => classifyThrown(error) !== 'terminal') ?? failures[0];
|
|
178
214
|
}
|
|
215
|
+
return answer;
|
|
179
216
|
}
|
|
180
217
|
|
|
181
218
|
/**
|
package/src/inbox-pg.ts
CHANGED
|
@@ -54,6 +54,12 @@ where recipient = $2 and notifier = $3 and key = $4
|
|
|
54
54
|
and not exists (select 1 from inserted)
|
|
55
55
|
`;
|
|
56
56
|
|
|
57
|
+
/** `add`'s second look, in its own statement — see `postgresInboxStore().add`. */
|
|
58
|
+
export const SQL_NOTIFY_INBOX_FIND = `
|
|
59
|
+
select ${COLUMNS} from x_notify_inbox
|
|
60
|
+
where recipient = $1 and notifier = $2 and key = $3
|
|
61
|
+
`;
|
|
62
|
+
|
|
57
63
|
/**
|
|
58
64
|
* Newest first, `(created_at desc, notifier, key)` — unique within a recipient by the table's own
|
|
59
65
|
* `unique (recipient, notifier, key)`, so the order is total and a bounded page cannot drop or
|
|
@@ -174,10 +180,23 @@ export function postgresInboxStore(options: PostgresInboxStoreOptions): Postgres
|
|
|
174
180
|
write.createdAt,
|
|
175
181
|
]);
|
|
176
182
|
const row = rows[0];
|
|
177
|
-
|
|
178
|
-
//
|
|
179
|
-
//
|
|
180
|
-
|
|
183
|
+
if (row !== undefined) return toRow(row);
|
|
184
|
+
// Nothing back is NOT a deleted row. Under READ COMMITTED a concurrent writer of the same
|
|
185
|
+
// (recipient, notifier, key) that commits while this insert waits on its lock makes the
|
|
186
|
+
// insert do nothing — and the `union all`'s select runs on the snapshot taken BEFORE that
|
|
187
|
+
// commit, so it cannot see the row either. A fresh statement has a fresh snapshot; an id
|
|
188
|
+
// made up here was a row that was never written (`inbox-pg.live.test.ts`).
|
|
189
|
+
const existing = await executor.query<InboxDbRow>(SQL_NOTIFY_INBOX_FIND, [
|
|
190
|
+
write.recipient,
|
|
191
|
+
write.notifier,
|
|
192
|
+
write.key,
|
|
193
|
+
]);
|
|
194
|
+
const found = existing[0];
|
|
195
|
+
// Still nothing: the conflicting row was deleted in between (a purge), which no caller can
|
|
196
|
+
// repair — the write reads as having happened, as the notification itself did.
|
|
197
|
+
return found === undefined
|
|
198
|
+
? { id: newId(), ...write, seenAt: null, readAt: null }
|
|
199
|
+
: toRow(found);
|
|
181
200
|
},
|
|
182
201
|
async list(query) {
|
|
183
202
|
// Screened here and not left to Postgres: this number is bound straight into `limit $3`, and
|
package/src/ledger.ts
CHANGED
|
@@ -143,6 +143,14 @@ export function memoryDeliveryLedger(
|
|
|
143
143
|
}
|
|
144
144
|
};
|
|
145
145
|
|
|
146
|
+
// Delete, then set: `Map.set` on a key it holds keeps the key's OLD place, so the row just
|
|
147
|
+
// re-claimed `sending` (or just settled `sent`) stayed first in line for `evict` — and a row
|
|
148
|
+
// evicted mid-flight is a claim the next attempt never sees: a concurrent second send.
|
|
149
|
+
const put = (id: string, row: { readonly record: DeliveryRecord; readonly claimId: string }) => {
|
|
150
|
+
rows.delete(id);
|
|
151
|
+
rows.set(id, row);
|
|
152
|
+
};
|
|
153
|
+
|
|
146
154
|
return {
|
|
147
155
|
get size(): number {
|
|
148
156
|
return rows.size;
|
|
@@ -166,7 +174,7 @@ export function memoryDeliveryLedger(
|
|
|
166
174
|
attempts: (existing?.attempts ?? 0) + 1,
|
|
167
175
|
at,
|
|
168
176
|
};
|
|
169
|
-
|
|
177
|
+
put(id, { record, claimId });
|
|
170
178
|
evict();
|
|
171
179
|
return Promise.resolve({ outcome: 'owned', claimId });
|
|
172
180
|
},
|
|
@@ -178,7 +186,7 @@ export function memoryDeliveryLedger(
|
|
|
178
186
|
if (existing?.claimId !== claimId || existing.record.status !== 'sending') {
|
|
179
187
|
return Promise.resolve(false);
|
|
180
188
|
}
|
|
181
|
-
|
|
189
|
+
put(id, { claimId, record: { ...existing.record, status, at } });
|
|
182
190
|
return Promise.resolve(true);
|
|
183
191
|
},
|
|
184
192
|
find(claim) {
|