Fix code for reducing chance of SQLite connection pool livelocks

This commit is contained in:
chylex 2022-07-18 14:32:22 +02:00
parent cd418f4871
commit d463b407f4
No known key found for this signature in database
GPG Key ID: 4DE42C8F19A80548

View File

@ -45,22 +45,21 @@ namespace DHT.Server.Database.Sqlite.Utils {
}
public ISqliteConnection Take() {
PooledConnection? conn = null;
while (conn == null) {
while (true) {
ThrowIfDisposed();
lock (monitor) {
if (free.TryTake(out conn, TimeSpan.FromMilliseconds(rand.Next(100, 200)))) {
if (free.TryTake(out var conn)) {
used.Add(conn);
break;
return conn;
}
else {
Log.ForType<SqliteConnectionPool>().Warn("Thread " + Thread.CurrentThread.ManagedThreadId + " is starving for connections.");
}
}
Thread.Sleep(TimeSpan.FromMilliseconds(rand.Next(100, 200)));
}
return conn;
}
private void Return(PooledConnection conn) {