private void ConsulResultChanged(EndPointsResult newResult) { lock (_resultLocker) { var shouldReportChanges = false; if (newResult.IsQueryDefined != Result.IsQueryDefined) { shouldReportChanges = true; if (newResult.IsQueryDefined == false) { _log.Warn(x => x("Service has become undefined on Consul", unencryptedTags: new { serviceName = Deployment })); } else { _log.Info(x => x("Service has become defined on Consul", unencryptedTags: new { serviceName = Deployment })); } } else if (!OrderedEndpoints(newResult.EndPoints).SequenceEqual(OrderedEndpoints(Result.EndPoints))) { shouldReportChanges = true; _log.Info(_ => _("Obtained a new list of endpoints for service from Consul", unencryptedTags: new { serviceName = Deployment, endpoints = string.Join(", ", newResult.EndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? ""))) })); } if (_firstTime || newResult.Error == null || Result.Error != null) { Result = newResult; } if (shouldReportChanges && !_firstTime) { EndpointsChangedBroadcast.Post(Result); } _lastResult = newResult; _firstTime = false; _initialized.TrySetResult(true); } }
public void SetEndPoints(string endPoints) { Result = new EndPointsResult { EndPoints = new EndPoint[0] }; if (!string.IsNullOrWhiteSpace(endPoints)) { Result = new EndPointsResult { EndPoints = endPoints.Split(',').Select(_ => _.Trim()) .Where(a => !string.IsNullOrWhiteSpace(a)) .Select(_ => new EndPoint { HostName = _ }) .ToArray() } } ; EndpointsChangedBroadcast.Post(Result); Task.Delay(100).Wait(); }