aboutsummaryrefslogtreecommitdiff
path: root/Emby.Server.Implementations/Data/ConnectionPool.cs
diff options
context:
space:
mode:
authorCody Robibero <cody@robibe.ro>2023-05-04 12:33:46 -0600
committerGitHub <noreply@github.com>2023-05-04 12:33:46 -0600
commitaaddc5a33e5f64c7a36eede3c76f561d00b0c7b2 (patch)
treea5a75dda01261eced7cdb4baa2e2187dfcb4473b /Emby.Server.Implementations/Data/ConnectionPool.cs
parent5e2a1fb4478d5324961a3f822a1057628e75d2a8 (diff)
parent8e1f0d53c1efe286628a4119b3c595f219513d23 (diff)
Merge pull request #9643 from Bond-009/sqlconnectionpool
Diffstat (limited to 'Emby.Server.Implementations/Data/ConnectionPool.cs')
-rw-r--r--Emby.Server.Implementations/Data/ConnectionPool.cs79
1 files changed, 79 insertions, 0 deletions
diff --git a/Emby.Server.Implementations/Data/ConnectionPool.cs b/Emby.Server.Implementations/Data/ConnectionPool.cs
new file mode 100644
index 000000000..5ea7e934f
--- /dev/null
+++ b/Emby.Server.Implementations/Data/ConnectionPool.cs
@@ -0,0 +1,79 @@
+using System;
+using System.Collections.Concurrent;
+using SQLitePCL.pretty;
+
+namespace Emby.Server.Implementations.Data;
+
+/// <summary>
+/// A pool of SQLite Database connections.
+/// </summary>
+public sealed class ConnectionPool : IDisposable
+{
+ private readonly BlockingCollection<SQLiteDatabaseConnection> _connections = new();
+ private bool _disposed;
+
+ /// <summary>
+ /// Initializes a new instance of the <see cref="ConnectionPool" /> class.
+ /// </summary>
+ /// <param name="count">The number of database connection to create.</param>
+ /// <param name="factory">Factory function to create the database connections.</param>
+ public ConnectionPool(int count, Func<SQLiteDatabaseConnection> factory)
+ {
+ for (int i = 0; i < count; i++)
+ {
+ _connections.Add(factory.Invoke());
+ }
+ }
+
+ /// <summary>
+ /// Gets a database connection from the pool if one is available, otherwise blocks.
+ /// </summary>
+ /// <returns>A database connection.</returns>
+ public ManagedConnection GetConnection()
+ {
+ if (_disposed)
+ {
+ ThrowObjectDisposedException();
+ }
+
+ return new ManagedConnection(_connections.Take(), this);
+
+ static void ThrowObjectDisposedException()
+ {
+ throw new ObjectDisposedException(nameof(ConnectionPool));
+ }
+ }
+
+ /// <summary>
+ /// Return a database connection to the pool.
+ /// </summary>
+ /// <param name="connection">The database connection to return.</param>
+ public void Return(SQLiteDatabaseConnection connection)
+ {
+ if (_disposed)
+ {
+ connection.Dispose();
+ return;
+ }
+
+ _connections.Add(connection);
+ }
+
+ /// <inheritdoc />
+ public void Dispose()
+ {
+ if (_disposed)
+ {
+ return;
+ }
+
+ foreach (var connection in _connections)
+ {
+ connection.Dispose();
+ }
+
+ _connections.Dispose();
+
+ _disposed = true;
+ }
+}