Permalink
Browse files

Use a Parallel.ForEach for connections to plex servers

  • Loading branch information...
Cazzar committed Jun 20, 2018
1 parent 97bde94 commit 7c883e2688322c414885ab10b5aa976572cb13a0
Showing with 25 additions and 14 deletions.
  1. +25 −14 Shoko.Server/Plex/PlexHelper.cs
@@ -95,20 +95,31 @@ private Connection ConnectionCache
if (DateTime.Now - TimeSpan.FromHours(12) < _lastCacheTime && _cachedConnection != null)
return _cachedConnection;

foreach (var connection in ServerCache.Connection)
try
{
var (result, _) = RequestAsync($"{connection.Uri}/library/sections", HttpMethod.Get,
new Dictionary<string, string> {{"X-Plex-Token", ServerCache.AccessToken}})
.Result;

if (result != HttpStatusCode.OK) continue;
_cachedConnection = connection;
break;
}
catch (AggregateException)
{
}
//foreach (var connection in ServerCache.Connection)
Parallel.ForEach(ServerCache.Connection, (connection, state) =>
{
try
{
if (state.ShouldExitCurrentIteration) return;
var (result, _) = RequestAsync($"{connection.Uri}/library/sections", HttpMethod.Get,
new Dictionary<string, string> {{"X-Plex-Token", ServerCache.AccessToken}})
.Result;

if (result != HttpStatusCode.OK)
{
Logger.Trace($"Got response from: {connection.Uri} {result}");
return;
}

_cachedConnection = connection;
state.Stop();
}
catch (AggregateException)
{
Logger.Trace($"Failed connection to: {connection.Uri}");
}
}
);

_lastCacheTime = DateTime.Now;

0 comments on commit 7c883e2

Please sign in to comment.