1
0
mirror of https://github.com/Radarr/Radarr.git synced 2026-04-28 23:07:13 -04:00

Async HttpClient and list lookup

This commit is contained in:
ta264
2020-05-17 11:44:10 +01:00
committed by Qstick
parent c64c2d9f27
commit c8a2af867e
10 changed files with 145 additions and 53 deletions
+25 -7
View File
@@ -1,5 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using NLog;
using NzbDrone.Common.Cache;
@@ -8,6 +9,7 @@ namespace NzbDrone.Common.TPL
public interface IRateLimitService
{
void WaitAndPulse(string key, TimeSpan interval);
Task WaitAndPulseAsync(string key, TimeSpan interval);
}
public class RateLimitService : IRateLimitService
@@ -23,13 +25,7 @@ namespace NzbDrone.Common.TPL
public void WaitAndPulse(string key, TimeSpan interval)
{
var waitUntil = _rateLimitStore.AddOrUpdate(key,
(s) => DateTime.UtcNow + interval,
(s, i) => new DateTime(Math.Max(DateTime.UtcNow.Ticks, i.Ticks), DateTimeKind.Utc) + interval);
waitUntil -= interval;
var delay = waitUntil - DateTime.UtcNow;
var delay = GetDelay(key, interval);
if (delay.TotalSeconds > 0.0)
{
@@ -37,5 +33,27 @@ namespace NzbDrone.Common.TPL
System.Threading.Thread.Sleep(delay);
}
}
public async Task WaitAndPulseAsync(string key, TimeSpan interval)
{
var delay = GetDelay(key, interval);
if (delay.TotalSeconds > 0.0)
{
_logger.Trace("Rate Limit triggered, delaying '{0}' for {1:0.000} sec", key, delay.TotalSeconds);
await Task.Delay(delay);
}
}
private TimeSpan GetDelay(string key, TimeSpan interval)
{
var waitUntil = _rateLimitStore.AddOrUpdate(key,
(s) => DateTime.UtcNow + interval,
(s, i) => new DateTime(Math.Max(DateTime.UtcNow.Ticks, i.Ticks), DateTimeKind.Utc) + interval);
waitUntil -= interval;
return waitUntil - DateTime.UtcNow;
}
}
}