Repository navigation
feat: add onidle and a Vercel attachDatabasePool adapter - #14
Merged
Merged
Conversation
Vercel Fluid Compute can suspend an instance while a pool connection is idle and its idle timer has not fired, leaving the connection open on the server. attachDatabasePool from @vercel/functions keeps the instance alive for idleTimeout after each pool release event, but recognises pools by duck typing and rejects postgres.js, which had no idle signal. onidle(connId) is called from move() each time a connection is filed in open and its idle timer starts, so it tracks exactly what Vercel's wait needs to cover. It is read at call time, so it can be set through sql.options.onidle after creation. The listen and subscribe pools set it to null: otherwise the never-idling LISTEN connection would keep re-arming the wait and report ids from a different pool. The call is guarded: a throw is rethrown in a microtask, as diagnostics_channel does for subscribers. Unguarded, a throw on the ReadyForQuery path reaches protocolError, and errored() has no query to reject, so the error vanishes; on release() it would throw at an unrelated call site. The existing callbacks have worse failure modes (#13). @steadybrew/postgresjs/vercel exports vercelPool(sql), which returns the pg-shaped object attachDatabasePool already accepts. It requires a numeric idle_timeout, since without one idle connections never close and keeping the instance alive does nothing, and it chains with an existing onidle. It lives in its own subpath so it can be deprecated if Vercel detects postgres.js natively. Based on porsager#1147. Closes porsager#1100. Tests: phase:idle-throw fails without the guard; the listen test fails without the listen override (onidle fired twice). Integration cases cover pool, queued, reserve, begin, late assignment and the adapter shape. Typechecked against @vercel/functions 3.9.11 from ESM and CJS consumers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds an
onidle(connId)option and a@steadybrew/postgresjs/verceladapter, so a pool can be passed toattachDatabasePoolfrom@vercel/functionson Fluid Compute. Addresses porsager#1100, based on porsager#1147.onidleis called frommove()each time a connection is filed inopenand its idle timer starts. It can be set after creation throughsql.options.onidle. The internal listen and subscribe pools do not inherit it.onidleis rethrown in a microtask, so it surfaces as an uncaught exception and never reaches the phase table. Unguarded, it is swallowed on the ReadyForQuery path. The existing callbacks are tracked in A throwing user callback breaks connection state #13.vercelPool(sql)returns the pg-shaped objectattachDatabasePoolalready detects. It requires a numericidle_timeoutand chains with an existingonidle.Local validation (Docker, Node 24.21.0, PostgreSQL 17.11):
phase:idle-throwfails without the guard, and the listen test fails without the listen override.@vercel/functions3.9.11 from ESM and CJS consumers.Deno and workerd run here in CI only. Behaviour on a real Vercel deployment has not been checked yet.