示例#1
0
        private void Monitor()
        {
            while (true)
            {
                if (_processDnsResultHasEverBeenCalled && _cluster.ShouldDnsMonitorStop())
                {
                    return;
                }

                List <SrvRecord> srvRecords = null;
                try
                {
                    srvRecords = _dnsResolver.ResolveSrvRecords(_service, _cancellationToken);
                }
                catch (Exception exception)
                {
                    if (!_processDnsResultHasEverBeenCalled)
                    {
                        _cluster.ProcessDnsException(exception);
                    }
                }

                if (srvRecords != null)
                {
                    var endPoints = GetValidEndPoints(srvRecords);
                    if (endPoints.Count > 0)
                    {
                        _cluster.ProcessDnsResults(endPoints);
                        _processDnsResultHasEverBeenCalled = true;
                    }
                    else
                    {
                        if (_sdamInformationEventHandler != null)
                        {
                            var message = $"A DNS SRV query on \"{_service}\" returned no valid hosts.";
                            var sdamInformationEvent = new SdamInformationEvent(() => message);
                            _sdamInformationEventHandler(sdamInformationEvent);
                        }
                    }
                }

                if (_cluster.ShouldDnsMonitorStop())
                {
                    return;
                }

                _cancellationToken.ThrowIfCancellationRequested();
                var delay = ComputeRescanDelay(srvRecords);
                Thread.Sleep(delay);
            }
        }