From f564152d88a8ed2df72326c032c89889bdf211df Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Wed, 1 Sep 2021 08:58:03 -0500 Subject: create a new connection for each search --- src/slskd/Application/SharedFileCache.cs | 19 +++++++++++-------- src/slskd/Program.cs | 3 ++- 2 files changed, 13 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/slskd/Application/SharedFileCache.cs b/src/slskd/Application/SharedFileCache.cs index d0486efd..e9ebeb66 100644 --- a/src/slskd/Application/SharedFileCache.cs +++ b/src/slskd/Application/SharedFileCache.cs @@ -29,7 +29,6 @@ namespace slskd using System.Threading; using System.Threading.Tasks; using Microsoft.Data.Sqlite; - using Microsoft.Extensions.Caching.Memory; using Serilog; using Soulseek; @@ -60,7 +59,6 @@ namespace slskd private List Shares { get; set; } private SqliteConnection SQLite { get; set; } private SemaphoreSlim SyncRoot { get; } = new SemaphoreSlim(1); - private ConcurrentDictionary SQLiteConnections { get; } = new ConcurrentDictionary(); /// /// Returns the contents of the cache. @@ -308,6 +306,12 @@ namespace slskd /// The matching files. public async Task> SearchAsync(SearchQuery query) { + // ignore requests while the cache is building + if (SyncRoot.CurrentCount == 0) + { + return Enumerable.Empty(); + } + string Clean(string str) => str.Replace("/", " ") .Replace("\\", " ") .Replace(":", " ") @@ -318,14 +322,13 @@ namespace slskd var exclusions = string.Join(" OR ", query.Exclusions.Select(exclusion => $"\"{Clean(exclusion)}\"")); var sql = $"SELECT * FROM cache WHERE cache MATCH '({match}) {(query.Exclusions.Any() ? $"NOT ({exclusions})" : string.Empty)}'"; + var results = new List(); try { - var sqlite = SQLiteConnections.GetOrAdd(Thread.CurrentThread.ManagedThreadId, new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared")); - using var cmd = new SqliteCommand(sql, sqlite); - - var results = new List(); - sqlite.Open(); + using var conn = new SqliteConnection("Data Source=file:shares?mode=memory&cache=shared"); + using var cmd = new SqliteCommand(sql, conn); + await conn.OpenAsync(); var reader = await cmd.ExecuteReaderAsync(); @@ -338,7 +341,7 @@ namespace slskd } catch (Exception ex) { - Log.Error(ex, "Malformed query: {Query}", query); + Log.Warning(ex, "Failed to execute shared file query '{Query}': {Message}", query, ex.Message); return Enumerable.Empty(); } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 4489487a..ca7635c3 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -61,7 +61,8 @@ namespace slskd /// /// The default configuration filename. /// - public static readonly string DefaultConfigurationFile = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.yml"); + //public static readonly string DefaultConfigurationFile = Path.Combine(AppContext.BaseDirectory, "config", $"{AppName}.yml"); + public static readonly string DefaultConfigurationFile = @"C:\Users\JP.WHATNET\slskd.yml"; /// /// The default incomplete download directory. -- cgit v1.2.3