项目概述
本项目实现一个面向 notification delivery 的完整系统,核心要解决的问题是 expanding a single event into per-channel deliveries without a slow channel blocking the fast ones。整体由 Event receiver、Channel router、Delivery queue、Rate per channel、Dead letter store 五个模块组成,每个模块单一职责、可独立测试,最后通过集成测试验证端到端正确性。设计上把 a slow email channel that backs up its queue and delays notifications that should have been instant on push 作为第一类需求先行处理,而非事后再补。
架构与关键决策
数据模型为 a fanout that turns one event into many deliveries, each queued per channel with its own rate limit and retry policy。Event receiver 定义其余阶段共同依赖的数据形状并在边界处校验输入;Channel router 承载 notification delivery 的正确性逻辑;Delivery queue 专门处理 a slow email channel that backs up its queue and delays notifications that should have been instant on push,在失败路径上给出明确的拒绝、重试或降级策略;Rate per channel 提供可观测性,每次操作输出带关联 id 的结构化事件;Dead letter store 在过载时按既定方式降级,避免过载演变为数据损坏。配置与代码分离,使风险变更可经 diff 审阅。
实施与测试
按五个阶段依次实现 Event receiver、Channel router、Delivery queue、Rate per channel、Dead letter store,每阶段配套聚焦单测。集成测试组合各模块并驱动真实 notification delivery 场景:先验证正常路径,再刻意触发 a slow email channel that backs up its queue and delays notifications that should have been instant on push 并断言系统降级而非损坏。所有依赖均在进程内或被桩替换,用例在 CI 中确定可复现。
限制与后续
- 单区域设计,多区域复制超出范围。
- Event receiver 默认进程内,未水平共享。
- 无鉴权层,假定部署在强制身份的边缘之后。
代码示例
# setup for notification-fanout-service
pnpm init
pnpm add fastify redis pino
pnpm add -D typescript tsx vitest
notification_fanout_service/
├── src/index.ts
├── src/event-receiver.ts
├── src/channel-router.ts
├── tests/notification_fanout_service.test.ts
└── package.json
// Event receiver: notification delivery core
export function eventreceiver(input: EventreceiverInput): EventreceiverResult {
// Build the event receiver that anchors notification delivery and defines the core data shape.
return processEventreceiver(input);
}
// Channel router — handles notification delivery Add the channel router so
export class Channelrouter {
constructor(private opts: ChannelrouterOptions) {}
async run(ctx: Context): Promise<ChannelrouterOutput> {
return this.execute(ctx);
}
}
// Delivery queue: notification delivery stage
export const deliveryqueue = (next: Next) => async (req: Request) => {
const out = await transform(req);
return next(out);
};
// Rate per channel for notification delivery
export async function rateperchannel(stream: AsyncIterable<Chunk>): Promise<Aggregate> {
let acc = init();
for await (const c of stream) acc = merge(acc, c);
return acc;
}
// Dead letter store: notification delivery finalization
export function deadletterstore(state: State): Result {
validate(state);
return finalize(state);
}
# run the notification delivery test suite
pnpm vitest run