Пример #1
0
        public void RemovePeer_ShouldNotRetrievableWithTheProvider()
        {
            var endpoint = "127.0.0.1:5677";

            _reconnectionService.SchedulePeerForReconnection(endpoint);
            _reconnectionService.CancelReconnection(endpoint);

            var peers = _connectionStateProvider.GetPeersReadyForReconnection(TimestampHelper.GetUtcNow().
                                                                              AddSeconds(1));

            peers.Count.ShouldBe(0);
        }
        public async Task DisconnectAsync(IPeer peer, bool sendDisconnect = false)
        {
            // clean the pool
            if (_peerPool.RemovePeer(peer.Info.Pubkey) == null)
            {
                Logger.LogWarning($"{peer} was not found in pool.");
            }

            // cancel any pending reconnection
            _reconnectionService.CancelReconnection(peer.RemoteEndpoint.ToString());

            // dispose the peer
            await peer.DisconnectAsync(sendDisconnect);

            Logger.LogInformation($"Removed peer {peer}");
        }
Пример #3
0
 /// <summary>
 /// Attempts to remove a node from the connected network nodes
 /// </summary>
 /// <param name="address">ip address</param>
 /// <returns></returns>
 public async Task <bool> RemovePeerAsync(string address)
 {
     _reconnectionService.CancelReconnection(address);
     return(await _networkService.RemovePeerAsync(address));
 }
Пример #4
0
        internal async Task DoReconnectionJobAsync()
        {
            CheckNtpClockDrift();

            await _networkService.CheckPeersHealthAsync();

            var peersToConnect = _reconnectionService.GetPeersReadyForReconnection(TimestampHelper.GetUtcNow());

            if (peersToConnect.Count <= 0)
            {
                return;
            }

            foreach (var peerToConnect in peersToConnect)
            {
                string peerEndpoint = peerToConnect.Endpoint;
                if (!AElfPeerEndpointHelper.TryParse(peerEndpoint, out var parsed))
                {
                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogWarning($"Invalid {peerEndpoint}.");
                    }

                    continue;
                }

                // check that we haven't already reconnected to this node
                if (_peerPool.FindPeerByEndpoint(parsed) != null)
                {
                    Logger.LogDebug($"Peer {peerEndpoint} already in the pool, no need to reconnect.");

                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogDebug($"Could not find to {peerEndpoint}.");
                    }

                    continue;
                }

                Logger.LogDebug($"Starting reconnection to {peerToConnect.Endpoint}.");

                var connected = false;

                try
                {
                    connected = await _networkService.AddPeerAsync(peerEndpoint);
                }
                catch (Exception ex)
                {
                    // down the stack the AddPeerAsync rethrows any exception,
                    // in order to continue this job, Exception has to be catched for now.
                    Logger.LogInformation(ex, $"Could not re-connect to {peerEndpoint}.");
                }

                if (connected)
                {
                    Logger.LogDebug($"Reconnection to {peerEndpoint} succeeded.");

                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogDebug($"Could not find {peerEndpoint}.");
                    }
                }
                else
                {
                    var timeExtension = _networkOptions.PeerReconnectionPeriod * (int)Math.Pow(2, ++peerToConnect.RetryCount);
                    peerToConnect.NextAttempt = TimestampHelper.GetUtcNow().AddMilliseconds(timeExtension);

                    // if the option is set, verify that the next attempt does not exceed
                    // the maximum reconnection time.
                    if (_networkOptions.MaximumReconnectionTime != 0)
                    {
                        var maxReconnectionDate = peerToConnect.DisconnectionTime +
                                                  TimestampHelper.DurationFromMilliseconds(_networkOptions.MaximumReconnectionTime);

                        if (peerToConnect.NextAttempt > maxReconnectionDate)
                        {
                            _reconnectionService.CancelReconnection(peerEndpoint);
                            Logger.LogDebug($"Maximum reconnection time reached {peerEndpoint}, " +
                                            $"next was {peerToConnect.NextAttempt}.");

                            continue;
                        }
                    }

                    Logger.LogDebug($"Could not connect to {peerEndpoint}, next attempt {peerToConnect.NextAttempt}, " +
                                    $"current retries {peerToConnect.RetryCount}.");
                }
            }

            void CheckNtpClockDrift()
            {
                try
                {
                    _networkService.CheckNtpDrift();
                }
                catch (Exception)
                {
                    // swallow any exception, we are not interested in anything else than valid checks.
                }
            }
        }
