feat: pool read connections in MDSQLiteAdapter#45
Open
Conversation
MDSQLiteAdapterMDSQLiteAdapter
Chriztiaan
reviewed
Feb 26, 2026
| private readonly AsyncLock writeMutex = new(); | ||
|
|
||
| // Many readers | ||
| private Channel<MDSQLiteConnection> readPool = null!; |
Collaborator
There was a problem hiding this comment.
I am tempted to suggest wrapping this in a class to help with readability for other devs (like me 😉):
public class ConnectionPool<T>
{
/// <summary>Creates a bounded pool with the given capacity.</summary>
public ConnectionPool(int size);
// xxxx
Claim() // or some other word for taking requesting connection
// xxx
Release() // Or some other word for giving a connection back to the pool
/// <summary>
/// Borrows a connection, executes the function, and returns it to the pool.
/// Blocks asynchronously if no connections are available.
/// </summary>
public async Task<TResult> Borrow<TResult>(Func<T, Task<TResult>> fn);
/// <summary>
/// Drains all connections from the pool, executes the callback,
/// then returns them. Blocks all other borrowers while running.
/// </summary>
public async Task WithAll(Func<List<T>, Task> callback);
}
Borrow could for example be used in readLock's implementation instead:
public async Task<T> ReadLock<T>(Func<IDBGetConnection, Task<T>> fn, DBLockOptions? options = null)
{
await initialized;
return await readPool.Borrow(conn => fn(conn));
}
Basically just making some of the reading easier. Some of the signatures could have better names though.
Contributor
Author
There was a problem hiding this comment.
I added an MDSQLiteConnectionPool class which wraps the channel.
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.
Pooled read connections
Status
TODO
Description
Currently, we use an exclusive lock to manage reading from the SQLite database. The lock is used to restrict access to the one singular SqliteConnection object we manage for reading. This means that only one read can occur against the database at any given time, which can become an issue when dealing with multiple watched queries, which might lock each other up despite all only needing read access to the database.
This PR replaces the old
readConnectionandreadMutexobjects with areadPoolchannel. TheMDSQLiteAdapternow maintains a "queue" ofSqliteConnectionobjects which it distributes access to via a bounded channel. The pool size can be set viaMDSQLiteOptions.ReadPoolSize(currently internal), and defaults to 5 read connections.Potential improvements
UNPLANNED
Dynamic pool sizing
GetPersistentConnectionmethodSqliteConnectionobject setup for reading data, mainly for integrations with existing tools that require an object implementingIDbConnection.Alternative solutions
Built-in ADO.NET connection pool
ADO.NET provides a built-in connection pool that allows multiple SqliteConnection objects with the same connection string to automatically reuse underlying connections. There's a potential use case here; however, since we call LoadExtension on each connection we use (as well as set up a number of PRAGMAs for both read and write connections), I'm unsure of how practical this would be ITO keeping connections setup correctly.
Edit: Upon testing, it does seem to be considerably slower. Will stick with a manual connection pool for now.