private async Task Load()
        {
            var lastConsulResult = await ConsulClient.GetEndPoints(DeploymentName).ConfigureAwait(false);

            lock (_lastResultLocker)
            {
                if (lastConsulResult.Error != null)
                {
                    _lastConsulResult = lastConsulResult;
                    return;
                }
                var newEndPoints = lastConsulResult
                                   .EndPoints
                                   .OrderBy(x => x.HostName)
                                   .ThenBy(x => x.Port)
                                   .ToArray();

                if (newEndPoints.SequenceEqual(EndPoints) == false)
                {
                    EndPoints = newEndPoints;

                    _log.Info(_ => _("Obtained new list endpoints for service from Consul", unencryptedTags: new
                    {
                        serviceName = DeploymentName,
                        endpoints   = string.Join(", ", EndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? "")))
                    }));

                    if (!_firstTime || _lastConsulResult?.Error != null)
                    {
                        EndPointsChanged?.Post(lastConsulResult);
                    }
                }

                _firstTime        = false;
                _lastConsulResult = lastConsulResult;
                _initialized      = Task.FromResult(1);
            }
        }
Esempio n. 2
0
        private async Task Load()
        {
            var newConsulResult = await ConsulClient.GetEndPoints(DeploymentName).ConfigureAwait(false);

            lock (_lastResultLocker)
            {
                var oldConsulResult = _lastConsulResult;
                _lastConsulResult = newConsulResult;

                if (IsDeploymentDefined(oldConsulResult) && !IsDeploymentDefined(newConsulResult))
                {
                    _log.Warn(x => x("Service has become undefined on Consul", unencryptedTags: new
                    {
                        serviceName = DeploymentName
                    }));
                }

                if (newConsulResult.Error != null)
                {
                    if (_firstTime)
                    {
                        _firstTime = false;
                        Result     = newConsulResult;
                    }
                    return;
                }

                var newEndPoints = newConsulResult
                                   .EndPoints
                                   .OrderBy(x => x.HostName)
                                   .ThenBy(x => x.Port)
                                   .ToArray();
                bool isEndPointChanged = false;
                if (newEndPoints.SequenceEqual(Result.EndPoints) == false)
                {
                    newConsulResult.EndPoints = newEndPoints;
                    Result = newConsulResult;

                    _log.Info(_ => _("Obtained new list endpoints for service from Consul", unencryptedTags: new
                    {
                        serviceName = DeploymentName,
                        endpoints   = string.Join(", ", newEndPoints.Select(e => e.HostName + ':' + (e.Port?.ToString() ?? "")))
                    }));

                    isEndPointChanged = true;
                }

                bool isDeploymentDefinedChanged = IsDeploymentDefined(oldConsulResult) != IsDeploymentDefined(newConsulResult);


                if (!_firstTime && (isEndPointChanged || isDeploymentDefinedChanged))
                {
                    EndPointsChanged?.Post(newConsulResult);
                }


                _firstTime = false;

                _initialized = Task.FromResult(1);
            }
        }