浏览代码

throttle tmdb requests

Luke Pulverenti 9 年之前
父节点
当前提交
2743b79f02
共有 1 个文件被更改,包括 15 次插入2 次删除
  1. 15 2
      MediaBrowser.Providers/Movies/MovieDbProvider.cs

+ 15 - 2
MediaBrowser.Providers/Movies/MovieDbProvider.cs

@@ -366,14 +366,27 @@ namespace MediaBrowser.Providers.Movies
             return mainResult;
         }
 
+        private static long _lastRequestTicks;
+        private static int requestIntervalMs = 100;
+
         /// <summary>
         /// Gets the movie db response.
         /// </summary>
-        internal Task<Stream> GetMovieDbResponse(HttpRequestOptions options)
+        internal async Task<Stream> GetMovieDbResponse(HttpRequestOptions options)
         {
+            var delayTicks = (requestIntervalMs * 10000) - (DateTime.UtcNow.Ticks - _lastRequestTicks);
+            var delayMs = Math.Min(delayTicks / 10000, requestIntervalMs);
+
+            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 _httpClient.Get(options);
+            return await _httpClient.Get(options).ConfigureAwait(false);
         }
 
         public TheMovieDbOptions GetTheMovieDbOptions()