1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 04:22:30 +01:00

Revert "Async HttpClient and list lookup"

This reverts commit c8a2af867e.
This commit is contained in:
Qstick 2020-05-24 15:40:36 -04:00
parent 0479bb9cc3
commit 1d6a7a1843
10 changed files with 53 additions and 145 deletions

View File

@ -1,19 +1,18 @@
using System.IO; using System.IO;
using System.Threading.Tasks;
namespace NzbDrone.Common.Extensions namespace NzbDrone.Common.Extensions
{ {
public static class StreamExtensions public static class StreamExtensions
{ {
public static async Task<byte[]> ToBytes(this Stream input) public static byte[] ToBytes(this Stream input)
{ {
var buffer = new byte[16 * 1024]; var buffer = new byte[16 * 1024];
using (var ms = new MemoryStream()) using (var ms = new MemoryStream())
{ {
int read; int read;
while ((read = await input.ReadAsync(buffer, 0, buffer.Length)) > 0) while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
{ {
await ms.WriteAsync(buffer, 0, read); ms.Write(buffer, 0, read);
} }
return ms.ToArray(); return ms.ToArray();

View File

@ -1,10 +1,9 @@
using System.Net; using System.Net;
using System.Threading.Tasks;
namespace NzbDrone.Common.Http.Dispatchers namespace NzbDrone.Common.Http.Dispatchers
{ {
public interface IHttpDispatcher public interface IHttpDispatcher
{ {
Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieContainer cookies); HttpResponse GetResponse(HttpRequest request, CookieContainer cookies);
} }
} }

View File

@ -3,7 +3,6 @@
using System.IO.Compression; using System.IO.Compression;
using System.Net; using System.Net;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks;
using NLog; using NLog;
using NLog.Fluent; using NLog.Fluent;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
@ -29,7 +28,7 @@ public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider, I
_logger = logger; _logger = logger;
} }
public async Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieContainer cookies) public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
{ {
var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url); var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url);
@ -78,7 +77,7 @@ public async Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieCont
} }
} }
httpWebResponse = (HttpWebResponse)await webRequest.GetResponseAsync(); httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
} }
catch (WebException e) catch (WebException e)
{ {
@ -121,7 +120,7 @@ public async Task<HttpResponse> GetResponseAsync(HttpRequest request, CookieCont
{ {
try try
{ {
data = await responseStream.ToBytes(); data = responseStream.ToBytes();
if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip") if (PlatformInfo.IsMono && httpWebResponse.ContentEncoding == "gzip")
{ {

View File

@ -4,7 +4,6 @@
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Common.EnvironmentInfo; using NzbDrone.Common.EnvironmentInfo;
@ -25,16 +24,6 @@ HttpResponse<T> Get<T>(HttpRequest request)
HttpResponse Post(HttpRequest request); HttpResponse Post(HttpRequest request);
HttpResponse<T> Post<T>(HttpRequest request) HttpResponse<T> Post<T>(HttpRequest request)
where T : new(); where T : new();
Task<HttpResponse> ExecuteAsync(HttpRequest request);
Task DownloadFileAsync(string url, string fileName);
Task<HttpResponse> GetAsync(HttpRequest request);
Task<HttpResponse<T>> GetAsync<T>(HttpRequest request)
where T : new();
Task<HttpResponse> HeadAsync(HttpRequest request);
Task<HttpResponse> PostAsync(HttpRequest request);
Task<HttpResponse<T>> PostAsync<T>(HttpRequest request)
where T : new();
} }
public class HttpClient : IHttpClient public class HttpClient : IHttpClient
@ -65,11 +54,11 @@ public HttpClient(IEnumerable<IHttpRequestInterceptor> requestInterceptors,
_cookieContainerCache = cacheManager.GetCache<CookieContainer>(typeof(HttpClient)); _cookieContainerCache = cacheManager.GetCache<CookieContainer>(typeof(HttpClient));
} }
public async Task<HttpResponse> ExecuteAsync(HttpRequest request) public HttpResponse Execute(HttpRequest request)
{ {
var cookieContainer = InitializeRequestCookies(request); var cookieContainer = InitializeRequestCookies(request);
var response = await ExecuteRequestAsync(request, cookieContainer); var response = ExecuteRequest(request, cookieContainer);
if (request.AllowAutoRedirect && response.HasHttpRedirect) if (request.AllowAutoRedirect && response.HasHttpRedirect)
{ {
@ -88,7 +77,7 @@ public async Task<HttpResponse> ExecuteAsync(HttpRequest request)
throw new WebException($"Too many automatic redirections were attempted for {autoRedirectChain.Join(" -> ")}", WebExceptionStatus.ProtocolError); throw new WebException($"Too many automatic redirections were attempted for {autoRedirectChain.Join(" -> ")}", WebExceptionStatus.ProtocolError);
} }
response = await ExecuteRequestAsync(request, cookieContainer); response = ExecuteRequest(request, cookieContainer);
} }
while (response.HasHttpRedirect); while (response.HasHttpRedirect);
} }
@ -115,12 +104,7 @@ public async Task<HttpResponse> ExecuteAsync(HttpRequest request)
return response; return response;
} }
public HttpResponse Execute(HttpRequest request) private HttpResponse ExecuteRequest(HttpRequest request, CookieContainer cookieContainer)
{
return ExecuteAsync(request).GetAwaiter().GetResult();
}
private async Task<HttpResponse> ExecuteRequestAsync(HttpRequest request, CookieContainer cookieContainer)
{ {
foreach (var interceptor in _requestInterceptors) foreach (var interceptor in _requestInterceptors)
{ {
@ -129,7 +113,7 @@ private async Task<HttpResponse> ExecuteRequestAsync(HttpRequest request, Cookie
if (request.RateLimit != TimeSpan.Zero) if (request.RateLimit != TimeSpan.Zero)
{ {
await _rateLimitService.WaitAndPulseAsync(request.Url.Host, request.RateLimit); _rateLimitService.WaitAndPulse(request.Url.Host, request.RateLimit);
} }
_logger.Trace(request); _logger.Trace(request);
@ -138,7 +122,7 @@ private async Task<HttpResponse> ExecuteRequestAsync(HttpRequest request, Cookie
PrepareRequestCookies(request, cookieContainer); PrepareRequestCookies(request, cookieContainer);
var response = await _httpDispatcher.GetResponseAsync(request, cookieContainer); var response = _httpDispatcher.GetResponse(request, cookieContainer);
HandleResponseCookies(response, cookieContainer); HandleResponseCookies(response, cookieContainer);
@ -247,7 +231,7 @@ private void HandleResponseCookies(HttpResponse response, CookieContainer cookie
} }
} }
public async Task DownloadFileAsync(string url, string fileName) public void DownloadFile(string url, string fileName)
{ {
try try
{ {
@ -263,7 +247,7 @@ public async Task DownloadFileAsync(string url, string fileName)
using (var webClient = new GZipWebClient()) using (var webClient = new GZipWebClient())
{ {
webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent()); webClient.Headers.Add(HttpRequestHeader.UserAgent, _userAgentBuilder.GetUserAgent());
await webClient.DownloadFileTaskAsync(url, fileName); webClient.DownloadFile(url, fileName);
stopWatch.Stop(); stopWatch.Stop();
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
} }
@ -271,92 +255,47 @@ public async Task DownloadFileAsync(string url, string fileName)
catch (WebException e) catch (WebException e)
{ {
_logger.Warn("Failed to get response from: {0} {1}", url, e.Message); _logger.Warn("Failed to get response from: {0} {1}", url, e.Message);
if (File.Exists(fileName))
{
File.Delete(fileName);
}
throw; throw;
} }
catch (Exception e) catch (Exception e)
{ {
_logger.Warn(e, "Failed to get response from: " + url); _logger.Warn(e, "Failed to get response from: " + url);
if (File.Exists(fileName))
{
File.Delete(fileName);
}
throw; throw;
} }
} }
public void DownloadFile(string url, string fileName)
{
// https://docs.microsoft.com/en-us/archive/msdn-magazine/2015/july/async-programming-brownfield-async-development#the-thread-pool-hack
Task.Run(() => DownloadFileAsync(url, fileName)).GetAwaiter().GetResult();
}
public Task<HttpResponse> GetAsync(HttpRequest request)
{
request.Method = HttpMethod.GET;
return ExecuteAsync(request);
}
public HttpResponse Get(HttpRequest request) public HttpResponse Get(HttpRequest request)
{ {
return Task.Run(() => GetAsync(request)).GetAwaiter().GetResult(); request.Method = HttpMethod.GET;
} return Execute(request);
public async Task<HttpResponse<T>> GetAsync<T>(HttpRequest request)
where T : new()
{
var response = await GetAsync(request);
CheckResponseContentType(response);
return new HttpResponse<T>(response);
} }
public HttpResponse<T> Get<T>(HttpRequest request) public HttpResponse<T> Get<T>(HttpRequest request)
where T : new() where T : new()
{ {
return Task.Run(() => GetAsync<T>(request)).GetAwaiter().GetResult(); var response = Get(request);
} CheckResponseContentType(response);
return new HttpResponse<T>(response);
public Task<HttpResponse> HeadAsync(HttpRequest request)
{
request.Method = HttpMethod.HEAD;
return ExecuteAsync(request);
} }
public HttpResponse Head(HttpRequest request) public HttpResponse Head(HttpRequest request)
{ {
return Task.Run(() => HeadAsync(request)).GetAwaiter().GetResult(); request.Method = HttpMethod.HEAD;
} return Execute(request);
public Task<HttpResponse> PostAsync(HttpRequest request)
{
request.Method = HttpMethod.POST;
return ExecuteAsync(request);
} }
public HttpResponse Post(HttpRequest request) public HttpResponse Post(HttpRequest request)
{ {
return Task.Run(() => PostAsync(request)).GetAwaiter().GetResult(); request.Method = HttpMethod.POST;
} return Execute(request);
public async Task<HttpResponse<T>> PostAsync<T>(HttpRequest request)
where T : new()
{
var response = await PostAsync(request);
CheckResponseContentType(response);
return new HttpResponse<T>(response);
} }
public HttpResponse<T> Post<T>(HttpRequest request) public HttpResponse<T> Post<T>(HttpRequest request)
where T : new() where T : new()
{ {
return Task.Run(() => PostAsync<T>(request)).GetAwaiter().GetResult(); var response = Post(request);
CheckResponseContentType(response);
return new HttpResponse<T>(response);
} }
private void CheckResponseContentType(HttpResponse response) private void CheckResponseContentType(HttpResponse response)

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Threading.Tasks;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
@ -9,7 +8,6 @@ namespace NzbDrone.Common.TPL
public interface IRateLimitService public interface IRateLimitService
{ {
void WaitAndPulse(string key, TimeSpan interval); void WaitAndPulse(string key, TimeSpan interval);
Task WaitAndPulseAsync(string key, TimeSpan interval);
} }
public class RateLimitService : IRateLimitService public class RateLimitService : IRateLimitService
@ -25,7 +23,13 @@ public RateLimitService(ICacheManager cacheManager, Logger logger)
public void WaitAndPulse(string key, TimeSpan interval) public void WaitAndPulse(string key, TimeSpan interval)
{ {
var delay = GetDelay(key, interval); var waitUntil = _rateLimitStore.AddOrUpdate(key,
(s) => DateTime.UtcNow + interval,
(s, i) => new DateTime(Math.Max(DateTime.UtcNow.Ticks, i.Ticks), DateTimeKind.Utc) + interval);
waitUntil -= interval;
var delay = waitUntil - DateTime.UtcNow;
if (delay.TotalSeconds > 0.0) if (delay.TotalSeconds > 0.0)
{ {
@ -33,27 +37,5 @@ public void WaitAndPulse(string key, TimeSpan interval)
System.Threading.Thread.Sleep(delay); System.Threading.Thread.Sleep(delay);
} }
} }
public async Task WaitAndPulseAsync(string key, TimeSpan interval)
{
var delay = GetDelay(key, interval);
if (delay.TotalSeconds > 0.0)
{
_logger.Trace("Rate Limit triggered, delaying '{0}' for {1:0.000} sec", key, delay.TotalSeconds);
await Task.Delay(delay);
}
}
private TimeSpan GetDelay(string key, TimeSpan interval)
{
var waitUntil = _rateLimitStore.AddOrUpdate(key,
(s) => DateTime.UtcNow + interval,
(s, i) => new DateTime(Math.Max(DateTime.UtcNow.Ticks, i.Ticks), DateTimeKind.Utc) + interval);
waitUntil -= interval;
return waitUntil - DateTime.UtcNow;
}
} }
} }

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using NzbDrone.Core.Movies; using NzbDrone.Core.Movies;
using NzbDrone.Core.Movies.Credits; using NzbDrone.Core.Movies.Credits;
@ -11,7 +10,5 @@ public interface IProvideMovieInfo
Movie GetMovieByImdbId(string imdbId); Movie GetMovieByImdbId(string imdbId);
Tuple<Movie, List<Credit>> GetMovieInfo(int tmdbId); Tuple<Movie, List<Credit>> GetMovieInfo(int tmdbId);
HashSet<int> GetChangedMovies(DateTime startTime); HashSet<int> GetChangedMovies(DateTime startTime);
Task<Tuple<Movie, List<Credit>>> GetMovieInfoAsync(int tmdbId);
} }
} }

