New: Add hour as interval unit for indexer limits

ui-volume-factors
Bogdan 1 year ago
parent d44b946d30
commit 96413f99c7

@ -7,20 +7,33 @@ namespace NzbDrone.Core.Indexers
{ {
public IndexerCommonSettingsValidator() public IndexerCommonSettingsValidator()
{ {
RuleFor(c => c.QueryLimit).GreaterThan(0).When(c => c.QueryLimit.HasValue).WithMessage("Should be greater than zero"); RuleFor(c => c.QueryLimit)
.GreaterThan(0)
.When(c => c.QueryLimit.HasValue)
.WithMessage("Should be greater than zero");
RuleFor(c => c.GrabLimit).GreaterThan(0).When(c => c.GrabLimit.HasValue).WithMessage("Should be greater than zero"); RuleFor(c => c.GrabLimit)
.GreaterThan(0)
.When(c => c.GrabLimit.HasValue)
.WithMessage("Should be greater than zero");
} }
} }
public class IndexerBaseSettings public class IndexerBaseSettings
{ {
private static readonly IndexerCommonSettingsValidator Validator = new (); [FieldDefinition(1, Type = FieldType.Number, Label = "Query Limit", HelpText = "The number of max queries as specified by the respective unit that Prowlarr will allow to the site", Advanced = true)]
[FieldDefinition(1, Type = FieldType.Number, Label = "Query Limit", HelpText = "The number of queries within a rolling 24 hour period Prowlarr will allow to the site", Advanced = true)]
public int? QueryLimit { get; set; } public int? QueryLimit { get; set; }
[FieldDefinition(2, Type = FieldType.Number, Label = "Grab Limit", HelpText = "The number of grabs within a rolling 24 hour period Prowlarr will allow to the site", Advanced = true)] [FieldDefinition(2, Type = FieldType.Number, Label = "Grab Limit", HelpText = "The number of max grabs as specified by the respective unit that Prowlarr will allow to the site", Advanced = true)]
public int? GrabLimit { get; set; } public int? GrabLimit { get; set; }
[FieldDefinition(3, Type = FieldType.Select, SelectOptions = typeof(IndexerLimitsUnit), Label = "Limits Unit", HelpText = "The unit of time for counting limits per indexer", Advanced = true)]
public int LimitsUnit { get; set; } = (int)IndexerLimitsUnit.Day;
}
public enum IndexerLimitsUnit
{
Day = 0,
Hour = 1
} }
} }

