public async Task <ChordHealthStatus> CheckHealth( IChordEndpoint target, int timeoutInSecs = 10, ChordHealthStatus failStatus = ChordHealthStatus.Questionable) { // send a health check request var cancelCallback = new CancellationTokenSource(); var timeoutTask = Task.Delay(timeoutInSecs * 1000); var healthCheckTask = Task.Run(() => sendRequest( new ChordRequestMessage() { Type = ChordRequestType.HealthCheck, RequesterId = local.NodeId, }, target ), cancelCallback.Token); // return the reported health state or the fail status (timeout) bool timeout = await Task.WhenAny(timeoutTask, healthCheckTask) == timeoutTask; if (timeout) { cancelCallback.Cancel(); } return(timeout ? failStatus : healthCheckTask.Result.Responder.State); }
private void updateHealth(List <IChordEndpoint> fingers, int timeoutSecs, ChordHealthStatus failStatus = ChordHealthStatus.Questionable) { // run health checks for each finger in parallel var healthCheckTasks = fingerTable.Values .Select(x => Task.Run(() => new { NodeId = x.NodeId, Health = CheckHealth(x, timeoutSecs, failStatus).Result })).ToArray(); Task.WaitAll(healthCheckTasks); // collect health check results var healthStates = healthCheckTasks.Select(x => x.Result) .ToDictionary(x => x.NodeId, x => x.Health); // update finger states fingers.ForEach(finger => finger.State = healthStates[finger.NodeId]); }