throttle people requests

This commit is contained in:
Luke Pulverenti 2016-01-22 21:32:14 -05:00
parent 8468443410
commit 0181383d1e
5 changed files with 45 additions and 3 deletions

View File

@ -76,7 +76,8 @@ namespace MediaBrowser.Api
ImageRefreshMode = request.ImageRefreshMode,
ReplaceAllImages = request.ReplaceAllImages,
ReplaceAllMetadata = request.ReplaceAllMetadata,
ForceSave = true
ForceSave = true,
IsAutomated = false
};
}
}

View File

@ -11,6 +11,7 @@ namespace MediaBrowser.Controller.Providers
public bool ReplaceAllImages { get; set; }
public List<ImageType> ReplaceImages { get; set; }
public bool IsAutomated { get; set; }
public ImageRefreshOptions(IDirectoryService directoryService)
{
@ -18,6 +19,7 @@ namespace MediaBrowser.Controller.Providers
DirectoryService = directoryService;
ReplaceImages = new List<ImageType>();
IsAutomated = true;
}
public bool IsReplacingImage(ImageType type)

View File

@ -33,10 +33,12 @@ namespace MediaBrowser.Controller.Providers
public int? Year { get; set; }
public int? IndexNumber { get; set; }
public int? ParentIndexNumber { get; set; }
public DateTime? PremiereDate { get; set; }
public DateTime? PremiereDate { get; set; }
public bool IsAutomated { get; set; }
public ItemLookupInfo()
{
IsAutomated = true;
ProviderIds = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
}

View File

@ -138,6 +138,7 @@ namespace MediaBrowser.Providers.Manager
var id = itemOfType.GetLookupInfo();
//await FindIdentities(id, cancellationToken).ConfigureAwait(false);
id.IsAutomated = refreshOptions.IsAutomated;
var result = await RefreshWithProviders(metadataResult, id, refreshOptions, providers, itemImageProvider, cancellationToken).ConfigureAwait(false);

View File

@ -10,6 +10,7 @@ using MediaBrowser.Model.Providers;
using MediaBrowser.Model.Serialization;
using MediaBrowser.Providers.Movies;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
@ -18,6 +19,7 @@ using System.Net;
using System.Threading;
using System.Threading.Tasks;
using CommonIO;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.Net;
namespace MediaBrowser.Providers.People
@ -32,14 +34,30 @@ namespace MediaBrowser.Providers.People
private readonly IFileSystem _fileSystem;
private readonly IServerConfigurationManager _configurationManager;
private readonly IHttpClient _httpClient;
private readonly ILogger _logger;
public MovieDbPersonProvider(IFileSystem fileSystem, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient)
private int _requestCount;
private readonly object _requestCountLock = new object();
private Timer _requestCountReset;
public MovieDbPersonProvider(IFileSystem fileSystem, IServerConfigurationManager configurationManager, IJsonSerializer jsonSerializer, IHttpClient httpClient, ILogger logger)
{
_fileSystem = fileSystem;
_configurationManager = configurationManager;
_jsonSerializer = jsonSerializer;
_httpClient = httpClient;
_logger = logger;
Current = this;
_requestCountReset = new Timer(OnRequestThrottleTimerFired, null, TimeSpan.FromHours(1), TimeSpan.FromHours(1));
}
private void OnRequestThrottleTimerFired(object state)
{
lock (_requestCountLock)
{
_requestCount = 0;
}
}
public string Name
@ -79,6 +97,24 @@ namespace MediaBrowser.Providers.People
return new[] { result };
}
if (searchInfo.IsAutomated)
{
lock (_requestCountLock)
{
var requestCount = _requestCount;
if (requestCount >= 5)
{
_logger.Debug("Throttling Tmdb people");
// This needs to be throttled
return new List<RemoteSearchResult>();
}
_requestCount = requestCount + 1;
}
}
var url = string.Format(@"http://api.themoviedb.org/3/search/person?api_key={1}&query={0}", WebUtility.UrlEncode(searchInfo.Name), MovieDbProvider.ApiKey);
using (var json = await MovieDbProvider.Current.GetMovieDbResponse(new HttpRequestOptions