diff --git a/src/NzbDrone.Common.Test/Http/HttpRateLimitKeyFactoryFixture.cs b/src/NzbDrone.Common.Test/Http/HttpRateLimitKeyFactoryFixture.cs new file mode 100644 index 000000000..df08a7ef4 --- /dev/null +++ b/src/NzbDrone.Common.Test/Http/HttpRateLimitKeyFactoryFixture.cs @@ -0,0 +1,30 @@ +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Common.Http; + +namespace NzbDrone.Common.Test.Http +{ + [TestFixture] + public class HttpRateLimitKeyFactoryFixture + { + [TestCase("http://127.0.0.2:9117/jackett/api/v2.0/indexers/viva/results/torznab/api?t=search&cat=5000,5070,100030,100041", "127.0.0.2:9117/jackett/api/v2.0/indexers/viva")] + public void should_detect_jackett(string url, string expectedKey) + { + var request = new HttpRequest(url); + + var key = HttpRateLimitKeyFactory.GetRateLimitKey(request); + + key.Should().Be(expectedKey); + } + + [TestCase("http://127.0.0.2:9117/jackett", "127.0.0.2")] + public void should_default_to_host(string url, string expectedKey) + { + var request = new HttpRequest(url); + + var key = HttpRateLimitKeyFactory.GetRateLimitKey(request); + + key.Should().Be(expectedKey); + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index a2ac9d2d5..45e4a1f18 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -109,7 +109,7 @@ namespace NzbDrone.Common.Http if (request.RateLimit != TimeSpan.Zero) { - _rateLimitService.WaitAndPulse(request.Url.Host, request.RateLimit); + _rateLimitService.WaitAndPulse(HttpRateLimitKeyFactory.GetRateLimitKey(request), request.RateLimit); } _logger.Trace(request); diff --git a/src/NzbDrone.Common/Http/HttpRateLimitKeyFactory.cs b/src/NzbDrone.Common/Http/HttpRateLimitKeyFactory.cs new file mode 100644 index 000000000..e4a627e75 --- /dev/null +++ b/src/NzbDrone.Common/Http/HttpRateLimitKeyFactory.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace NzbDrone.Common.Http +{ + public static class HttpRateLimitKeyFactory + { + // Use a different key for jackett instances to prevent hitting the ratelimit for multiple separate indexers. + private static readonly Regex _regex = new Regex(@"^https?://(.+/jackett/api/v2.0/indexers/\w+)/", RegexOptions.Compiled); + + public static string GetRateLimitKey(HttpRequest request) + { + var match = _regex.Match(request.Url.ToString()); + + if (match.Success) + { + return match.Groups[1].Value; + } + + return request.Url.Host; + } + + } +}