Merge branch 'develop'
This commit is contained in:
@@ -20,6 +20,20 @@ namespace UmlautAdaptarr.Services
|
|||||||
_cache = cache;
|
_cache = cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async Task EnsureMinimumDelayAsync(string targetUri)
|
||||||
|
{
|
||||||
|
var host = new Uri(targetUri).Host;
|
||||||
|
if (_lastRequestTimes.TryGetValue(host, out var lastRequestTime))
|
||||||
|
{
|
||||||
|
var timeSinceLastRequest = DateTimeOffset.Now - lastRequestTime;
|
||||||
|
if (timeSinceLastRequest < TimeSpan.FromMilliseconds(1500))
|
||||||
|
{
|
||||||
|
await Task.Delay(TimeSpan.FromMilliseconds(1500) - timeSinceLastRequest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_lastRequestTimes[host] = DateTimeOffset.Now;
|
||||||
|
}
|
||||||
|
|
||||||
public async Task<HttpResponseMessage> ProxyRequestAsync(HttpContext context, string targetUri)
|
public async Task<HttpResponseMessage> ProxyRequestAsync(HttpContext context, string targetUri)
|
||||||
{
|
{
|
||||||
if (!HttpMethods.IsGet(context.Request.Method))
|
if (!HttpMethods.IsGet(context.Request.Method))
|
||||||
@@ -27,18 +41,6 @@ namespace UmlautAdaptarr.Services
|
|||||||
throw new ArgumentException("Only GET requests are supported", context.Request.Method);
|
throw new ArgumentException("Only GET requests are supported", context.Request.Method);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Throttling mechanism
|
|
||||||
var host = new Uri(targetUri).Host;
|
|
||||||
if (_lastRequestTimes.TryGetValue(host, out var lastRequestTime))
|
|
||||||
{
|
|
||||||
var timeSinceLastRequest = DateTimeOffset.Now - lastRequestTime;
|
|
||||||
if (timeSinceLastRequest < TimeSpan.FromSeconds(3))
|
|
||||||
{
|
|
||||||
await Task.Delay(TimeSpan.FromSeconds(3) - timeSinceLastRequest);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
_lastRequestTimes[host] = DateTimeOffset.Now;
|
|
||||||
|
|
||||||
// Check cache
|
// Check cache
|
||||||
if (_cache.TryGetValue(targetUri, out HttpResponseMessage cachedResponse))
|
if (_cache.TryGetValue(targetUri, out HttpResponseMessage cachedResponse))
|
||||||
{
|
{
|
||||||
@@ -46,6 +48,8 @@ namespace UmlautAdaptarr.Services
|
|||||||
return cachedResponse!;
|
return cachedResponse!;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await EnsureMinimumDelayAsync(targetUri);
|
||||||
|
|
||||||
var requestMessage = new HttpRequestMessage
|
var requestMessage = new HttpRequestMessage
|
||||||
{
|
{
|
||||||
RequestUri = new Uri(targetUri),
|
RequestUri = new Uri(targetUri),
|
||||||
|
|||||||
@@ -13,13 +13,14 @@ namespace UmlautAdaptarr.Services
|
|||||||
private async Task EnsureMinimumDelayAsync()
|
private async Task EnsureMinimumDelayAsync()
|
||||||
{
|
{
|
||||||
var sinceLastRequest = DateTime.Now - lastRequestTime;
|
var sinceLastRequest = DateTime.Now - lastRequestTime;
|
||||||
if (sinceLastRequest < TimeSpan.FromSeconds(2))
|
if (sinceLastRequest < TimeSpan.FromSeconds(1))
|
||||||
{
|
{
|
||||||
await Task.Delay(TimeSpan.FromSeconds(2) - sinceLastRequest);
|
await Task.Delay(TimeSpan.FromSeconds(1) - sinceLastRequest);
|
||||||
}
|
}
|
||||||
lastRequestTime = DateTime.Now;
|
lastRequestTime = DateTime.Now;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO add cache, TODO add bulk request
|
||||||
public async Task<(string? germanTitle, string[]? aliases)> FetchGermanTitleAndAliasesByExternalIdAsync(string mediaType, string externalId)
|
public async Task<(string? germanTitle, string[]? aliases)> FetchGermanTitleAndAliasesByExternalIdAsync(string mediaType, string externalId)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
Reference in New Issue
Block a user