-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDbCommandPool.cs
executable file
·180 lines (163 loc) · 5.73 KB
/
DbCommandPool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
namespace NeoSmart.Caching.Sqlite
{
class DbCommandPool : IDisposable
#if NETCOREAPP3_1_OR_GREATER
, IAsyncDisposable
#endif
{
/// <summary>
/// Number of connections to open to the database at startup. Ramps up as concurrency increases.
/// </summary>
private const int InitialConcurrency = 4;
private readonly ILogger _logger;
private readonly ConcurrentBag<SqliteCommand>[] _commands = new ConcurrentBag<SqliteCommand>[DbCommands.Count];
private readonly ConcurrentBag<SqliteConnection> _connections = new ConcurrentBag<SqliteConnection>();
private readonly string _connectionString;
public DbCommandPool(SqliteConnection db, ILogger logger)
{
_connectionString = db.ConnectionString;
_logger = logger;
_logger.LogTrace("Initializing db command pool");
for (int i = 0; i < _commands.Length; ++i)
{
_commands[i] = new ConcurrentBag<SqliteCommand>();
}
_logger.LogTrace("Creating {InitialConnections} initial connections in the pool", InitialConcurrency);
for (int i = 0; i < InitialConcurrency; ++i)
{
var connection = new SqliteConnection(_connectionString);
_logger.LogTrace("Opening connection to {SqliteCacheDbPath}", _connectionString);
connection.Open();
_connections.Add(connection);
}
}
public void Use(Operation type, Action<SqliteCommand> handler)
{
Use<bool>(type, (cmd) =>
{
handler(cmd);
return true;
});
}
public R Use<R>(Func<SqliteConnection, R> handler)
{
if (!_connections.TryTake(out var db))
{
_logger.LogTrace("Adding a new connection to the connection pool");
db = new SqliteConnection(_connectionString);
_logger.LogTrace("Opening connection to {SqliteCacheDbPath}", _connectionString);
db.Open();
}
try
{
return handler(db);
}
finally
{
_connections.Add(db);
}
}
public R Use<R>(Operation type, Func<SqliteCommand, R> handler)
{
return Use((conn) =>
{
var pool = _commands[(int)type];
if (!pool.TryTake(out var command))
{
_logger.LogTrace("Adding a new {DbCommand} command to the command pool", type);
command = new SqliteCommand(DbCommands.Commands[(int)type], conn);
}
try
{
command.Connection = conn;
return handler(command);
}
finally
{
command.Connection = null;
command.Parameters.Clear();
pool.Add(command);
}
});
}
public async Task<R> UseAsync<R>(Func<SqliteConnection, Task<R>> handler)
{
if (!_connections.TryTake(out var db))
{
_logger.LogTrace("Adding a new connection to the connection pool");
db = new SqliteConnection(_connectionString);
_logger.LogTrace("Opening connection to {SqliteCacheDbPath}", _connectionString);
await db.OpenAsync().ConfigureAwait(false);
}
try
{
return await handler(db).ConfigureAwait(false);
}
finally
{
_connections.Add(db);
}
}
public Task<R> UseAsync<R>(Operation type, Func<SqliteCommand, Task<R>> handler)
{
return UseAsync(async (conn) =>
{
var pool = _commands[(int)type];
if (!pool.TryTake(out var command))
{
_logger.LogTrace("Adding a new {DbCommand} command to the command pool", type);
command = new SqliteCommand(DbCommands.Commands[(int)type], conn);
}
try
{
command.Connection = conn;
return await handler(command).ConfigureAwait(false);
}
finally
{
command.Connection = null;
command.Parameters.Clear();
pool.Add(command);
}
});
}
public void Dispose()
{
foreach (var pool in _commands)
{
while (pool.TryTake(out var cmd))
{
cmd.Dispose();
}
}
foreach (var conn in _connections)
{
_logger.LogTrace("Closing connection to {SqliteCacheDbPath}", _connectionString);
conn.Close();
conn.Dispose();
}
}
#if NETCOREAPP3_1_OR_GREATER
public async ValueTask DisposeAsync()
{
foreach (var pool in _commands)
{
while (pool.TryTake(out var cmd))
{
await cmd.DisposeAsync().ConfigureAwait(false);
}
}
foreach (var conn in _connections)
{
await conn.CloseAsync().ConfigureAwait(false);
await conn.DisposeAsync().ConfigureAwait(false);
}
}
#endif
}
}