Skip to content

Commit

Permalink
Merge #3166 Retry failed web requests, fix install stanza error
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Oct 4, 2020
2 parents 7482dc0 + 822f978 commit 07e44f1
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -20,6 +20,7 @@ All notable changes to this project will be documented in this file.
- [Build] Cache build tools (#3127 by: HebaruSan; reviewed: DasSkelett)
- [Netkan] Remove invalid zip files in NetKAN (#3156 by DasSkelett; reviewed: HebaruSan)
- [Build] Tests should cleanup its environment (#3132 by Olympic1; reviewed: HebaruSan)
- [Netkan] Retry failed web requests, fix install stanza error (#3166 by: HebaruSan; reviewed: DasSkelett)

## v1.28.0 (Dyson)

Expand Down
29 changes: 24 additions & 5 deletions Core/Net/Net.cs
Expand Up @@ -5,10 +5,11 @@
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using Autofac;
using ChinhDo.Transactions.FileManager;
using CKAN.Configuration;
using log4net;
using CKAN.Configuration;

namespace CKAN
{
Expand All @@ -21,6 +22,9 @@ public class Net
// The user agent that we report to web sites
public static string UserAgentString = "Mozilla/4.0 (compatible; CKAN)";

private const int MaxRetries = 3;
private const int RetryDelayMilliseconds = 100;

private static readonly ILog log = LogManager.GetLogger(typeof(Net));

public static readonly Dictionary<string, Uri> ThrottledHosts = new Dictionary<string, Uri>()
Expand Down Expand Up @@ -211,12 +215,27 @@ public static string DownloadText(Uri url, string authToken = "", string mimeTyp
agent.Headers.Add("Accept", mimeType);
}

string content = agent.DownloadString(url.OriginalString);
string header = agent.ResponseHeaders.ToString();
for (int whichAttempt = 0; whichAttempt < MaxRetries + 1; ++whichAttempt)
{
try
{
string content = agent.DownloadString(url.OriginalString);
string header = agent.ResponseHeaders.ToString();

log.DebugFormat("Response from {0}:\r\n\r\n{1}\r\n{2}", url, header, content);
log.DebugFormat("Response from {0}:\r\n\r\n{1}\r\n{2}", url, header, content);

return content;
return content;
}
catch (WebException wex) when (wex.Status != WebExceptionStatus.ProtocolError && whichAttempt < MaxRetries)
{
log.DebugFormat("Web request failed with non-protocol error, retrying in {0} milliseconds: {1}", RetryDelayMilliseconds * whichAttempt, wex.Message);
Thread.Sleep(RetryDelayMilliseconds * whichAttempt);
}
}
// Should never get here, because we don't catch any exceptions
// in the final iteration of the above for loop. They should be
// thrown to the calling code, or the call should succeed.
return null;
}

public static Uri ResolveRedirect(Uri uri)
Expand Down
11 changes: 9 additions & 2 deletions Core/Types/CkanModule.cs
Expand Up @@ -646,9 +646,16 @@ public override string ToString()
public string DescribeInstallStanzas()
{
List<string> descriptions = new List<string>();
foreach (ModuleInstallDescriptor mid in install)
if (install != null)
{
descriptions.Add(mid.DescribeMatch());
foreach (ModuleInstallDescriptor mid in install)
{
descriptions.Add(mid.DescribeMatch());
}
}
else
{
descriptions.Add(ModuleInstallDescriptor.DefaultInstallStanza(identifier).DescribeMatch());
}
return string.Join(", ", descriptions);
}
Expand Down
3 changes: 2 additions & 1 deletion Core/Types/ModuleInstallDescriptor.cs
Expand Up @@ -418,7 +418,8 @@ public List<InstallableFile> FindInstallableFiles(ZipFile zipfile, KSP ksp)
: zipfile.Cast<ZipEntry>()
.Select(entry => inst_pattern.Match(entry.Name.Replace('\\', '/')))
.Where(match => match.Success)
.Min(match => match.Index);
.DefaultIfEmpty()
.Min(match => match?.Index);

// O(N^2) solution, as we're walking the zipfile for each stanza.
// Surely there's a better way, although this is fast enough we may not care.
Expand Down
2 changes: 1 addition & 1 deletion Netkan/Transformers/AvcTransformer.cs
Expand Up @@ -94,7 +94,7 @@ public IEnumerable<Metadata> Transform(Metadata metadata, TransformOptions opts)
}
catch (Exception e)
{
Log.WarnFormat("Error fetching remote version file: {0}", e.Message);
Log.WarnFormat("Error fetching remote version file {0}: {1}", remoteUri, e.Message);
Log.Debug(e);
}
}
Expand Down

0 comments on commit 07e44f1

Please sign in to comment.