diff --git a/src/FilterLists.Agent/ListArchiver/DownloadLists.cs b/src/FilterLists.Agent/ListArchiver/DownloadLists.cs index ce1fd0b9c..107e48b26 100644 --- a/src/FilterLists.Agent/ListArchiver/DownloadLists.cs +++ b/src/FilterLists.Agent/ListArchiver/DownloadLists.cs @@ -29,15 +29,20 @@ public Handler(IMediator mediator) _mediator = mediator; } + // https://stackoverflow.com/a/22492731/2343739 protected override async Task Handle(Command request, CancellationToken cancellationToken) { var downloader = new TransformBlock( l => _mediator.Send(new DownloadList.Command(l), cancellationToken), new ExecutionDataflowBlockOptions {MaxDegreeOfParallelism = MaxDegreeOfParallelism}); + var buffer = new BufferBlock(); + downloader.LinkTo(buffer); foreach (var list in request.Lists) await downloader.SendAsync(list, cancellationToken); downloader.Complete(); await downloader.Completion; + if (buffer.TryReceiveAll(out var tasks)) + await Task.WhenAll(tasks); } } }