Glitch sleep/wake cycle vs. Node.js database connection pool: preventing intermittent exhaustion
0 reputation · 08 May 2024, 10:20 UTC
I'm running a Node.js project on Glitch that connects to an external database through a connection pool. Under low traffic, the project sleeps after about 5 minutes without HTTP requests, and I suspect this sleep/wake cycle is behind intermittent pool exhaustion: after several wake cycles, the pool appears to hit its limit even though actual query volume is low.
My understanding is that sleep terminates open sockets, but I'm unsure whether pooled connections are always cleaned up, especially TLS-wrapped ones, and whether stale connections can remain counted on the database server side. Glitch doesn't seem to expose a dedicated before-sleep hook, so the options appear to be listening for process events like SIGTERM to drain the pool, or tuning settings such as idleTimeoutMillis below the sleep interval — but it's unclear how reliable either approach is across drivers.
How should a pool be configured or shut down so connections are fully released before Glitch puts the project to sleep? Is there a reliable signal or event to hook into, and what trade-offs does aggressive idle-timeout tuning introduce for wake-up latency?