public async Task <AggregateHealthRecord> InternalIsHealthyAsync() { await _semaphoreSlim.WaitAsync(); try { if (HealthRecords.IsEmpty) { return(null); } var unHealtyRecords = new List <string>(); foreach (var item in HealthRecords) { if (item.Value.Healty == false) { unHealtyRecords.Add(item.Key); } } if (unHealtyRecords.Any()) { return(AggregateHealthRecord.Unhealthy(unHealtyRecords.ToArray())); } return(AggregateHealthRecord.Healthy()); } finally { //When the task is ready, release the semaphore. It is vital to ALWAYS release the semaphore when we are ready, or else we will end up with a Semaphore that is forever locked. //This is why it is important to do the Release within a try...finally clause; program execution may crash or take a different path, this way you are guaranteed execution _semaphoreSlim.Release(); } }
public async Task SetHealthAsync(string key, HealthRecord healthRecord) { HealthRecords[key] = healthRecord; _aggregateRecord = await InternalIsHealthyAsync(); }
public HealthCheckStore() { HealthRecords = new ConcurrentDictionary <string, HealthRecord>(); _aggregateRecord = AggregateHealthRecord.Unhealthy(new [] { "" }); }