Skip to content

Commit

Permalink
feat: added retry mechanism for Tags Client (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanJosipovic authored Oct 1, 2021
1 parent aa2ef0e commit 2219d40
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/ACR-SyncTool.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
<PackageReference Include="Docker.DotNet" Version="3.125.5" />
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.0-rc.1.21451.13" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0-rc.1.21451.13" />
<PackageReference Include="Polly" Version="7.2.2" />
<PackageReference Include="SemanticVersioning" Version="2.0.0" />
<PackageReference Include="System.Net.Http.Json" Version="6.0.0-rc.1.21451.13" />
</ItemGroup>

<ItemGroup>
<None Include="..\README.md" Pack="true" PackagePath="\"/>
<None Include="..\README.md" Pack="true" PackagePath="\" />
</ItemGroup>

</Project>
17 changes: 14 additions & 3 deletions src/DockerClient/DockerTagClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ private async Task<List<string>> GetTagsRecursive(string url)

var request = CreateRequest(url);

var response = await httpClient.SendAsync(request);
HttpStatusCode[] httpStatusCodesWorthRetrying = {
HttpStatusCode.RequestTimeout, // 408
HttpStatusCode.TooManyRequests, // 429
HttpStatusCode.InternalServerError, // 500
HttpStatusCode.BadGateway, // 502
HttpStatusCode.ServiceUnavailable, // 503
HttpStatusCode.GatewayTimeout // 504
};

var response = await Policy
.HandleResult<HttpResponseMessage>(r => httpStatusCodesWorthRetrying.Contains(r.StatusCode))
.WaitAndRetryAsync(5, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)))
.ExecuteAsync(async () => await httpClient.SendAsync(request));

if (response.StatusCode == HttpStatusCode.Unauthorized)
{
Expand All @@ -67,8 +79,7 @@ private async Task<List<string>> GetTagsRecursive(string url)

var pageQuery = matches.Groups[1].Value;

// Delay recursion to prevent "429 Too Many Requests" errors
await Task.Delay(TimeSpan.FromSeconds(10));
await Task.Delay(TimeSpan.FromSeconds(2));

var newTags = await GetTagsRecursive($"{(Https ? "https" : "http")}://{host}{pageQuery}");

Expand Down
21 changes: 11 additions & 10 deletions src/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
global using ACR_SyncTool.DockerClient.Authentication;
global using ACR_SyncTool.DockerClient;
global using ACR_SyncTool.DockerClient.Authentication;
global using ACR_SyncTool.DockerClient.Extensionis;
global using ACR_SyncTool.DockerClient.OAuth;
global using ACR_SyncTool.DockerClient;
global using ACR_SyncTool.Models;
global using Azure.Containers.ContainerRegistry;
global using Azure.Identity;
global using Docker.DotNet.Models;
global using Docker.DotNet;
global using Microsoft.Extensions.Configuration.Json;
global using Docker.DotNet.Models;
global using Microsoft.Extensions.Configuration;
global using Microsoft.Extensions.Configuration.Json;
global using Microsoft.Extensions.DependencyInjection;
global using Microsoft.Extensions.Hosting;
global using Microsoft.Extensions.Logging;
global using Polly;
global using System;
global using System.Collections.Generic;
global using System.Diagnostics;
global using System.IO;
global using System.Linq;
global using System.Net;
global using System.Net.Http;
global using System.Net.Http.Headers;
global using System.Net.Http.Json;
global using System.Net.Http;
global using System.Net;
global using System.Security.Authentication;
global using System.Security.Cryptography.X509Certificates;
global using System.Text.Json.Serialization;
global using System.Text;
global using System.Text.Json;
global using System.Text.Json.Serialization;
global using System.Text.RegularExpressions;
global using System.Text;
global using System.Threading.Tasks;
global using System.Threading;
global using System;
global using System.Threading.Tasks;

0 comments on commit 2219d40

Please sign in to comment.