View File

@ -1,5 +1,4 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Threading.Tasks;
using NzbDrone.Core.Movies; using NzbDrone.Core.Movies;
namespace NzbDrone.Core.MetadataSource namespace NzbDrone.Core.MetadataSource
@ -9,6 +8,5 @@ public interface ISearchForNewMovie
List<Movie> SearchForNewMovie(string title); List<Movie> SearchForNewMovie(string title);
Movie MapMovieToTmdbMovie(Movie movie); Movie MapMovieToTmdbMovie(Movie movie);
Task<Movie> MapMovieToTmdbMovieAsync(Movie movie);
} }
} }

View File

@ -2,7 +2,6 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net; using System.Net;
using System.Threading.Tasks;
using NLog; using NLog;
using NzbDrone.Common.Cloud; using NzbDrone.Common.Cloud;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
@ -15,6 +14,7 @@
using NzbDrone.Core.Movies; using NzbDrone.Core.Movies;
using NzbDrone.Core.Movies.AlternativeTitles; using NzbDrone.Core.Movies.AlternativeTitles;
using NzbDrone.Core.Movies.Credits; using NzbDrone.Core.Movies.Credits;
using NzbDrone.Core.NetImport.ImportExclusions;
using NzbDrone.Core.NetImport.TMDb; using NzbDrone.Core.NetImport.TMDb;
using NzbDrone.Core.Parser; using NzbDrone.Core.Parser;
@ -65,7 +65,7 @@ public HashSet<int> GetChangedMovies(DateTime startTime)
return new HashSet<int>(response.Resource.Results.Select(c => c.Id)); return new HashSet<int>(response.Resource.Results.Select(c => c.Id));
} }
public async Task<Tuple<Movie, List<Credit>>> GetMovieInfoAsync(int tmdbId) public Tuple<Movie, List<Credit>> GetMovieInfo(int tmdbId)
{ {
var httpRequest = _radarrMetadata.Create() var httpRequest = _radarrMetadata.Create()
.SetSegment("route", "movie") .SetSegment("route", "movie")
@ -75,7 +75,7 @@ public async Task<Tuple<Movie, List<Credit>>> GetMovieInfoAsync(int tmdbId)
httpRequest.AllowAutoRedirect = true; httpRequest.AllowAutoRedirect = true;
httpRequest.SuppressHttpError = true; httpRequest.SuppressHttpError = true;
var httpResponse = await _httpClient.GetAsync<MovieResource>(httpRequest); var httpResponse = _httpClient.Get<MovieResource>(httpRequest);
if (httpResponse.HasHttpError) if (httpResponse.HasHttpError)
{ {
@ -98,11 +98,6 @@ public async Task<Tuple<Movie, List<Credit>>> GetMovieInfoAsync(int tmdbId)
return new Tuple<Movie, List<Credit>>(movie, credits.ToList()); return new Tuple<Movie, List<Credit>>(movie, credits.ToList());
} }
public Tuple<Movie, List<Credit>> GetMovieInfo(int tmdbId)
{
return GetMovieInfoAsync(tmdbId).GetAwaiter().GetResult();
}
public Movie GetMovieByImdbId(string imdbId) public Movie GetMovieByImdbId(string imdbId)
{ {
var httpRequest = _radarrMetadata.Create() var httpRequest = _radarrMetadata.Create()
@ -241,14 +236,14 @@ private string StripTrailingTheFromTitle(string title)
return title; return title;
} }
public async Task<Movie> MapMovieToTmdbMovieAsync(Movie movie) public Movie MapMovieToTmdbMovie(Movie movie)
{ {
try try
{ {
Movie newMovie = movie; Movie newMovie = movie;
if (movie.TmdbId > 0) if (movie.TmdbId > 0)
{ {
newMovie = (await GetMovieInfoAsync(movie.TmdbId)).Item1; newMovie = GetMovieInfo(movie.TmdbId).Item1;
} }
else if (movie.ImdbId.IsNotNullOrWhiteSpace()) else if (movie.ImdbId.IsNotNullOrWhiteSpace())
{ {
@ -288,11 +283,6 @@ public async Task<Movie> MapMovieToTmdbMovieAsync(Movie movie)
} }
} }
public Movie MapMovieToTmdbMovie(Movie movie)
{
return MapMovieToTmdbMovieAsync(movie).GetAwaiter().GetResult();
}
public List<Movie> SearchForNewMovie(string title) public List<Movie> SearchForNewMovie(string title)
{ {
try try

View File

@ -1,5 +1,4 @@
using System; using NLog;
using NLog;
using NzbDrone.Common.Cloud; using NzbDrone.Common.Cloud;
using NzbDrone.Common.Http; using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration; using NzbDrone.Core.Configuration;
@ -12,7 +11,6 @@ public abstract class TMDbNetImportBase<TSettings> : HttpNetImportBase<TSettings
where TSettings : TMDbSettingsBase<TSettings>, new() where TSettings : TMDbSettingsBase<TSettings>, new()
{ {
public override NetImportType ListType => NetImportType.TMDB; public override NetImportType ListType => NetImportType.TMDB;
public override TimeSpan RateLimit => TimeSpan.Zero;
public readonly ISearchForNewMovie _skyhookProxy; public readonly ISearchForNewMovie _skyhookProxy;
public readonly IHttpRequestBuilderFactory _requestBuilder; public readonly IHttpRequestBuilderFactory _requestBuilder;

View File

@ -1,6 +1,5 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks;
using Nancy; using Nancy;
using NzbDrone.Common.Extensions; using NzbDrone.Common.Extensions;
using NzbDrone.Core.MediaCover; using NzbDrone.Core.MediaCover;
@ -28,14 +27,22 @@ private object Search()
{ {
var results = _fetchNetImport.FetchAndFilter((int)Request.Query.listId, false); var results = _fetchNetImport.FetchAndFilter((int)Request.Query.listId, false);
var tasks = results.Where(movie => movie.TmdbId == 0 || !movie.Images.Any() || movie.Overview.IsNullOrWhiteSpace()) List<Movie> realResults = new List<Movie>();
.Select(x => _movieSearch.MapMovieToTmdbMovieAsync(x));
var realResults = results.Where(movie => movie.TmdbId != 0 && movie.Images.Any() && movie.Overview.IsNotNullOrWhiteSpace()).ToList(); foreach (var movie in results)
{
var mapped = movie;
var mapped = Task.WhenAll(tasks).GetAwaiter().GetResult(); if (movie.TmdbId == 0 || !movie.Images.Any() || movie.Overview.IsNullOrWhiteSpace())
{
mapped = _movieSearch.MapMovieToTmdbMovie(movie);
}
realResults.AddRange(mapped.Where(x => x != null)); if (mapped != null)
{
realResults.Add(mapped);
}
}
return MapToResource(realResults); return MapToResource(realResults);
} }