From 2743b79f0271f3f47feec480256dc015d59990bf Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Mon, 28 Dec 2015 12:27:37 -0500 Subject: [PATCH] throttle tmdb requests --- .../Movies/MovieDbProvider.cs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/MediaBrowser.Providers/Movies/MovieDbProvider.cs b/MediaBrowser.Providers/Movies/MovieDbProvider.cs index fe3e1f358..4aa24bf5d 100644 --- a/MediaBrowser.Providers/Movies/MovieDbProvider.cs +++ b/MediaBrowser.Providers/Movies/MovieDbProvider.cs @@ -366,14 +366,27 @@ namespace MediaBrowser.Providers.Movies return mainResult; } + private static long _lastRequestTicks; + private static int requestIntervalMs = 100; + /// /// Gets the movie db response. /// - internal Task GetMovieDbResponse(HttpRequestOptions options) + internal async Task GetMovieDbResponse(HttpRequestOptions options) { - options.ResourcePool = MovieDbResourcePool; + var delayTicks = (requestIntervalMs * 10000) - (DateTime.UtcNow.Ticks - _lastRequestTicks); + var delayMs = Math.Min(delayTicks / 10000, requestIntervalMs); - return _httpClient.Get(options); + if (delayMs > 0) + { + _logger.Debug("Throttling Tmdb by {0} ms", delayMs); + await Task.Delay(Convert.ToInt32(delayMs)).ConfigureAwait(false); + } + + options.ResourcePool = MovieDbResourcePool; + _lastRequestTicks = DateTime.UtcNow.Ticks; + + return await _httpClient.Get(options).ConfigureAwait(false); } public TheMovieDbOptions GetTheMovieDbOptions()