Fixes locking and retrying in http class

This commit is contained in:
benbierens 2023-10-24 09:41:37 +02:00
parent cc2e8d5992
commit e073f7a881
No known key found for this signature in database
GPG Key ID: FE44815D96D0A1AA
2 changed files with 70 additions and 56 deletions

View File

@ -13,15 +13,15 @@ namespace Core
T HttpGetJson<T>(string route); T HttpGetJson<T>(string route);
TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body); TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body);
string HttpPostJson<TRequest>(string route, TRequest body); string HttpPostJson<TRequest>(string route, TRequest body);
string HttpPostString(string route, string body);
TResponse HttpPostString<TResponse>(string route, string body); TResponse HttpPostString<TResponse>(string route, string body);
string HttpPostStream(string route, Stream stream); string HttpPostStream(string route, Stream stream);
Stream HttpGetStream(string route); Stream HttpGetStream(string route);
T TryJsonDeserialize<T>(string json); T Deserialize<T>(string json);
} }
internal class Http : IHttp internal class Http : IHttp
{ {
private static readonly object httpLock = new object();
private readonly ILog log; private readonly ILog log;
private readonly ITimeSet timeSet; private readonly ITimeSet timeSet;
private readonly Address address; private readonly Address address;
@ -48,25 +48,24 @@ namespace Core
public string HttpGetString(string route) public string HttpGetString(string route)
{ {
return Retry(() => return LockRetry(() =>
{ {
using var client = GetClient(); return GetString(route);
var url = GetUrl() + route;
Log(url, "");
var result = Time.Wait(client.GetAsync(url));
var str = Time.Wait(result.Content.ReadAsStringAsync());
Log(url, str);
return str; ;
}, $"HTTP-GET:{route}"); }, $"HTTP-GET:{route}");
} }
public T HttpGetJson<T>(string route) public T HttpGetJson<T>(string route)
{ {
var json = HttpGetString(route); return LockRetry(() =>
return TryJsonDeserialize<T>(json); {
var json = GetString(route);
return Deserialize<T>(json);
}, $"HTTP-GET:{route}");
} }
public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body) public TResponse HttpPostJson<TRequest, TResponse>(string route, TRequest body)
{
return LockRetry(() =>
{ {
var response = PostJson(route, body); var response = PostJson(route, body);
var json = Time.Wait(response.Content.ReadAsStringAsync()); var json = Time.Wait(response.Content.ReadAsStringAsync());
@ -75,43 +74,34 @@ namespace Core
throw new HttpRequestException(json); throw new HttpRequestException(json);
} }
Log(GetUrl() + route, json); Log(GetUrl() + route, json);
return TryJsonDeserialize<TResponse>(json); return Deserialize<TResponse>(json);
}, $"HTTP-POST-JSON: {route}");
} }
public string HttpPostJson<TRequest>(string route, TRequest body) public string HttpPostJson<TRequest>(string route, TRequest body)
{
return LockRetry(() =>
{ {
var response = PostJson(route, body); var response = PostJson(route, body);
return Time.Wait(response.Content.ReadAsStringAsync()); return Time.Wait(response.Content.ReadAsStringAsync());
} }, $"HTTP-POST-JSON: {route}");
public string HttpPostString(string route, string body)
{
return Retry(() =>
{
using var client = GetClient();
var url = GetUrl() + route;
Log(url, body);
var content = new StringContent(body);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var result = Time.Wait(client.PostAsync(url, content));
var str = Time.Wait(result.Content.ReadAsStringAsync());
Log(url, str);
return str;
}, $"HTTP-POST-STRING: {route}");
} }
public TResponse HttpPostString<TResponse>(string route, string body) public TResponse HttpPostString<TResponse>(string route, string body)
{ {
var response = HttpPostString(route, body); return LockRetry(() =>
{
var response = PostJsonString(route, body);
if (response == null) throw new Exception("Received no response."); if (response == null) throw new Exception("Received no response.");
var result = JsonConvert.DeserializeObject<TResponse>(response); var result = JsonConvert.DeserializeObject<TResponse>(response);
if (result == null) throw new Exception("Failed to deserialize response"); if (result == null) throw new Exception("Failed to deserialize response");
return result; return result;
}, $"HTTO-POST-JSON: {route}");
} }
public string HttpPostStream(string route, Stream stream) public string HttpPostStream(string route, Stream stream)
{ {
return Retry(() => return LockRetry(() =>
{ {
using var client = GetClient(); using var client = GetClient();
var url = GetUrl() + route; var url = GetUrl() + route;
@ -127,7 +117,7 @@ namespace Core
public Stream HttpGetStream(string route) public Stream HttpGetStream(string route)
{ {
return Retry(() => return LockRetry(() =>
{ {
var client = GetClient(); var client = GetClient();
var url = GetUrl() + route; var url = GetUrl() + route;
@ -136,7 +126,7 @@ namespace Core
}, $"HTTP-GET-STREAM: {route}"); }, $"HTTP-GET-STREAM: {route}");
} }
public T TryJsonDeserialize<T>(string json) public T Deserialize<T>(string json)
{ {
var errors = new List<string>(); var errors = new List<string>();
var deserialized = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings() var deserialized = JsonConvert.DeserializeObject<T>(json, new JsonSerializerSettings()
@ -154,7 +144,7 @@ namespace Core
} }
} }
}); });
if (errors.Count() > 0) if (errors.Count > 0)
{ {
throw new JsonSerializationException($"Failed to deserialize JSON '{json}' with exception(s): \n{string.Join("\n", errors)}"); throw new JsonSerializationException($"Failed to deserialize JSON '{json}' with exception(s): \n{string.Join("\n", errors)}");
} }
@ -165,16 +155,37 @@ namespace Core
return deserialized; return deserialized;
} }
private HttpResponseMessage PostJson<TRequest>(string route, TRequest body) private string GetString(string route)
{ {
return Retry(() => using var client = GetClient();
var url = GetUrl() + route;
Log(url, "");
var result = Time.Wait(client.GetAsync(url));
var str = Time.Wait(result.Content.ReadAsStringAsync());
Log(url, str);
return str;
}
private HttpResponseMessage PostJson<TRequest>(string route, TRequest body)
{ {
using var client = GetClient(); using var client = GetClient();
var url = GetUrl() + route; var url = GetUrl() + route;
using var content = JsonContent.Create(body); using var content = JsonContent.Create(body);
Log(url, JsonConvert.SerializeObject(body)); Log(url, JsonConvert.SerializeObject(body));
return Time.Wait(client.PostAsync(url, content)); return Time.Wait(client.PostAsync(url, content));
}, $"HTTP-POST-JSON: {route}"); }
private string PostJsonString(string route, string body)
{
using var client = GetClient();
var url = GetUrl() + route;
Log(url, body);
var content = new StringContent(body);
content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
var result = Time.Wait(client.PostAsync(url, content));
var str = Time.Wait(result.Content.ReadAsStringAsync());
Log(url, str);
return str;
} }
private string GetUrl() private string GetUrl()
@ -194,10 +205,13 @@ namespace Core
} }
} }
private T Retry<T>(Func<T> operation, string description) private T LockRetry<T>(Func<T> operation, string description)
{
lock (httpLock)
{ {
return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description); return Time.Retry(operation, timeSet.HttpMaxNumberOfRetries(), timeSet.HttpCallRetryDelay(), description);
} }
}
private HttpClient GetClient() private HttpClient GetClient()
{ {

View File

@ -40,7 +40,7 @@ namespace CodexPlugin
}; };
} }
var result = http.TryJsonDeserialize<CodexDebugPeerResponse>(str); var result = http.Deserialize<CodexDebugPeerResponse>(str);
result.IsPeerFound = true; result.IsPeerFound = true;
return result; return result;
} }