Пример #5
0
        internal async Task DoReconnectionJobAsync()
        {
            CheckNtpClockDrift();

            await _networkService.SendHealthChecksAsync();

            var peersToConnect = _reconnectionService.GetPeersReadyForReconnection(TimestampHelper.GetUtcNow());

            if (peersToConnect.Count <= 0)
            {
                Logger.LogDebug("No peers to reconnect.");
                return;
            }

            foreach (var peerToConnect in peersToConnect)
            {
                string peerEndpoint = peerToConnect.Endpoint;
                if (!AElfPeerEndpointHelper.TryParse(peerEndpoint, out var parsed))
                {
                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogWarning($"Invalid {peerEndpoint}.");
                    }

                    continue;
                }

                // check that we haven't already reconnected to this node
                if (_peerPool.FindPeerByEndpoint(parsed) != null)
                {
                    Logger.LogDebug($"Peer {peerEndpoint} already in the pool, no need to reconnect.");

                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogDebug($"Could not find to {peerEndpoint}.");
                    }

                    continue;
                }

                Logger.LogDebug($"Starting reconnection to {peerToConnect.Endpoint}.");

                var connected = false;

                try
                {
                    connected = await _networkService.AddPeerAsync(peerEndpoint);
                }
                catch (Exception ex)
                {
                    // down the stack the AddPeerAsync rethrows any exception,
                    // in order to continue this job, Exception has to be catched for now.
                    Logger.LogError(ex, $"Could not re-connect to {peerEndpoint}.");
                }

                if (connected)
                {
                    Logger.LogDebug($"Reconnection to {peerEndpoint} succeeded.");

                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogDebug($"Could not find {peerEndpoint}.");
                    }
                }
                else
                {
                    peerToConnect.NextAttempt =
                        TimestampHelper.GetUtcNow().AddMilliseconds(_networkOptions.PeerReconnectionPeriod);

                    Logger.LogDebug($"Could not connect to {peerEndpoint}, next attempt {peerToConnect.NextAttempt}.");
                }
            }

            void CheckNtpClockDrift()
            {
                try
                {
                    _networkService.CheckNtpDrift();
                }
                catch (Exception)
                {
                    // swallow any exception, we are not interested in anything else than valid checks.
                }
            }
        }
Пример #6
0
        internal async Task DoReconnectionJobAsync()
        {
            await _networkService.SendHealthChecksAsync();

            var peersToConnect = _reconnectionService.GetPeersReadyForReconnection(TimestampHelper.GetUtcNow());

            if (peersToConnect.Count <= 0)
            {
                Logger.LogDebug("No peers to reconnect.");
                return;
            }

            foreach (var peerToConnect in peersToConnect)
            {
                string peerEndpoint = peerToConnect.Endpoint;

                // check that we haven't already reconnected to this node
                if (_peerPool.FindPeerByEndpoint(IpEndPointHelper.Parse(peerEndpoint)) != null)
                {
                    Logger.LogDebug($"Peer {peerEndpoint} already in the pool, no need to reconnect.");

                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogDebug($"Could not find to {peerEndpoint}.");
                    }

                    continue;
                }

                Logger.LogDebug($"Starting reconnection to {peerToConnect.Endpoint}.");

                var connected = false;

                try
                {
                    connected = await _networkService.AddPeerAsync(peerEndpoint);
                }
                catch (Exception ex)
                {
                    // todo consider different handling of the exception in dialer
                    // down the stack the AddPeerAsync rethrows any exception,
                    // in order to continue this job, Exception has to be catched for now.
                    Logger.LogError(ex, $"Could not re-connect to {peerEndpoint}.");
                }

                if (connected)
                {
                    Logger.LogDebug($"Reconnection to {peerEndpoint} succeeded.");

                    if (!_reconnectionService.CancelReconnection(peerEndpoint))
                    {
                        Logger.LogDebug($"Could not find {peerEndpoint}.");
                    }
                }
                else
                {
                    peerToConnect.NextAttempt =
                        TimestampHelper.GetUtcNow().AddMilliseconds(_networkOptions.PeerReconnectionPeriod);

                    Logger.LogDebug($"Could not connect to {peerEndpoint}, next attempt {peerToConnect.NextAttempt}.");
                }
            }
        }