Throttle episode scanning to 10 concurrent scans.

This commit is contained in:
Michael Bucari-Tovo 2022-05-25 20:43:12 -06:00
parent 76954b5a0a
commit a8f41841bd

View File

@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AudibleApi;
using AudibleApi.Common;
@ -123,14 +124,15 @@ namespace AudibleUtilities
List<Task<List<Item>>> getChildEpisodesTasks = new();
int count = 0;
int count = 0, maxConcurrentEpisodeScans = 10;
using SemaphoreSlim concurrencySemaphore = new(maxConcurrentEpisodeScans);
await foreach (var item in Api.GetLibraryItemAsyncEnumerable(libraryOptions))
{
if (item.IsEpisodes && importEpisodes)
{
//Get child episodes asynchronously and await all at the end
getChildEpisodesTasks.Add(getChildEpisodesAsync(item));
getChildEpisodesTasks.Add(getChildEpisodesAsync(concurrencySemaphore, item));
}
else if (!item.IsEpisodes)
items.Add(item);
@ -138,7 +140,7 @@ namespace AudibleUtilities
count++;
}
Serilog.Log.Logger.Debug("Library scan complete. Found {count} books. Waiting on episode scans to complete", count);
Serilog.Log.Logger.Debug("Library scan complete. Found {count} books and series. Waiting on {getChildEpisodesTasksCount} series episode scans to complete", count, getChildEpisodesTasks.Count);
//await and add all episides from all parents
foreach (var epList in await Task.WhenAll(getChildEpisodesTasks))
@ -163,7 +165,11 @@ namespace AudibleUtilities
#region episodes and podcasts
private async Task<List<Item>> getChildEpisodesAsync(Item parent)
private async Task<List<Item>> getChildEpisodesAsync(SemaphoreSlim concurrencySemaphore, Item parent)
{
await concurrencySemaphore.WaitAsync();
try
{
Serilog.Log.Logger.Debug("Beginning episode scan for {parent}", parent);
@ -201,6 +207,11 @@ namespace AudibleUtilities
}
return children;
}
finally
{
concurrencySemaphore.Release();
}
}
private async Task<List<Item>> getEpisodeChildrenAsync(Item parent)
{