From fee691e782dfd97bb411a7763737ab1e70965ade Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sat, 7 Aug 2021 16:38:46 +0530 Subject: [PATCH] AddressMonitoring: updated implementation to use healthCheckUrl based health monitor. --- Apps/FailoverApp/AddressMonitoring.cs | 36 +++++++++++++++++++++------ 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/Apps/FailoverApp/AddressMonitoring.cs b/Apps/FailoverApp/AddressMonitoring.cs index 5da45627..e24d7ecc 100644 --- a/Apps/FailoverApp/AddressMonitoring.cs +++ b/Apps/FailoverApp/AddressMonitoring.cs @@ -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 healthMonitor in _healthMonitors) + { + if (healthMonitor.Key.StartsWith(healthCheck + "|")) + { + if (_healthMonitors.TryRemove(healthMonitor.Key, out HealthMonitor removedMonitor)) + removedMonitor.Dispose(); + } + } } public bool IsExpired()