Retry from B-CDN if unable to connect to cache server.

This commit is contained in:
Unnoen 2021-01-11 19:42:03 +11:00
parent 13c39d3d27
commit 13e0b21e95
No known key found for this signature in database
GPG Key ID: 8F8E42252BA20553

View File

@ -11,6 +11,7 @@ using Wabbajack.Common.Exceptions;
using Wabbajack.Common.Serialization.Json; using Wabbajack.Common.Serialization.Json;
using Wabbajack.Lib.AuthorApi; using Wabbajack.Lib.AuthorApi;
using Wabbajack.Lib.Validation; using Wabbajack.Lib.Validation;
using System.Linq;
namespace Wabbajack.Lib.Downloaders namespace Wabbajack.Lib.Downloaders
{ {
@ -89,7 +90,7 @@ namespace Wabbajack.Lib.Downloaders
if (DomainRemaps.TryGetValue(Url.Host, out var remap)) if (DomainRemaps.TryGetValue(Url.Host, out var remap))
{ {
var builder = new UriBuilder(Url) {Host = remap}; var builder = new UriBuilder(Url) {Host = remap};
using var response = await client.GetAsync($"{builder}/parts/{part.Index}"); using var response = await GetWithCDNRetry(client, $"{builder}/parts/{part.Index}");
if (!response.IsSuccessStatusCode) if (!response.IsSuccessStatusCode)
throw new HttpException((int)response.StatusCode, response.ReasonPhrase ?? "Unknown"); throw new HttpException((int)response.StatusCode, response.ReasonPhrase ?? "Unknown");
await response.Content.CopyToAsync(ostream); await response.Content.CopyToAsync(ostream);
@ -114,6 +115,31 @@ namespace Wabbajack.Lib.Downloaders
return true; return true;
} }
private async Task<HttpResponseMessage> GetWithCDNRetry(Http.Client client, string url, CancellationToken? token = null)
{
int retries = 0;
TOP:
try
{
return await client.GetAsync(url, retry: false, token: token);
}
catch (Exception ex)
{
if (retries > 2)
{
Utils.Log($"Trying CDN...");
var remap = url.Replace(new Uri(url).Host, DomainRemaps.FirstOrDefault(x => x.Value == new Uri(url).Host).Key);
return await client.GetAsync(remap, retry: false, token: token);
}
retries += 1;
Utils.Log($"Error reading {url} retrying [{retries}]");
Utils.Log(ex.ToString());
goto TOP;
}
}
private async Task<HttpResponseMessage> GetWithMirroredRetry(Http.Client client, string url) private async Task<HttpResponseMessage> GetWithMirroredRetry(Http.Client client, string url)
{ {
int retries = 0; int retries = 0;
@ -157,7 +183,7 @@ namespace Wabbajack.Lib.Downloaders
if (DomainRemaps.TryGetValue(Url.Host, out var remap)) if (DomainRemaps.TryGetValue(Url.Host, out var remap))
{ {
var builder = new UriBuilder(Url) {Host = remap}; var builder = new UriBuilder(Url) {Host = remap};
using var data = await client.GetAsync(builder + "/definition.json.gz", token: token); using var data = await GetWithCDNRetry(client, builder + "/definition.json.gz", token: token);
await using var gz = new GZipStream(await data.Content.ReadAsStreamAsync(), await using var gz = new GZipStream(await data.Content.ReadAsStreamAsync(),
CompressionMode.Decompress); CompressionMode.Decompress);
return gz.FromJson<CDNFileDefinition>(); return gz.FromJson<CDNFileDefinition>();