AddressMonitoring: updated implementation to use healthCheckUrl based health monitor.

This commit is contained in:
Shreyas Zare
2021-08-07 16:38:46 +05:30
parent 9d169e4760
commit fee691e782

View File

@@ -37,13 +37,13 @@ namespace Failover
#region constructor
public AddressMonitoring(HealthMonitoringService service, IPAddress address, string healthCheck)
public AddressMonitoring(HealthMonitoringService service, IPAddress address, string healthCheck, Uri healthCheckUrl)
{
_service = service;
_address = address;
if (_service.HealthChecks.TryGetValue(healthCheck, out HealthCheck existingHealthCheck))
_healthMonitors.TryAdd(healthCheck, new HealthMonitor(_service.DnsServer, _address, existingHealthCheck));
_healthMonitors.TryAdd(GetHealthMonitorKey(healthCheck, healthCheckUrl), new HealthMonitor(_service.DnsServer, _address, existingHealthCheck, healthCheckUrl));
}
#endregion
@@ -76,23 +76,45 @@ namespace Failover
#endregion
#region private
private static string GetHealthMonitorKey(string healthCheck, Uri healthCheckUrl)
{
string healthMonitorKey = healthCheck;
if (healthCheckUrl is not null)
healthMonitorKey += "|" + healthCheckUrl.AbsoluteUri;
return healthMonitorKey;
}
#endregion
#region public
public HealthCheckStatus QueryStatus(string healthCheck)
public HealthCheckStatus QueryStatus(string healthCheck, Uri healthCheckUrl)
{
if (_healthMonitors.TryGetValue(healthCheck, out HealthMonitor monitor))
string healthMonitorKey = GetHealthMonitorKey(healthCheck, healthCheckUrl);
if (_healthMonitors.TryGetValue(healthMonitorKey, out HealthMonitor monitor))
return monitor.HealthCheckStatus;
if (_service.HealthChecks.TryGetValue(healthCheck, out HealthCheck existingHealthCheck))
_healthMonitors.TryAdd(healthCheck, new HealthMonitor(_service.DnsServer, _address, existingHealthCheck));
_healthMonitors.TryAdd(healthMonitorKey, new HealthMonitor(_service.DnsServer, _address, existingHealthCheck, healthCheckUrl));
return null;
}
public void RemoveHealthMonitor(string healthCheck)
{
if (_healthMonitors.TryRemove(healthCheck, out HealthMonitor removedMonitor))
removedMonitor.Dispose();
foreach (KeyValuePair<string, HealthMonitor> healthMonitor in _healthMonitors)
{
if (healthMonitor.Key.StartsWith(healthCheck + "|"))
{
if (_healthMonitors.TryRemove(healthMonitor.Key, out HealthMonitor removedMonitor))
removedMonitor.Dispose();
}
}
}
public bool IsExpired()