Exemplo n.º 1
0
        void AddValidNode(NodeInfo nodeInfo)
        {
            if (!nodeInfo.IsPublicEndPoint)
            {
                return;
            }

            if (nodeInfo.NodeId == LocalId)
            {
                return;
            }

            if (!_bucket.Contains(nodeInfo.NodeId))
            {
                Log.Trace($"Adding new node {nodeInfo.PublicEndPoint}.", this);
            }

            var evictNode = _bucket.AddOrUpdate(nodeInfo);

            if (evictNode != null)
            {
                lock (_lock)
                {
                    if (_evictionsCount > 8)
                    {
                        return;
                    }
                    _evictionsCount++;
                }
                TaskRunner.Run(async() =>
                {
                    var client = new NodeClient(evictNode.PublicEndPoint);
                    var ni     = await client.DownloadNodeInfo();
                    if (ni.ResultType == DownloadResultTypes.Ok)
                    {
                        _bucket.AddOrUpdate(evictNode);
                    }
                    else
                    {
                        _bucket.Remove(evictNode.NodeId);
                        _bucket.AddOrUpdate(nodeInfo);
                    }

                    lock (_lock)
                    {
                        _evictionsCount--;
                    }
                });
            }
        }