@ -11,6 +11,7 @@ namespace NzbDrone.Core.Indexers
bool AtQueryLimit(IndexerDefinition indexer); bool AtQueryLimit(IndexerDefinition indexer);
int CalculateRetryAfterDownloadLimit(IndexerDefinition indexer); int CalculateRetryAfterDownloadLimit(IndexerDefinition indexer);
int CalculateRetryAfterQueryLimit(IndexerDefinition indexer); int CalculateRetryAfterQueryLimit(IndexerDefinition indexer);
int CalculateIntervalLimitHours(IndexerDefinition indexer);
} }
public class IndexerLimitService : IIndexerLimitService public class IndexerLimitService : IIndexerLimitService
@ -27,19 +28,20 @@ namespace NzbDrone.Core.Indexers
public bool AtDownloadLimit(IndexerDefinition indexer) public bool AtDownloadLimit(IndexerDefinition indexer)
{ {
if (indexer.Id > 0 && ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit.HasValue) if (indexer is { Id: > 0 } && ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit.HasValue)
{ {
var grabCount = _historyService.CountSince(indexer.Id, DateTime.Now.AddHours(-24), new List<HistoryEventType> { HistoryEventType.ReleaseGrabbed }); var intervalLimitHours = CalculateIntervalLimitHours(indexer);
var grabCount = _historyService.CountSince(indexer.Id, DateTime.Now.AddHours(-1 * intervalLimitHours), new List<HistoryEventType> { HistoryEventType.ReleaseGrabbed });
var grabLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit; var grabLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit;
if (grabCount >= grabLimit) if (grabCount >= grabLimit)
{ {
_logger.Info("Indexer {0} has performed {1} of possible {2} grabs in last 24 hours, exceeding the maximum grab limit", indexer.Name, grabCount, grabLimit); _logger.Info("Indexer {0} has performed {1} of possible {2} grabs in last {3} hour(s), exceeding the maximum grab limit", indexer.Name, grabCount, grabLimit, intervalLimitHours);
return true; return true;
} }
_logger.Debug("Indexer {0} has performed {1} of possible {2} grabs in last 24 hours, proceeding", indexer.Name, grabCount, grabLimit); _logger.Debug("Indexer {0} has performed {1} of possible {2} grabs in last {3} hour(s), proceeding", indexer.Name, grabCount, grabLimit, intervalLimitHours);
} }
return false; return false;
@ -47,19 +49,20 @@ namespace NzbDrone.Core.Indexers
public bool AtQueryLimit(IndexerDefinition indexer) public bool AtQueryLimit(IndexerDefinition indexer)
{ {
if (indexer.Id > 0 && ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit.HasValue) if (indexer is { Id: > 0 } && ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit.HasValue)
{ {
var queryCount = _historyService.CountSince(indexer.Id, DateTime.Now.AddHours(-24), new List<HistoryEventType> { HistoryEventType.IndexerQuery, HistoryEventType.IndexerRss }); var intervalLimitHours = CalculateIntervalLimitHours(indexer);
var queryCount = _historyService.CountSince(indexer.Id, DateTime.Now.AddHours(-1 * intervalLimitHours), new List<HistoryEventType> { HistoryEventType.IndexerQuery, HistoryEventType.IndexerRss });
var queryLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit; var queryLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit;
if (queryCount >= queryLimit) if (queryCount >= queryLimit)
{ {
_logger.Info("Indexer {0} has performed {1} of possible {2} queries in last 24 hours, exceeding the maximum query limit", indexer.Name, queryCount, queryLimit); _logger.Info("Indexer {0} has performed {1} of possible {2} queries in last {3} hour(s), exceeding the maximum query limit", indexer.Name, queryCount, queryLimit, intervalLimitHours);
return true; return true;
} }
_logger.Debug("Indexer {0} has performed {1} of possible {2} queries in last 24 hours, proceeding", indexer.Name, queryCount, queryLimit); _logger.Debug("Indexer {0} has performed {1} of possible {2} queries in last {3} hour(s), proceeding", indexer.Name, queryCount, queryLimit, intervalLimitHours);
} }
return false; return false;
@ -67,15 +70,16 @@ namespace NzbDrone.Core.Indexers
public int CalculateRetryAfterDownloadLimit(IndexerDefinition indexer) public int CalculateRetryAfterDownloadLimit(IndexerDefinition indexer)
{ {
if (indexer.Id > 0 && ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit.HasValue) if (indexer is { Id: > 0 } && ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit.HasValue)
{ {
var intervalLimitHours = CalculateIntervalLimitHours(indexer);
var grabLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit.GetValueOrDefault(); var grabLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.GrabLimit.GetValueOrDefault();
var firstHistorySince = _historyService.FindFirstForIndexerSince(indexer.Id, DateTime.Now.AddHours(-24), new List<HistoryEventType> { HistoryEventType.ReleaseGrabbed }, grabLimit); var firstHistorySince = _historyService.FindFirstForIndexerSince(indexer.Id, DateTime.Now.AddHours(-1 * intervalLimitHours), new List<HistoryEventType> { HistoryEventType.ReleaseGrabbed }, grabLimit);
if (firstHistorySince != null) if (firstHistorySince != null)
{ {
return Convert.ToInt32(firstHistorySince.Date.ToLocalTime().AddHours(24).Subtract(DateTime.Now).TotalSeconds); return Convert.ToInt32(firstHistorySince.Date.ToLocalTime().AddHours(intervalLimitHours).Subtract(DateTime.Now).TotalSeconds);
} }
} }
@ -84,19 +88,35 @@ namespace NzbDrone.Core.Indexers
public int CalculateRetryAfterQueryLimit(IndexerDefinition indexer) public int CalculateRetryAfterQueryLimit(IndexerDefinition indexer)
{ {
if (indexer.Id > 0 && ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit.HasValue) if (indexer is { Id: > 0 } && ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit.HasValue)
{ {
var intervalLimitHours = CalculateIntervalLimitHours(indexer);
var queryLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit.GetValueOrDefault(); var queryLimit = ((IIndexerSettings)indexer.Settings).BaseSettings.QueryLimit.GetValueOrDefault();
var firstHistorySince = _historyService.FindFirstForIndexerSince(indexer.Id, DateTime.Now.AddHours(-24), new List<HistoryEventType> { HistoryEventType.IndexerQuery, HistoryEventType.IndexerRss }, queryLimit); var firstHistorySince = _historyService.FindFirstForIndexerSince(indexer.Id, DateTime.Now.AddHours(-1 * intervalLimitHours), new List<HistoryEventType> { HistoryEventType.IndexerQuery, HistoryEventType.IndexerRss }, queryLimit);
if (firstHistorySince != null) if (firstHistorySince != null)
{ {
return Convert.ToInt32(firstHistorySince.Date.ToLocalTime().AddHours(24).Subtract(DateTime.Now).TotalSeconds); return Convert.ToInt32(firstHistorySince.Date.ToLocalTime().AddHours(intervalLimitHours).Subtract(DateTime.Now).TotalSeconds);
} }
} }
return 0; return 0;
} }
public int CalculateIntervalLimitHours(IndexerDefinition indexer)
{
if (indexer is { Id: > 0 })
{
return ((IIndexerSettings)indexer.Settings).BaseSettings.LimitsUnit switch
{
(int)IndexerLimitsUnit.Hour => 1,
_ => 24
};
}
// Fallback to limits per day
return 24;
}
} }
} }

@ -162,7 +162,10 @@ namespace NzbDrone.Api.V1.Indexers
var retryAfterQueryLimit = _indexerLimitService.CalculateRetryAfterQueryLimit(indexerDef); var retryAfterQueryLimit = _indexerLimitService.CalculateRetryAfterQueryLimit(indexerDef);
AddRetryAfterHeader(retryAfterQueryLimit); AddRetryAfterHeader(retryAfterQueryLimit);
return CreateResponse(CreateErrorXML(429, $"User configurable Indexer Query Limit of {((IIndexerSettings)indexer.Definition.Settings).BaseSettings.QueryLimit} reached."), statusCode: StatusCodes.Status429TooManyRequests); var queryLimit = ((IIndexerSettings)indexer.Definition.Settings).BaseSettings.QueryLimit;
var intervalLimitHours = _indexerLimitService.CalculateIntervalLimitHours(indexerDef);
return CreateResponse(CreateErrorXML(429, $"User configurable Indexer Query Limit of {queryLimit} in last {intervalLimitHours} hour(s) reached."), statusCode: StatusCodes.Status429TooManyRequests);
} }
switch (requestType) switch (requestType)
@ -226,7 +229,10 @@ namespace NzbDrone.Api.V1.Indexers
var retryAfterDownloadLimit = _indexerLimitService.CalculateRetryAfterDownloadLimit(indexerDef); var retryAfterDownloadLimit = _indexerLimitService.CalculateRetryAfterDownloadLimit(indexerDef);
AddRetryAfterHeader(retryAfterDownloadLimit); AddRetryAfterHeader(retryAfterDownloadLimit);
return CreateResponse(CreateErrorXML(429, $"User configurable Indexer Grab Limit of {((IIndexerSettings)indexer.Definition.Settings).BaseSettings.GrabLimit} reached."), statusCode: StatusCodes.Status429TooManyRequests); var grabLimit = ((IIndexerSettings)indexer.Definition.Settings).BaseSettings.GrabLimit;
var intervalLimitHours = _indexerLimitService.CalculateIntervalLimitHours(indexerDef);
return CreateResponse(CreateErrorXML(429, $"User configurable Indexer Grab Limit of {grabLimit} in last {intervalLimitHours} hour(s) reached."), statusCode: StatusCodes.Status429TooManyRequests);
} }
if (link.IsNullOrWhiteSpace() || file.IsNullOrWhiteSpace()) if (link.IsNullOrWhiteSpace() || file.IsNullOrWhiteSpace())

Loading…
Cancel
Save