From 94b149fd7ab1166adfaae34aea92dbcf936af5d2 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Jul 2022 16:48:55 -0500 Subject: Create -> Start --- src/slskd/Search/ISearchService.cs | 2 +- src/slskd/Search/SearchService.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/ISearchService.cs b/src/slskd/Search/ISearchService.cs index d2cac32e..9a45280b 100644 --- a/src/slskd/Search/ISearchService.cs +++ b/src/slskd/Search/ISearchService.cs @@ -38,7 +38,7 @@ namespace slskd.Search /// The search scope. /// Search options. /// The completed search. - Task CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); + Task StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null); /// /// Deletes the specified ssearch. diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index f077e76b..94ad5e3e 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -77,7 +77,7 @@ namespace slskd.Search /// The search scope. /// Search options. /// The completed search. - public async Task CreateAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null) + public async Task StartAsync(Guid id, SearchQuery query, SearchScope scope, SearchOptions options = null) { var token = Client.GetNextToken(); -- cgit v1.2.3 From dcdec201f730d065246f1695fdde6372f71435c5 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Jul 2022 16:56:12 -0500 Subject: save search state more frequently during execution --- src/slskd/Search/SearchService.cs | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 94ad5e3e..45ad86db 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -100,15 +100,31 @@ namespace slskd.Search List responses = new(); + async Task UpdateAndSaveChangesAsync(Search search) + { + using var context = ContextFactory.CreateDbContext(); + context.Update(search); + await context.SaveChangesAsync(); + } + options ??= new SearchOptions(); options = options.WithActions( stateChanged: (args) => { search = search.WithSoulseekSearch(args.Search); SearchHub.BroadcastUpdateAsync(search); + _ = UpdateAndSaveChangesAsync(search); }, - responseReceived: (args) => - rateLimiter.Invoke(() => SearchHub.BroadcastUpdateAsync(search.WithSoulseekSearch(args.Search)))); + responseReceived: (args) => rateLimiter.Invoke(() => + { + // note: this is rate limited, but has the potential to update + // the database every 250ms (or whatever the interval is set to) + // for the duration of the search. any issues with disk/io or performance + // while searches are running should investigate this as a cause + search = search.WithSoulseekSearch(args.Search); + SearchHub.BroadcastUpdateAsync(search); + _ = UpdateAndSaveChangesAsync(search); + })); var soulseekSearchTask = Client.SearchAsync( query, @@ -132,13 +148,10 @@ namespace slskd.Search try { - using var context = ContextFactory.CreateDbContext(); - search.EndedAt = DateTime.UtcNow; search.Responses = responses.Select(r => Response.FromSoulseekSearchResponse(r)); - context.Update(search); - await context.SaveChangesAsync(); + await UpdateAndSaveChangesAsync(search); // zero responses before broadcasting search.Responses = Enumerable.Empty(); -- cgit v1.2.3 From 6bcab1af3c4cc3227f18192d14cbcecc13f40a94 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Jul 2022 16:57:34 -0500 Subject: oops --- src/slskd/Search/API/Controllers/SearchesController.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Search/API/Controllers/SearchesController.cs b/src/slskd/Search/API/Controllers/SearchesController.cs index 57ddfc13..acae18e0 100644 --- a/src/slskd/Search/API/Controllers/SearchesController.cs +++ b/src/slskd/Search/API/Controllers/SearchesController.cs @@ -68,7 +68,7 @@ namespace slskd.Search.API try { - search = await Searches.CreateAsync(id, SearchQuery.FromText(request.SearchText), SearchScope.Network, request.ToSearchOptions()); + search = await Searches.StartAsync(id, SearchQuery.FromText(request.SearchText), SearchScope.Network, request.ToSearchOptions()); } catch (Exception ex) when (ex is ArgumentException || ex is Soulseek.DuplicateTokenException) { -- cgit v1.2.3 From f84f2498c1ef9bc8a0fc09a4848a1a5d27f8f22b Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Jul 2022 18:01:56 -0500 Subject: prevent updates after the search is concluded --- src/slskd/Search/SearchService.cs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 45ad86db..9b30f26c 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -117,13 +117,22 @@ namespace slskd.Search }, responseReceived: (args) => rateLimiter.Invoke(() => { - // note: this is rate limited, but has the potential to update - // the database every 250ms (or whatever the interval is set to) - // for the duration of the search. any issues with disk/io or performance - // while searches are running should investigate this as a cause - search = search.WithSoulseekSearch(args.Search); - SearchHub.BroadcastUpdateAsync(search); - _ = UpdateAndSaveChangesAsync(search); + // due to the rate limiter, this has the potential to be executed after the search is complete. + // ensure that doesn't happen. this causes the update to be dropped, but we broadcast again when + // the search is complete with the final stats, so it has no negative impact. + if (search.State.HasFlag(SearchStates.InProgress)) + { + // note: this is rate limited, but has the potential to update + // the database every 250ms (or whatever the interval is set to) + // for the duration of the search. any issues with disk/io or performance + // while searches are running should investigate this as a cause + search.ResponseCount = args.Search.ResponseCount; + search.FileCount = args.Search.FileCount; + search.LockedFileCount = args.Search.LockedFileCount; + + SearchHub.BroadcastUpdateAsync(search); + _ = UpdateAndSaveChangesAsync(search); + } })); var soulseekSearchTask = Client.SearchAsync( -- cgit v1.2.3 From 113e92e271825cb0690bee5ff7c11991d7f09c00 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 16 Jul 2022 23:05:35 -0500 Subject: make flushing on dispose optional --- src/slskd/Common/RateLimiter.cs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/RateLimiter.cs b/src/slskd/Common/RateLimiter.cs index a5d1ba45..e54514d7 100644 --- a/src/slskd/Common/RateLimiter.cs +++ b/src/slskd/Common/RateLimiter.cs @@ -29,7 +29,8 @@ namespace slskd /// Initializes a new instance of the class. /// /// The minimum interval between invocations. - public RateLimiter(int interval) + /// A value indicating whether pending action(s) should be executed during disposal + public RateLimiter(int interval, bool flushOnDispose = false) { Timer = new System.Timers.Timer(interval) { @@ -41,9 +42,12 @@ namespace slskd Staged?.Invoke(); Staged = null; }; + + FlushOnDispose = flushOnDispose; } private bool Disposed { get; set; } + private bool FlushOnDispose { get; } private bool Init { get; set; } private Action Staged { get; set; } private System.Timers.Timer Timer { get; set; } @@ -85,7 +89,10 @@ namespace slskd if (disposing) { // if an action is staged, invoke it to 'flush' - Staged?.Invoke(); + if (FlushOnDispose) + { + Staged?.Invoke(); + } Timer.Dispose(); } -- cgit v1.2.3 From 58626e1e8f954160fc86f4c15f437c6698bf49db Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Jul 2022 10:43:01 -0500 Subject: remove guard and note about errant updates --- src/slskd/Search/SearchService.cs | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/slskd/Search/SearchService.cs b/src/slskd/Search/SearchService.cs index 9b30f26c..12c75fb1 100644 --- a/src/slskd/Search/SearchService.cs +++ b/src/slskd/Search/SearchService.cs @@ -117,22 +117,16 @@ namespace slskd.Search }, responseReceived: (args) => rateLimiter.Invoke(() => { - // due to the rate limiter, this has the potential to be executed after the search is complete. - // ensure that doesn't happen. this causes the update to be dropped, but we broadcast again when - // the search is complete with the final stats, so it has no negative impact. - if (search.State.HasFlag(SearchStates.InProgress)) - { - // note: this is rate limited, but has the potential to update - // the database every 250ms (or whatever the interval is set to) - // for the duration of the search. any issues with disk/io or performance - // while searches are running should investigate this as a cause - search.ResponseCount = args.Search.ResponseCount; - search.FileCount = args.Search.FileCount; - search.LockedFileCount = args.Search.LockedFileCount; - - SearchHub.BroadcastUpdateAsync(search); - _ = UpdateAndSaveChangesAsync(search); - } + // note: this is rate limited, but has the potential to update + // the database every 250ms (or whatever the interval is set to) + // for the duration of the search. any issues with disk i/o or performance + // while searches are running should investigate this as a cause + search.ResponseCount = args.Search.ResponseCount; + search.FileCount = args.Search.FileCount; + search.LockedFileCount = args.Search.LockedFileCount; + + SearchHub.BroadcastUpdateAsync(search); + _ = UpdateAndSaveChangesAsync(search); })); var soulseekSearchTask = Client.SearchAsync( -- cgit v1.2.3 From 44b547caea0a87367f040cb116cac107f6eb56b4 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Jul 2022 10:55:30 -0500 Subject: docs --- src/slskd/Common/RateLimiter.cs | 2 +- src/slskd/Core/Extensions.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/RateLimiter.cs b/src/slskd/Common/RateLimiter.cs index e54514d7..a93bbebf 100644 --- a/src/slskd/Common/RateLimiter.cs +++ b/src/slskd/Common/RateLimiter.cs @@ -29,7 +29,7 @@ namespace slskd /// Initializes a new instance of the class. /// /// The minimum interval between invocations. - /// A value indicating whether pending action(s) should be executed during disposal + /// A value indicating whether pending action(s) should be executed during disposal. public RateLimiter(int interval, bool flushOnDispose = false) { Timer = new System.Timers.Timer(interval) diff --git a/src/slskd/Core/Extensions.cs b/src/slskd/Core/Extensions.cs index d818d387..65942180 100644 --- a/src/slskd/Core/Extensions.cs +++ b/src/slskd/Core/Extensions.cs @@ -75,7 +75,7 @@ namespace slskd /// /// Creates a new instance of from this instance of . /// - /// The UserStatistics instance from which to copy data + /// The UserStatistics instance from which to copy data. /// The new instance. public static UserStatisticsState ToUserStatisticsState(this UserStatistics stats) => new() { -- cgit v1.2.3 From 7322aac4a5431348279eaf08d8511ae81ab89478 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 17 Jul 2022 10:55:38 -0500 Subject: remove unused var --- src/slskd/Application.cs | 2 -- 1 file changed, 2 deletions(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index b0bccd6c..a307a291 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -69,8 +69,6 @@ namespace slskd /// public static readonly string LeecherGroup = "leechers"; - private static readonly int ReconnectMaxDelayMilliseconds = 300000; // 5 minutes - public Application( OptionsAtStartup optionsAtStartup, IOptionsMonitor optionsMonitor, -- cgit v1.2.3