-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Expand file tree
/
Copy pathSqliteConnectionPool.cs
More file actions
163 lines (133 loc) · 4.05 KB
/
SqliteConnectionPool.cs
File metadata and controls
163 lines (133 loc) · 4.05 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
namespace Microsoft.Data.Sqlite;
internal class SqliteConnectionPool
{
private static readonly Random _random = new();
private readonly SqliteConnectionStringBuilder _connectionOptions;
private readonly List<SqliteConnectionInternal> _connections = [];
private readonly Stack<SqliteConnectionInternal> _warmPool = new();
private readonly Stack<SqliteConnectionInternal> _coldPool = new();
private Timer? _pruneTimer;
private State _state = State.Active;
public SqliteConnectionPool(SqliteConnectionStringBuilder connectionOptions)
{
lock (_random)
{
// 2-4 minutes in 10 second intervals
var prunePeriod = TimeSpan.FromSeconds(_random.Next(2 * 6, 4 * 6) * 10);
_pruneTimer = new Timer(PruneCallback, null, prunePeriod, prunePeriod);
}
_connectionOptions = connectionOptions;
}
public int Count
=> _connections.Count;
public void Shutdown()
{
_state = State.Disabled;
_pruneTimer?.Dispose();
_pruneTimer = null;
}
public SqliteConnectionInternal GetConnection()
{
SqliteConnectionInternal? connection = null;
do
{
lock (_connections)
{
if (!TryPop(_warmPool, out connection)
&& !TryPop(_coldPool, out connection)
&& (Count % 2 == 1 || !ReclaimLeakedConnections()))
{
connection = new SqliteConnectionInternal(_connectionOptions, this);
_connections.Add(connection);
}
}
}
while (connection == null);
return connection;
}
private static bool TryPop(Stack<SqliteConnectionInternal> stack, out SqliteConnectionInternal? connection)
=> stack.TryPop(out connection);
public void Return(SqliteConnectionInternal connection)
{
lock (_connections)
{
connection.Deactivate();
if (_state != State.Disabled
&& connection.CanBePooled)
{
_warmPool.Push(connection);
}
else
{
DisposeConnection(connection);
}
}
}
public void Clear()
{
lock (_connections)
{
foreach (var connection in _connections)
{
connection.DoNotPool();
}
while (TryPop(_warmPool, out var connection))
{
DisposeConnection(connection!);
}
while (TryPop(_coldPool, out var connection))
{
DisposeConnection(connection!);
}
ReclaimLeakedConnections();
}
}
private void PruneCallback(object? _)
{
lock (_connections)
{
while (TryPop(_coldPool, out var connection))
{
DisposeConnection(connection!);
}
while (TryPop(_warmPool, out var connection))
{
_coldPool.Push(connection!);
}
}
}
private void DisposeConnection(SqliteConnectionInternal connection)
{
lock (_connections)
{
_connections.Remove(connection);
}
connection.Dispose();
}
private bool ReclaimLeakedConnections()
{
var leakedConnectionsFound = false;
List<SqliteConnectionInternal> leakedConnections;
lock (_connections)
{
leakedConnections = _connections.Where(c => c.Leaked).ToList();
}
foreach (var connection in leakedConnections)
{
leakedConnectionsFound = true;
Return(connection);
}
return leakedConnectionsFound;
}
private enum State
{
Active,
Disabled
}
}