Skip to content

Commit dc3c015

Browse files
surayya-MSbaronfel
authored andcommitted
add download retries
1 parent cefa4ac commit dc3c015

File tree

1 file changed

+33
-16
lines changed
  • src/Containers/Microsoft.NET.Build.Containers/Registry

1 file changed

+33
-16
lines changed

src/Containers/Microsoft.NET.Build.Containers/Registry/Registry.cs

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ internal sealed class Registry
7474
private const string DockerHubRegistry1 = "registry-1.docker.io";
7575
private const string DockerHubRegistry2 = "registry.hub.docker.com";
7676
private static readonly int s_defaultChunkSizeBytes = 1024 * 64;
77+
private const int MaxDownloadRetries = 5;
78+
private const int FixedDownloadDelayInSeconds = 1;
7779

7880
private readonly ILogger _logger;
7981
private readonly IRegistryAPI _registryAPI;
@@ -401,33 +403,48 @@ public async Task<string> DownloadBlobAsync(string repository, Descriptor descri
401403
{
402404
cancellationToken.ThrowIfCancellationRequested();
403405
string localPath = ContentStore.PathForDescriptor(descriptor);
404-
406+
405407
if (File.Exists(localPath))
406408
{
407409
// Assume file is up to date and just return it
408410
return localPath;
409411
}
410-
412+
411413
string tempTarballPath = ContentStore.GetTempFile();
412-
413-
try
414+
415+
int retryCount = 0;
416+
while (retryCount < MaxDownloadRetries)
414417
{
415-
// No local copy, so download one
416-
using Stream responseStream = await _registryAPI.Blob.GetStreamAsync(repository, descriptor.Digest, cancellationToken).ConfigureAwait(false);
417-
418-
using (FileStream fs = File.Create(tempTarballPath))
418+
try
419419
{
420-
await responseStream.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);
420+
// No local copy, so download one
421+
using Stream responseStream = await _registryAPI.Blob.GetStreamAsync(repository, descriptor.Digest, cancellationToken).ConfigureAwait(false);
422+
423+
using (FileStream fs = File.Create(tempTarballPath))
424+
{
425+
await responseStream.CopyToAsync(fs, cancellationToken).ConfigureAwait(false);
426+
}
427+
428+
// Break the loop if successful
429+
break;
430+
}
431+
catch (Exception ex)
432+
{
433+
retryCount++;
434+
if (retryCount >= MaxDownloadRetries)
435+
{
436+
throw new UnableToDownloadFromRepositoryException(repository);
437+
}
438+
439+
_logger.LogTrace("Download attempt {0}/{1} for repository '{2}' failed. Error: {3}", retryCount, MaxDownloadRetries, repository, ex.ToString());
440+
441+
// Wait before retrying
442+
await Task.Delay(TimeSpan.FromSeconds(FixedDownloadDelayInSeconds), cancellationToken).ConfigureAwait(false);
421443
}
422444
}
423-
catch (Exception)
424-
{
425-
throw new UnableToDownloadFromRepositoryException(repository);
426-
}
427-
cancellationToken.ThrowIfCancellationRequested();
428-
445+
429446
File.Move(tempTarballPath, localPath, overwrite: true);
430-
447+
431448
return localPath;
432449
}
433450

0 commit comments

Comments
 (0)