diff --git a/server/RdtClient.Data/Data/DownloadData.cs b/server/RdtClient.Data/Data/DownloadData.cs index 93b850c..e3fdcc5 100644 --- a/server/RdtClient.Data/Data/DownloadData.cs +++ b/server/RdtClient.Data/Data/DownloadData.cs @@ -47,8 +47,6 @@ public class DownloadData(DataContext dataContext) await dataContext.SaveChangesAsync(); - await TorrentData.VoidCache(); - return download; } @@ -65,8 +63,6 @@ public class DownloadData(DataContext dataContext) dbDownload.Link = unrestrictedLink; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateFileName(Guid downloadId, String fileName) @@ -82,8 +78,6 @@ public class DownloadData(DataContext dataContext) dbDownload.FileName = fileName; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateDownloadStarted(Guid downloadId, DateTimeOffset? dateTime) @@ -99,8 +93,6 @@ public class DownloadData(DataContext dataContext) dbDownload.DownloadStarted = dateTime; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateDownloadFinished(Guid downloadId, DateTimeOffset? dateTime) @@ -116,8 +108,6 @@ public class DownloadData(DataContext dataContext) dbDownload.DownloadFinished = dateTime; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateUnpackingQueued(Guid downloadId, DateTimeOffset? dateTime) @@ -133,8 +123,6 @@ public class DownloadData(DataContext dataContext) dbDownload.UnpackingQueued = dateTime; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateUnpackingStarted(Guid downloadId, DateTimeOffset? dateTime) @@ -150,8 +138,6 @@ public class DownloadData(DataContext dataContext) dbDownload.UnpackingStarted = dateTime; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateUnpackingFinished(Guid downloadId, DateTimeOffset? dateTime) @@ -167,8 +153,6 @@ public class DownloadData(DataContext dataContext) dbDownload.UnpackingFinished = dateTime; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateCompleted(Guid downloadId, DateTimeOffset? dateTime) @@ -184,8 +168,6 @@ public class DownloadData(DataContext dataContext) dbDownload.Completed = dateTime; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateError(Guid downloadId, String? error) @@ -201,8 +183,6 @@ public class DownloadData(DataContext dataContext) dbDownload.Error = error; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateRetryCount(Guid downloadId, Int32 retryCount) @@ -218,8 +198,6 @@ public class DownloadData(DataContext dataContext) dbDownload.RetryCount = retryCount; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task UpdateRemoteId(Guid downloadId, String remoteId) @@ -246,8 +224,6 @@ public class DownloadData(DataContext dataContext) dataContext.Downloads.RemoveRange(downloads); await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } public async Task Reset(Guid downloadId) @@ -269,7 +245,5 @@ public class DownloadData(DataContext dataContext) dbDownload.Error = null; await dataContext.SaveChangesAsync(); - - await TorrentData.VoidCache(); } } diff --git a/server/RdtClient.Data/Data/TorrentData.cs b/server/RdtClient.Data/Data/TorrentData.cs index 55f00ce..c42e07f 100644 --- a/server/RdtClient.Data/Data/TorrentData.cs +++ b/server/RdtClient.Data/Data/TorrentData.cs @@ -17,9 +17,10 @@ public class TorrentData(DataContext dataContext) : ITorrentData try { _torrentCache ??= await dataContext.Torrents - .AsNoTracking() - .Include(m => m.Downloads) - .ToListAsync(); + .AsNoTracking() + .AsSplitQuery() + .Include(m => m.Downloads) + .ToListAsync(); return [.. _torrentCache.OrderBy(m => m.Priority ?? 9999).ThenBy(m => m.Added)]; } diff --git a/server/RdtClient.Data/DiConfig.cs b/server/RdtClient.Data/DiConfig.cs index d5a7823..bbd49f4 100644 --- a/server/RdtClient.Data/DiConfig.cs +++ b/server/RdtClient.Data/DiConfig.cs @@ -14,7 +14,7 @@ public static class DiConfig throw new("Invalid database path found in appSettings"); } - var connectionString = $"Data Source={appSettings.Database.Path}"; + var connectionString = $"Data Source={appSettings.Database.Path};Cache=Shared;Pooling=True;Command Timeout=30"; services.AddDbContext(options => options.UseSqlite(connectionString)); services.AddScoped(); diff --git a/server/RdtClient.Service/BackgroundServices/Startup.cs b/server/RdtClient.Service/BackgroundServices/Startup.cs index db890ac..36dbaf2 100644 --- a/server/RdtClient.Service/BackgroundServices/Startup.cs +++ b/server/RdtClient.Service/BackgroundServices/Startup.cs @@ -24,6 +24,12 @@ public class Startup(IServiceProvider serviceProvider) : IHostedService var dbContext = scope.ServiceProvider.GetRequiredService(); await dbContext.Database.MigrateAsync(cancellationToken); + // Configure SQLite for better concurrency and performance + await dbContext.Database.ExecuteSqlRawAsync("PRAGMA journal_mode=WAL;", cancellationToken); + await dbContext.Database.ExecuteSqlRawAsync("PRAGMA synchronous=NORMAL;", cancellationToken); + await dbContext.Database.ExecuteSqlRawAsync("PRAGMA busy_timeout=5000;", cancellationToken); + await dbContext.Database.ExecuteSqlRawAsync("PRAGMA cache_size=-64000;", cancellationToken); + var settings = scope.ServiceProvider.GetRequiredService(); await settings.Seed(); await settings.ResetCache(); diff --git a/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs b/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs index 91c7da0..1b58fc1 100644 --- a/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs +++ b/server/RdtClient.Service/BackgroundServices/WebsocketsUpdater.cs @@ -14,14 +14,15 @@ public class WebsocketsUpdater(ILogger logger, IServiceProvid await Task.Delay(1000, stoppingToken); } - var scope = serviceProvider.CreateScope(); - - var remoteService = scope.ServiceProvider.GetRequiredService(); + logger.LogInformation("WebsocketsUpdater started."); while (!stoppingToken.IsCancellationRequested) { try { + using var scope = serviceProvider.CreateScope(); + var remoteService = scope.ServiceProvider.GetRequiredService(); + await remoteService.Update(); } catch (Exception ex) @@ -29,7 +30,11 @@ public class WebsocketsUpdater(ILogger logger, IServiceProvid logger.LogError(ex, $"Unexpected error occurred in WebsocketsUpdater: {ex.Message}"); } - await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken); + var delay = RdtHub.HasConnections + ? TimeSpan.FromSeconds(1) + : TimeSpan.FromSeconds(5); + + await Task.Delay(delay, stoppingToken); } logger.LogInformation("WebsocketsUpdater stopped.");