Skip to content

Commit

Permalink
make file downloading cancellable
Browse files Browse the repository at this point in the history
  • Loading branch information
sensslen committed Dec 28, 2023
1 parent ff33df1 commit f44a1de
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
40 changes: 16 additions & 24 deletions src/NuGetUtility/LicenseValidator/LicenseValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public LicenseValidator(Dictionary<Uri, string> licenseMapping,
}

public async Task<IEnumerable<LicenseValidationResult>> Validate(
IAsyncEnumerable<ReferencedPackageWithContext> packages)
IAsyncEnumerable<ReferencedPackageWithContext> packages, CancellationToken token)
{
var result = new ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult>();
await foreach (ReferencedPackageWithContext info in packages)
Expand All @@ -44,7 +44,7 @@ public async Task<IEnumerable<LicenseValidationResult>> Validate(
}
else if (info.PackageInfo.LicenseUrl != null)
{
await ValidateLicenseByUrl(info.PackageInfo, info.Context, result);
await ValidateLicenseByUrl(info.PackageInfo, info.Context, result, token);
}
else
{
Expand Down Expand Up @@ -77,8 +77,8 @@ private void AddOrUpdateLicense(
origin,
new List<ValidationError> { error });
result.AddOrUpdate(new LicenseNameAndVersion(info.Identity.Id, info.Identity.Version),
key => CreateResult(key, newValue),
(key, oldValue) => UpdateResult(key, oldValue, newValue));
newValue,
(key, oldValue) => UpdateResult(oldValue, newValue));
}

private void AddOrUpdateLicense(
Expand All @@ -94,12 +94,11 @@ private void AddOrUpdateLicense(
license,
origin);
result.AddOrUpdate(new LicenseNameAndVersion(info.Identity.Id, info.Identity.Version),
key => CreateResult(key, newValue),
(key, oldValue) => UpdateResult(key, oldValue, newValue));
newValue,
(key, oldValue) => UpdateResult(oldValue, newValue));
}

private LicenseValidationResult UpdateResult(LicenseNameAndVersion _,
LicenseValidationResult oldValue,
private LicenseValidationResult UpdateResult(LicenseValidationResult oldValue,
LicenseValidationResult newValue)
{
oldValue.ValidationErrors.AddRange(newValue.ValidationErrors);
Expand All @@ -111,11 +110,6 @@ private LicenseValidationResult UpdateResult(LicenseNameAndVersion _,
return oldValue;
}

private LicenseValidationResult CreateResult(LicenseNameAndVersion _, LicenseValidationResult newValue)
{
return newValue;
}

private void ValidateLicenseByMetadata(IPackageMetadata info,
string context,
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result)
Expand Down Expand Up @@ -155,14 +149,20 @@ private void ValidateLicenseByMetadata(IPackageMetadata info,

private async Task ValidateLicenseByUrl(IPackageMetadata info,
string context,
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result)
ConcurrentDictionary<LicenseNameAndVersion, LicenseValidationResult> result,
CancellationToken token)
{
if (info.LicenseUrl!.IsAbsoluteUri)
{
try
{
await _fileDownloader.DownloadFile(info.LicenseUrl,
$"{info.Identity.Id}__{info.Identity.Version}.html");
$"{info.Identity.Id}__{info.Identity.Version}.html",
token);
}
catch (OperationCanceledException)
{
// swallow cancellation
}
catch (Exception e)
{
Expand Down Expand Up @@ -212,15 +212,7 @@ private bool IsLicenseValid(string licenseId)
return true;
}

foreach (string allowedLicense in _allowedLicenses)
{
if (allowedLicense.Equals(licenseId))
{
return true;
}
}

return false;
return _allowedLicenses.Any(allowedLicense => allowedLicense.Equals(licenseId));
}

private string GetLicenseNotAllowedMessage(string license)
Expand Down
2 changes: 1 addition & 1 deletion src/NuGetUtility/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private async Task<int> OnExecuteAsync(CancellationToken cancellationToken)
});
IAsyncEnumerable<ReferencedPackageWithContext> downloadedLicenseInformation =
packagesForProject.SelectMany(p => GetPackageInfos(p, overridePackageInformation, cancellationToken));
var results = (await validator.Validate(downloadedLicenseInformation)).ToList();
var results = (await validator.Validate(downloadedLicenseInformation, cancellationToken)).ToList();

if (projectReaderExceptions.Any())
{
Expand Down
12 changes: 6 additions & 6 deletions src/NuGetUtility/Wrapper/HttpClientWrapper/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ public FileDownloader(HttpClient client, string downloadDirectory)
_downloadDirectory = downloadDirectory;
}

public async Task DownloadFile(Uri url, string fileName)
public async Task DownloadFile(Uri url, string fileName, CancellationToken token)
{
await _parallelDownloadLimiter.WaitAsync();
await _parallelDownloadLimiter.WaitAsync(token);
try
{
for (int i = 0; i < MAX_RETRIES; i++)
{
await using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName));
var request = new HttpRequestMessage(HttpMethod.Get, url);

HttpResponseMessage response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
HttpResponseMessage response = await _client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, token);
response.EnsureSuccessStatusCode();
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests)
{
await Task.Delay((int)Math.Pow(EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS, i + 1));
await Task.Delay((int)Math.Pow(EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS, i + 1), token);
continue;
}
using Stream downloadStream = await response.Content.ReadAsStreamAsync();
using Stream downloadStream = await response.Content.ReadAsStreamAsync(token);

await downloadStream.CopyToAsync(file);
await downloadStream.CopyToAsync(file, token);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public interface IFileDownloader
{
public Task DownloadFile(Uri url, string fileName);
public Task DownloadFile(Uri url, string fileName, CancellationToken token);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
public class NopFileDownloader : IFileDownloader
{
public Task DownloadFile(Uri url, string fileName)
public Task DownloadFile(Uri url, string fileName, CancellationToken token)
{
return Task.CompletedTask;
}
Expand Down

0 comments on commit f44a1de

Please sign in to comment.