mirror of
https://github.com/C9Glax/tranga.git
synced 2025-10-11 13:19:48 +02:00
Use DelegatingHandler for RateLimits
Some checks failed
Docker Image CI / build (push) Has been cancelled
Some checks failed
Docker Image CI / build (push) Has been cancelled
This commit is contained in:
@@ -1,56 +1,6 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Net;
|
||||
using log4net;
|
||||
namespace API.MangaDownloadClients;
|
||||
|
||||
namespace API.MangaDownloadClients;
|
||||
|
||||
public abstract class DownloadClient
|
||||
public interface IDownloadClient
|
||||
{
|
||||
private static readonly ConcurrentDictionary<RequestType, DateTime> LastExecutedRateLimit = new();
|
||||
protected ILog Log { get; init; }
|
||||
|
||||
protected DownloadClient()
|
||||
{
|
||||
this.Log = LogManager.GetLogger(GetType());
|
||||
}
|
||||
|
||||
// TODO Requests still go too fast across threads!
|
||||
public RequestResult MakeRequest(string url, RequestType requestType, string? referrer = null, string? clickButton = null)
|
||||
{
|
||||
Log.Debug($"Requesting {requestType} {url}");
|
||||
|
||||
// If we don't have a RequestLimit set for a Type, use the default one
|
||||
if (!Tranga.Settings.RequestLimits.ContainsKey(requestType))
|
||||
requestType = RequestType.Default;
|
||||
|
||||
int rateLimit = Tranga.Settings.RequestLimits[requestType];
|
||||
// TODO this probably needs a better check whether the useragent matches...
|
||||
// If the UserAgent is the default one, do not exceed the default request-limits.
|
||||
if (Tranga.Settings.UserAgent == TrangaSettings.DefaultUserAgent && rateLimit > TrangaSettings.DefaultRequestLimits[requestType])
|
||||
rateLimit = TrangaSettings.DefaultRequestLimits[requestType];
|
||||
|
||||
// Apply the delay
|
||||
TimeSpan timeBetweenRequests = TimeSpan.FromMinutes(1).Divide(rateLimit);
|
||||
DateTime now = DateTime.UtcNow;
|
||||
LastExecutedRateLimit.TryAdd(requestType, now.Subtract(timeBetweenRequests));
|
||||
|
||||
TimeSpan rateLimitTimeout = timeBetweenRequests.Subtract(now.Subtract(LastExecutedRateLimit[requestType]));
|
||||
Log.Debug($"Request limit {requestType} {rateLimit}/Minute timeBetweenRequests: {timeBetweenRequests:ss'.'fffff}");
|
||||
|
||||
if (rateLimitTimeout > TimeSpan.Zero)
|
||||
{
|
||||
Log.Info($"Waiting {rateLimitTimeout} for {url}");
|
||||
Thread.Sleep(rateLimitTimeout);
|
||||
}
|
||||
|
||||
// Make the request
|
||||
RequestResult result = MakeRequestInternal(url, referrer, clickButton);
|
||||
|
||||
// Update the time the last request was made
|
||||
LastExecutedRateLimit[requestType] = DateTime.UtcNow;
|
||||
Log.Debug($"Result {url}: {result}");
|
||||
return result;
|
||||
}
|
||||
|
||||
internal abstract RequestResult MakeRequestInternal(string url, string? referrer = null, string? clickButton = null);
|
||||
internal Task<HttpResponseMessage> MakeRequest(string url, RequestType requestType, string? referrer = null);
|
||||
}
|
Reference in New Issue
Block a user