Repository navigation
refactor: memoize the Couchbase connection as one shared promise - #237
Open
dex-the-ai wants to merge 1 commit into
Open
dex-the-ai wants to merge 1 commit into
dex-the-ai wants to merge 1 commit into
Conversation
getDatabase() is the single seam every API route uses to reach Couchbase, but its caching did not do what it claimed: - the "hot reload" cache read global.couchbase but never wrote it back, so every module re-evaluation in dev opened a fresh cluster connection; - the connection was cached only after `couchbase.connect` resolved, so concurrent first requests (and the import-time insertFTSIndexes call) each opened their own cluster. Memoize the in-flight connection promise on globalThis instead, and clear it on failure so a later request can retry. Callers are unchanged. Add lib/couchbase-connection.test.ts, which mocks the couchbase SDK and covers concurrent callers, module re-evaluation, and retry after failure. All three tests fail on main and pass here. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This branch was successfully deployed
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.
Summary
getDatabase()inlib/couchbase-connection.tsis how every API route reaches Couchbase. Its caching didn't work the way its comment says:global.couchbasebut never wrote to it. So innext dev, every module re-evaluation opened a new cluster connection, which is exactly what the comment said it prevents.couchbase.connect()resolved. The import-timeinsertFTSIndexes()call and any requests that arrived during the connect each opened their ownCluster.This PR caches the in-flight connection promise on
globalThisand clears it if connecting fails, so a later request can retry. Callers don't change:getDatabase()keeps the same signature and return shape. Two shallow layers are gone,createCouchbaseCluster()and the unused module-levelcachedDbConnection, which leaves a single, deeper connection module.Tests
lib/couchbase-connection.test.tsis new. It mocks thecouchbaseSDK, so no cluster is needed, and covers:couchbase.connectcall, same connection objectAll 3 tests fail on
mainand pass on this branch.Validation
npx vitest runagainst local Couchbase Server 8.0.3 withtravel-sample: 47/47 pass (44 existing + 3 new). Onmainthe same setup gives 44/44.npx tsc --noEmit: passnpx eslint lib/: passnpx next build: passprettier --check: the new test file passes.couchbase-connection.tsalready failed formatting onmain, and I left it unformatted to keep this diff small.🤖 Generated with Claude Code