From 30b6c4680b5f747c02ed1216813fd324ee9c19e3 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 22 May 2023 21:38:23 +0200 Subject: [PATCH] Better Rate-Limits Added Logger to DownloadClient --- Tranga/Connector.cs | 43 +++++++++++++++++++++++++---------- Tranga/Connectors/MangaDex.cs | 2 +- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index 83c21ff..b084514 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -169,19 +169,21 @@ public abstract class Connector private static readonly HttpClient Client = new(); private readonly Dictionary _lastExecutedRateLimit; - private readonly Dictionary _RateLimit; + private readonly Dictionary _rateLimit; + private Logger? logger; /// /// Creates a httpClient /// /// minimum delay between requests (to avoid spam) /// Rate limits for requests. byte is RequestType, int maximum requests per minute for RequestType - public DownloadClient(Dictionary rateLimitRequestsPerMinute) + public DownloadClient(Dictionary rateLimitRequestsPerMinute, Logger? logger) { + this.logger = logger; _lastExecutedRateLimit = new(); - _RateLimit = new(); + _rateLimit = new(); foreach(KeyValuePair limit in rateLimitRequestsPerMinute) - _RateLimit.Add(limit.Key, TimeSpan.FromMinutes(1).Divide(limit.Value)); + _rateLimit.Add(limit.Key, TimeSpan.FromMinutes(1).Divide(limit.Value)); } /// @@ -192,19 +194,36 @@ public abstract class Connector /// RequestResult with StatusCode and Stream of received data public RequestResult MakeRequest(string url, byte requestType) { - if (_RateLimit.TryGetValue(requestType, out TimeSpan value)) + if (_rateLimit.TryGetValue(requestType, out TimeSpan value)) _lastExecutedRateLimit.TryAdd(requestType, DateTime.Now.Subtract(value)); else + { + logger?.WriteLine(this.GetType().ToString(), "RequestType not configured for rate-limit."); return new RequestResult(HttpStatusCode.NotAcceptable, Stream.Null); - - - while(DateTime.Now.Subtract(_lastExecutedRateLimit[requestType]) < _RateLimit[requestType]) - Thread.Sleep(10); - _lastExecutedRateLimit[requestType] = DateTime.Now; + } - HttpRequestMessage requestMessage = new(HttpMethod.Get, url); - HttpResponseMessage response = Client.Send(requestMessage); + TimeSpan rateLimitTimeout = _rateLimit[requestType] + .Subtract(DateTime.Now.Subtract(_lastExecutedRateLimit[requestType])); + + Thread.Sleep(rateLimitTimeout); + + HttpResponseMessage? response = null; + while (response is null) + { + try + { + HttpRequestMessage requestMessage = new(HttpMethod.Get, url); + _lastExecutedRateLimit[requestType] = DateTime.Now; + response = Client.Send(requestMessage); + } + catch (HttpRequestException e) + { + Thread.Sleep(_rateLimit[requestType] * 2); + } + } Stream resultString = response.IsSuccessStatusCode ? response.Content.ReadAsStream() : Stream.Null; + if (!response.IsSuccessStatusCode) + logger?.WriteLine(this.GetType().ToString(), $"Request-Error {response.StatusCode}: {response.ReasonPhrase}"); return new RequestResult(response.StatusCode, resultString); } diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 3afc6b0..f037a7d 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -28,7 +28,7 @@ public class MangaDex : Connector {(byte)RequestType.AtHomeServer, 40}, {(byte)RequestType.Cover, 250}, {(byte)RequestType.Author, 250} - }); + }, logger); } public override Publication[] GetPublications(string publicationTitle = "")