Skip to content

Add Semaphore utility to help implement connection pools#906

Open
simolus3 wants to merge 3 commits intomainfrom
add-semaphore
Open

Add Semaphore utility to help implement connection pools#906
simolus3 wants to merge 3 commits intomainfrom
add-semaphore

Conversation

@simolus3
Copy link
Contributor

We currently have two manual connection pool implementations in this repository: @powersync/node and @powersync/op-sqlite. Both implementations share a similar architecture:

  1. We use a Mutex to guard access to the write connection.
  2. For each read connection, we store a busy flag and have a queue of readers backed by an array.

For VFS implementations that allow concurrent access on the web, we'll soon have a third connection pool implementation. Adding the third copy-paste is always a good signal to refactor, so this PR adds utilities for implementing connection pools to @powersync/common.

The Semaphore class wraps an array of inner elements and can give out leases wrapping a single or multiple such elements. These are all acquired atomically, and requests on semaphores can be aborted individually. This is pretty much the same implementation we already had for pools, except that:

  1. We use a slightly more efficient queue implementation by avoiding the O(n) Array.shift() when removing elements.
  2. We also use a queue for non-busy connections which allows obtaining a lease without iterating through all of them.
  3. By using our Semaphore implementation, we get to abort readers (which both @powersync/node and @powersync/op-sqlite didn't really support before).
  4. close() and refreshSchema() now use the respective connection locks as well, avoiding potential concurrency issues.

Hopefully this is the last refactoring item for concurrent reads on the web :D

@changeset-bot
Copy link

changeset-bot bot commented Mar 23, 2026

🦋 Changeset detected

Latest commit: e6d0c2d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 8 packages
Name Type
@powersync/node Patch
@powersync/op-sqlite Patch
@powersync/common Minor
@powersync/adapter-sql-js Patch
@powersync/react-native Patch
@powersync/tanstack-react-query Patch
@powersync/web Patch
@powersync/diagnostics-app Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@simolus3 simolus3 marked this pull request as ready for review March 23, 2026 11:36
@simolus3 simolus3 requested a review from stevensJourney March 23, 2026 11:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant