private async Task KeepSyncing(CancellationToken token) { int finalizeSignalsCount = 0; do { if (token.IsCancellationRequested) { return; } FastBlocksBatch request = PrepareRequest(); if (request != null) { request.Allocation = await _syncPeerPool.BorrowAsync(new FastBlocksSelectionStrategy(request.MinNumber, request.Prioritized), "fast blocks", 1000); Interlocked.Increment(ref _pendingRequests); Task task = ExecuteRequest(token, request); #pragma warning disable 4014 task.ContinueWith(t => #pragma warning restore 4014 { Interlocked.Decrement(ref _pendingRequests); }); } else { finalizeSignalsCount++; await Task.Delay(10); if (_logger.IsDebug) { _logger.Debug($"0 batches created with {_pendingRequests} pending requests."); } } } while (_pendingRequests != 0 || finalizeSignalsCount < 3 || !_fastBlocksFeed.IsFinished); if (_logger.IsInfo) { _logger.Info($"Finished download with {_pendingRequests} pending requests."); } }
private async Task <int> SyncOnce(CancellationToken token, StateSyncBatch request) { int requestSize = 0; if (request.RequestedNodes.Length != 0) { // should be random selection? (we do not know if they support what we need) request.AssignedPeer = await _syncPeerPool.BorrowAsync(new TotalDiffFilter(BySpeedSelectionStrategy.Fastest, request.RequiredPeerDifficulty), "node sync", 1000); Interlocked.Increment(ref _pendingRequests); // if (_logger.IsWarn) _logger.Warn($"Creating new request with {request.RequestedNodes.Length} nodes"); Task task = ExecuteRequest(token, request); #pragma warning disable 4014 task.ContinueWith(t => #pragma warning restore 4014 { if (t.IsFaulted) { if (_logger.IsWarn) { _logger.Warn($"Failure when executing node data request {t.Exception}"); } } Interlocked.Decrement(ref _pendingRequests); requestSize = request.RequestedNodes.Length; }); } else { await Task.Delay(50); if (_logger.IsDebug) { _logger.Debug($"DIAG: 0 batches created with {_pendingRequests} pending requests, {_feed.TotalNodesPending} pending nodes"); } } return(requestSize); }