← 中文资源

简体中文版本

构建连接池管理器

构建连接池管理器:完整覆盖 database connection pooling,含 Idle queue、Health probe 与测试的端到端实现。

阅读英文原文

项目概述

本项目实现一个面向 database connection pooling 的完整系统,核心要解决的问题是 reusing expensive connections across concurrent requests without leaking them or starving waiters。整体由 Idle queue、Health probe、Acquire timeout、Leak detector、Pool reaper 五个模块组成,每个模块单一职责、可独立测试,最后通过集成测试验证端到端正确性。设计上把 a request that holds a connection across an await that never resolves and exhausts the pool for everyone else 作为第一类需求先行处理,而非事后再补。

架构与关键决策

数据模型为 a bounded set of connection objects with a state machine of idle, busy, and draining, plus a FIFO of waiting acquirers。Idle queue 定义其余阶段共同依赖的数据形状并在边界处校验输入;Health probe 承载 database connection pooling 的正确性逻辑;Acquire timeout 专门处理 a request that holds a connection across an await that never resolves and exhausts the pool for everyone else,在失败路径上给出明确的拒绝、重试或降级策略;Leak detector 提供可观测性,每次操作输出带关联 id 的结构化事件;Pool reaper 在过载时按既定方式降级,避免过载演变为数据损坏。配置与代码分离,使风险变更可经 diff 审阅。

实施与测试

按五个阶段依次实现 Idle queue、Health probe、Acquire timeout、Leak detector、Pool reaper,每阶段配套聚焦单测。集成测试组合各模块并驱动真实 database connection pooling 场景:先验证正常路径,再刻意触发 a request that holds a connection across an await that never resolves and exhausts the pool for everyone else 并断言系统降级而非损坏。所有依赖均在进程内或被桩替换,用例在 CI 中确定可复现。

限制与后续

  • 单区域设计,多区域复制超出范围。
  • Idle queue 默认进程内,未水平共享。
  • 无鉴权层,假定部署在强制身份的边缘之后。

代码示例

# setup for connection-pool-manager
pnpm init
pnpm add pg pino
pnpm add -D typescript tsx vitest
connection_pool_manager/
├── src/index.ts
├── src/idle-queue.ts
├── src/health-probe.ts
├── tests/connection_pool_manager.test.ts
└── package.json
// Idle queue: database connection pooling core
export function idlequeue(input: IdlequeueInput): IdlequeueResult {
  // Build the idle queue that anchors database connection pooling and defines the core data shape.
  return processIdlequeue(input);
}
// Health probe — handles database connection pooling Add the health probe so
export class Healthprobe {
  constructor(private opts: HealthprobeOptions) {}
  async run(ctx: Context): Promise<HealthprobeOutput> {
    return this.execute(ctx);
  }
}
// Acquire timeout: database connection pooling stage
export const acquiretimeout = (next: Next) => async (req: Request) => {
  const out = await transform(req);
  return next(out);
};
// Leak detector for database connection pooling
export async function leakdetector(stream: AsyncIterable<Chunk>): Promise<Aggregate> {
  let acc = init();
  for await (const c of stream) acc = merge(acc, c);
  return acc;
}
// Pool reaper: database connection pooling finalization
export function poolreaper(state: State): Result {
  validate(state);
  return finalize(state);
}
# run the database connection pooling test suite
pnpm vitest run