forked from tomchavakis/nuget-license
-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from sensslen/simon/add-rate-limiting-support
- Loading branch information
Showing
18 changed files
with
101 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["MIT","Apache-2.0","MS-EULA"] |
File renamed without changes.
2 changes: 2 additions & 0 deletions
2
.github/workflows/assets/App/overwritePackageInformation.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[ | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["./src/NuGetUtility/NuGetUtility.csproj"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
{ | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["NETStandard.Library"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,4 +79,4 @@ | |
"Version": "2.1.1", | ||
"License": "MIT" | ||
} | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
["./tests/NuGetUtility.Test/NuGetUtility.Test.csproj"] |
2 changes: 1 addition & 1 deletion
2
...workflows/assets/urlToLicenseMapping.json → ...ows/assets/Tests/urlToLicenseMapping.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"https://raw.githubusercontent.com/bchavez/Bogus/master/LICENSE": "MIT" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 31 additions & 5 deletions
36
src/NuGetUtility/Wrapper/HttpClientWrapper/FileDownloader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,48 @@ | ||
namespace NuGetUtility.Wrapper.HttpClientWrapper | ||
using System; | ||
|
||
namespace NuGetUtility.Wrapper.HttpClientWrapper | ||
{ | ||
public class FileDownloader : IFileDownloader | ||
{ | ||
private readonly SemaphoreSlim _parallelDownloadLimiter = new SemaphoreSlim(10, 10); | ||
private readonly HttpClient _client; | ||
private readonly string _downloadDirectory; | ||
private const int EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS = 200; | ||
private const int MAX_RETRIES = 5; | ||
|
||
public FileDownloader(HttpClient client, string downloadDirectory) | ||
{ | ||
_client = client; | ||
_downloadDirectory = downloadDirectory; | ||
} | ||
|
||
public async Task DownloadFile(Uri url, string fileName) | ||
public async Task DownloadFile(Uri url, string fileName, CancellationToken token) | ||
{ | ||
await using FileStream file = File.OpenWrite(Path.Combine(_downloadDirectory, fileName)); | ||
await using Stream downloadStream = await _client.GetStreamAsync(url); | ||
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, token); | ||
response.EnsureSuccessStatusCode(); | ||
if (response.StatusCode == System.Net.HttpStatusCode.TooManyRequests) | ||
{ | ||
await Task.Delay((int)Math.Pow(EXPONENTIAL_BACKOFF_WAIT_TIME_MILLISECONDS, i + 1), token); | ||
continue; | ||
} | ||
using Stream downloadStream = await response.Content.ReadAsStreamAsync(token); | ||
|
||
await downloadStream.CopyToAsync(file); | ||
await downloadStream.CopyToAsync(file, token); | ||
return; | ||
} | ||
} | ||
finally | ||
{ | ||
_parallelDownloadLimiter.Release(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.