Exemple #1
0
        private void CheckHeartbeat()
        {
            foreach (var peer in _peerRepository.GetAll())
            {
                Task.Run(async() =>
                {
                    var result       = await _peerChallengeRequest.ChallengePeerAsync(peer.PeerId);
                    var counterValue = _nonResponsivePeerMap.GetOrAdd(peer.DocumentId, 0);
                    _logger.Verbose(
                        $"Heartbeat result: {result.ToString()} Peer: {peer.PeerId} Non-Responsive Counter: {counterValue}");
                    if (!result)
                    {
                        _nonResponsivePeerMap[peer.DocumentId] += 1;
                        counterValue += 1;

                        if (counterValue >= _maxNonResponsiveCounter)
                        {
                            _peerRepository.Delete(peer.DocumentId);
                            _nonResponsivePeerMap.TryRemove(peer.DocumentId, out _);
                            _logger.Verbose(
                                $"Peer reached maximum non-responsive count: {peer.PeerId}. Evicted from repository");
                        }
                    }
                    else
                    {
                        _nonResponsivePeerMap[peer.DocumentId] = 0;
                    }
                }).ConfigureAwait(false);
            }
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="removePeerRequest"></param>
        /// <param name="channelHandlerContext"></param>
        /// <param name="senderPeerId"></param>
        /// <param name="correlationId"></param>
        /// <returns></returns>
        protected override RemovePeerResponse HandleRequest(RemovePeerRequest removePeerRequest,
                                                            IChannelHandlerContext channelHandlerContext,
                                                            PeerId senderPeerId,
                                                            ICorrelationId correlationId)
        {
            Guard.Argument(removePeerRequest, nameof(removePeerRequest)).NotNull();
            Guard.Argument(channelHandlerContext, nameof(channelHandlerContext)).NotNull();
            Guard.Argument(senderPeerId, nameof(senderPeerId)).NotNull();
            Logger.Debug("Received message of type RemovePeerRequest");

            uint peerDeletedCount = 0;

            var publicKeyIsEmpty = removePeerRequest.PublicKey.IsEmpty;

            var peersToDelete = _peerRepository.FindAll(new Specification <Peer>(peer =>
                                                                                 peer.PeerId.Ip.SequenceEqual(removePeerRequest.PeerIp) &&
                                                                                 (publicKeyIsEmpty || peer.PeerId.PublicKey.SequenceEqual(removePeerRequest.PublicKey.ToByteArray())))).ToArray();

            foreach (var peerToDelete in peersToDelete)
            {
                _peerRepository.Delete(peerToDelete);
                peerDeletedCount += 1;
            }

            return(new RemovePeerResponse
            {
                DeletedCount = peerDeletedCount
            });
        }
Exemple #3
0
        private void CheckHeartbeat()
        {
            foreach (var peer in _peerRepository.GetAll())
            {
                Task.Run(async() =>
                {
                    var result       = await _peerChallengeRequest.ChallengePeerAsync(peer.PeerId);
                    var counterValue = _nonResponsivePeerMap.GetOrAdd(peer.DocumentId, 0);
                    _logger.Verbose(
                        $"Heartbeat result: {result.ToString()} Peer: {peer.PeerId} Non-Responsive Counter: {counterValue}");
                    if (!result)
                    {
                        // @TODO touch last seen on peer
                        _nonResponsivePeerMap[peer.DocumentId] += 1;
                        counterValue += 1;

                        if (counterValue >= _maxNonResponsiveCounter)
                        {
                            // Remove all non POA nodes at the moment until node is using same p2p discovery
                            if (!peer.IsPoaNode)
                            {
                                _peerRepository.Delete(peer.DocumentId);
                                _nonResponsivePeerMap.TryRemove(peer.DocumentId, out _);
                            }
                            _logger.Verbose(
                                $"Peer reached maximum non-responsive count: {peer.PeerId}. Evicted from repository");
                        }
                    }
                    else
                    {
                        peer.Touch();
                        _peerRepository.Update(peer);
                        _nonResponsivePeerMap[peer.DocumentId] = 0;
                    }
                }).ConfigureAwait(false);
            }
        }