Skip to content

Commit

Permalink
One concurrent download per host for all hosts
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Apr 6, 2022
1 parent bb6585f commit 33ebf27
Showing 1 changed file with 9 additions and 26 deletions.
35 changes: 9 additions & 26 deletions Core/Net/NetAsyncDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,6 @@ private void ResetAgent()
}
}

/// <summary>
/// Downloads from hosts in this list will be done sequentially rather
/// than in parallel
/// </summary>
private static readonly HashSet<string> throttledHosts = new HashSet<string>()
{
/// GitHub returns a 403-Forbidden status sometimes if you try to download
/// too much in parallel, see https://github.com/KSP-CKAN/CKAN/issues/2210
"github.com",
"api.github.com",
"raw.githubusercontent.com",
};

private static readonly ILog log = LogManager.GetLogger(typeof (NetAsyncDownloader));

public readonly IUser User;
Expand Down Expand Up @@ -189,17 +176,16 @@ private void DownloadModule(Net.DownloadTarget target)

/// <summary>
/// Check whether a given download should be deferred to be started later.
/// Decision is made based on whether the host is throttled and whether
/// we're already downloading something else from it.
/// Decision is made based on whether we're already downloading something
/// else from the same host.
/// </summary>
/// <param name="target">Info about a requested download</param>
/// <returns>
/// true to queue, false to start immediately
/// </returns>
private bool shouldQueue(Net.DownloadTarget target)
{
return throttledHosts.Contains(target.url.Host)
&& downloads.Any(dl =>
return downloads.Any(dl =>
dl.target.url.Host == target.url.Host
&& dl.bytesLeft > 0);
}
Expand Down Expand Up @@ -423,16 +409,13 @@ private void FileDownloadComplete(int index, Exception error)
log.InfoFormat("Finished downloading {0}", downloads[index].target.url);
}

if (throttledHosts.Contains(downloads[index].target.url.Host))
var next = queuedDownloads.FirstOrDefault(dl =>
dl.url.Host == downloads[index].target.url.Host);
if (next != null)
{
var next = queuedDownloads.FirstOrDefault(dl =>
dl.url.Host == downloads[index].target.url.Host);
if (next != null)
{
// Start this host's next queued download
queuedDownloads.Remove(next);
DownloadModule(next);
}
// Start this host's next queued download
queuedDownloads.Remove(next);
DownloadModule(next);
}

onOneCompleted.Invoke(downloads[index].target.url, downloads[index].path, downloads[index].error);
Expand Down

0 comments on commit 33ebf27

Please sign in to comment.