fix(picloud): F-P-003 PICLOUD_DB_MAX_CONNECTIONS env knob, default 32 (matches gate)

init_db hard-coded max_connections(10). The execution gate
(ExecutionGate, PICLOUD_MAX_CONCURRENT_EXECUTIONS default 32) lets 32
script executions run concurrently, each doing multiple sequential
DB calls — plus the dispatcher tick every 100ms, the cron tick, three
GC sweeps, and the auth middleware running per request all draw from
the same pool. With max=10 vs gate=32, pool starvation surfaces as
acquire_timeout errors and tail-latency spikes under load.

- DEFAULT_DB_MAX_CONNECTIONS = 32 (matches gate default).
- Env knob: PICLOUD_DB_MAX_CONNECTIONS overrides; invalid/zero → default.
- Documented in CLAUDE.md runtime config table.

AUDIT.md anchor: F-P-003.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
MechaCat02
2026-06-07 20:03:56 +02:00
parent c63c5cc275
commit 47ea239eb6
2 changed files with 16 additions and 1 deletions

View File

@@ -588,11 +588,25 @@ pub async fn build_app(
.layer(TraceLayer::new_for_http()))
}
/// Default Postgres pool size. Scaled to match the execution gate
/// default (32 concurrent script executions) so a hot pool doesn't
/// starve the data plane. Override with `PICLOUD_DB_MAX_CONNECTIONS`.
/// Sizing relationship: each script run does multiple sequential DB
/// calls (script resolve, SDK calls, log sink, outbox emit) plus the
/// dispatcher tick / cron tick / GC sweeps / auth middleware all draw
/// from the same pool, so this is a floor not a ceiling.
pub const DEFAULT_DB_MAX_CONNECTIONS: u32 = 32;
/// Open a Postgres pool with the binary's standard timeout settings.
/// Exposed so tests reach for the same configuration when needed.
pub async fn init_db(url: &str) -> anyhow::Result<PgPool> {
let max_connections = std::env::var("PICLOUD_DB_MAX_CONNECTIONS")
.ok()
.and_then(|v| v.trim().parse::<u32>().ok())
.filter(|n| *n > 0)
.unwrap_or(DEFAULT_DB_MAX_CONNECTIONS);
let pool = PgPoolOptions::new()
.max_connections(10)
.max_connections(max_connections)
.acquire_timeout(Duration::from_secs(5))
.connect(url)
.await?;