private void NewNodeHandler(RedisChannel arg1, RedisValue arg2) { var ep = ((byte[])arg2).ParseIP(); var nn = new Node(Redis, (ip) => { NewNodeQueue.Post(ip); }, GeoIp, ep); if (!nn.IsBadNode().Result&& !nn.IsGoodNode().Result) //exclude already known nodes { NewNodeSemaphore.Wait(); Console.WriteLine($"Checking new node {ep}"); Task.Factory.StartNew(async() => { await nn.Connect(); NewNodeSemaphore.Release(); }); } }
private async Task NewNodeWorker() { while (Running) { var ep = await NewNodeQueue.ReceiveAsync(); var nn = new Node(Redis, (ip) => { NewNodeQueue.Post(ip); }, GeoIp, ep); if (!await nn.IsBadNode() && !await nn.IsGoodNode()) //exclude already known nodes { await NewNodeSemaphore.WaitAsync(); Console.WriteLine($"Checking new node {ep}"); await Task.Factory.StartNew(async() => { await nn.Connect(); NewNodeSemaphore.Release(); }); } } }
public async Task Run() { var db = Redis.GetDatabase(); //var sub = Redis.GetSubscriber(); //await sub.SubscribeAsync("hs:nodes:new-stream", NewNodeHandler); NewNodeWorkerTask = NewNodeWorker(); if (!await db.KeyExistsAsync("hs:nodes:good-nodes")) { Console.WriteLine("First run.. checking seeds"); //get seeds List <IPEndPoint> allIps = new List <IPEndPoint>(); foreach (var dseed in DNSSeeds) { var ips = await Dns.GetHostAddressesAsync(dseed); if (ips != null && ips.Length > 0) { allIps.AddRange(ips.Select(a => new IPEndPoint(a.MapToIPv6(), 8333))); } } foreach (var n in allIps) { NewNodeQueue.Post(n); } Console.WriteLine("First run complete."); } while (Running) { if (NewNodeQueue.Count < 100) { var s = new SemaphoreSlim(MaxPoll, MaxPoll); Console.WriteLine("Scrape starting."); var nodes = await GetGoodNodes(); foreach (var n in nodes) { await s.WaitAsync(); await Task.Factory.StartNew(async() => { var nn = new Node(Redis, (ip) => { NewNodeQueue.Post(ip); }, GeoIp, n); await nn.Connect(); s.Release(); }); } } else { Console.WriteLine($"Skipping poll to catch up on {NewNodeQueue.Count} new nodes"); } Console.WriteLine("Scrape complete."); await Task.Delay(TimeSpan.FromMinutes(5)); } }