reduce server pinging

pull/702/head
Luke Pulverenti 9 years ago
parent 8c7296b560
commit 8a50392ba7

@ -210,7 +210,6 @@ namespace MediaBrowser.Server.Startup.Common
private readonly string _releaseAssetFilename; private readonly string _releaseAssetFilename;
internal INativeApp NativeApp { get; set; } internal INativeApp NativeApp { get; set; }
private Timer _ipAddressCacheTimer;
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="ApplicationHost" /> class. /// Initializes a new instance of the <see cref="ApplicationHost" /> class.
@ -234,8 +233,6 @@ namespace MediaBrowser.Server.Startup.Common
NativeApp = nativeApp; NativeApp = nativeApp;
SetBaseExceptionMessage(); SetBaseExceptionMessage();
_ipAddressCacheTimer = new Timer(OnCacheClearTimerFired, null, TimeSpan.FromMinutes(3), TimeSpan.FromMinutes(3));
} }
private Version _version; private Version _version;
@ -534,7 +531,7 @@ namespace MediaBrowser.Server.Startup.Common
RegisterSingleInstance(EncodingManager); RegisterSingleInstance(EncodingManager);
RegisterSingleInstance(NativeApp.GetPowerManagement()); RegisterSingleInstance(NativeApp.GetPowerManagement());
var sharingRepo = new SharingRepository(LogManager, ApplicationPaths); var sharingRepo = new SharingRepository(LogManager, ApplicationPaths);
await sharingRepo.Initialize().ConfigureAwait(false); await sharingRepo.Initialize().ConfigureAwait(false);
RegisterSingleInstance<ISharingManager>(new SharingManager(sharingRepo, ServerConfigurationManager, LibraryManager, this)); RegisterSingleInstance<ISharingManager>(new SharingManager(sharingRepo, ServerConfigurationManager, LibraryManager, this));
@ -972,10 +969,10 @@ namespace MediaBrowser.Server.Startup.Common
{ {
get get
{ {
if (!ServerConfigurationManager.Configuration.EnableAutoUpdate) if (!ServerConfigurationManager.Configuration.EnableAutoUpdate)
{ {
return false; return false;
} }
#if DEBUG #if DEBUG
return false; return false;
#endif #endif
@ -1159,7 +1156,12 @@ namespace MediaBrowser.Server.Startup.Common
} }
private readonly ConcurrentDictionary<string, bool> _validAddressResults = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase); private readonly ConcurrentDictionary<string, bool> _validAddressResults = new ConcurrentDictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
private DateTime _lastAddressCacheClear;
private bool IsIpAddressValid(IPAddress address) private bool IsIpAddressValid(IPAddress address)
{
return IsIpAddressValidInternal(address).Result;
}
private async Task<bool> IsIpAddressValidInternal(IPAddress address)
{ {
if (IPAddress.IsLoopback(address)) if (IPAddress.IsLoopback(address))
{ {
@ -1169,6 +1171,12 @@ namespace MediaBrowser.Server.Startup.Common
var apiUrl = GetLocalApiUrl(address.ToString()); var apiUrl = GetLocalApiUrl(address.ToString());
apiUrl += "/system/ping"; apiUrl += "/system/ping";
if ((DateTime.UtcNow - _lastAddressCacheClear).TotalMinutes >= 5)
{
_lastAddressCacheClear = DateTime.UtcNow;
_validAddressResults.Clear();
}
bool cachedResult; bool cachedResult;
if (_validAddressResults.TryGetValue(apiUrl, out cachedResult)) if (_validAddressResults.TryGetValue(apiUrl, out cachedResult))
{ {
@ -1177,14 +1185,15 @@ namespace MediaBrowser.Server.Startup.Common
try try
{ {
using (var response = HttpClient.SendAsync(new HttpRequestOptions using (var response = await HttpClient.SendAsync(new HttpRequestOptions
{ {
Url = apiUrl, Url = apiUrl,
LogErrorResponseBody = false, LogErrorResponseBody = false,
LogErrors = false, LogErrors = false,
LogRequest = false LogRequest = false,
TimeoutMs = 30000
}, "POST").Result) }, "POST").ConfigureAwait(false))
{ {
using (var reader = new StreamReader(response.Content)) using (var reader = new StreamReader(response.Content))
{ {
@ -1192,25 +1201,20 @@ namespace MediaBrowser.Server.Startup.Common
var valid = string.Equals(Name, result, StringComparison.OrdinalIgnoreCase); var valid = string.Equals(Name, result, StringComparison.OrdinalIgnoreCase);
_validAddressResults.AddOrUpdate(apiUrl, valid, (k, v) => valid); _validAddressResults.AddOrUpdate(apiUrl, valid, (k, v) => valid);
Logger.Debug("Ping test result to {0}. Success: {1}", apiUrl, valid); //Logger.Debug("Ping test result to {0}. Success: {1}", apiUrl, valid);
return valid; return valid;
} }
} }
} }
catch catch
{ {
Logger.Debug("Ping test result to {0}. Success: {1}", apiUrl, false); //Logger.Debug("Ping test result to {0}. Success: {1}", apiUrl, false);
_validAddressResults.AddOrUpdate(apiUrl, false, (k, v) => false); _validAddressResults.AddOrUpdate(apiUrl, false, (k, v) => false);
return false; return false;
} }
} }
private void OnCacheClearTimerFired(object state)
{
_validAddressResults.Clear();
}
public string FriendlyName public string FriendlyName
{ {
get get

Loading…
Cancel
Save