Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One concurrent download per host for all hosts #3557

Merged
merged 1 commit into from
Apr 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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