Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Retry failed web requests, fix install stanza error #3166

Merged
merged 1 commit into from Oct 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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