private async Task RunAsync(CancellationToken stoppingToken) { try { var sleepTimeLimit = Options.EmptySleepTime * 1000; var sleepTime = 0; var bucket = CreateBucket(Options.Speed); var batch = (int)Options.Batch; while (!stoppingToken.IsCancellationRequested) { var requests = (await _services.Scheduler.DequeueAsync(batch)).ToArray(); if (requests.Length > 0) { //重置 sleepTime = 0; foreach (var request in requests) { while (bucket.ShouldThrottle(1, out var waitTimeMillis)) { await Task.Delay(waitTimeMillis, default); } await PublishRequestMessagesAsync(request); } } else { OnSchedulerEmpty?.Invoke(); sleepTime += 10; // 等待空队列超时 if (!await WaitForContinueAsync(sleepTime, sleepTimeLimit)) { break; } } } } catch (Exception e) { Logger.LogError($"{Id} exited by exception: {e}"); } finally { Stop(); } }
private async Task RunAsync(CancellationToken stoppingToken) { var tuple = ComputeIntervalAndDequeueBatch(Options.Speed); var sleepTimeLimit = Options.EmptySleepTime * 1000; await Task.Factory.StartNew(async() => { try { var pausedTime = 0; var sleepTime = 0; var printFlag = 0; while (!stoppingToken.IsCancellationRequested) { printFlag += tuple.Interval; if (printFlag >= 5000) { printFlag = 0; await _services.StatisticsClient.PrintAsync(Id); } if (_requestedQueue.Count > Options.RequestedQueueCount) { if (pausedTime > sleepTimeLimit) { Logger.LogInformation( $"{Id} paused too much time"); break; } pausedTime += tuple.Interval; Logger.LogInformation( $"{Id} too much requests enqueued"); await Task.Delay(tuple.Interval, default); continue; } pausedTime = 0; var timeoutRequests = _requestedQueue.GetAllTimeoutList(); if (timeoutRequests.Length > 0) { foreach (var request in timeoutRequests) { Logger.LogWarning( $"{Id} request {request.RequestUri}, {request.Hash} timeout"); } await AddRequestsAsync(timeoutRequests); OnTimeout?.Invoke(timeoutRequests); } else { var requests = (await _services.Scheduler.DequeueAsync(tuple.Batch)).ToArray(); if (requests.Length > 0) { sleepTime = 0; } else { sleepTime += tuple.Interval; if (sleepTime > sleepTimeLimit) { Logger.LogError($"Exit by sleep too long: {sleepTime}"); break; } OnSchedulerEmpty?.Invoke(); } foreach (var request in requests) { ConfigureRequest(request); } if (!await PublishRequestMessagesAsync(requests)) { Logger.LogError("Exit by publish request message failed"); break; } } await Task.Delay(tuple.Interval, default); } } catch (Exception e) { Logger.LogError($"{Id} exited by exception: {e}"); } finally { await ExitAsync(); } }, stoppingToken); }
private async Task RunAsync(CancellationToken stoppingToken) { try { var sleepTimeLimit = Options.EmptySleepTime * 1000; var bucket = CreateBucket(Options.Speed); var sleepTime = 0; var batch = (int)Options.Batch; var start = DateTime.Now; var end = start; PrintStatistics(stoppingToken); while (!stoppingToken.IsCancellationRequested) { if (_requestedQueue.Count > Options.RequestedQueueCount) { sleepTime += 10; if (await WaitForContinueAsync(sleepTime, sleepTimeLimit, (end - start).TotalSeconds, $"{Id} too much requests enqueued")) { continue; } else { break; } } if (await HandleTimeoutRequestAsync()) { continue; } var requests = (await _services.Scheduler.DequeueAsync(batch)).ToArray(); if (requests.Length > 0) { sleepTime = 0; foreach (var request in requests) { ConfigureRequest(request); while (bucket.ShouldThrottle(1, out var waitTimeMillis)) { await Task.Delay(waitTimeMillis, default); } if (!await PublishRequestMessagesAsync(request)) { Logger.LogError("Exit by publish request message failed"); break; } } end = DateTime.Now; } else { OnSchedulerEmpty?.Invoke(); sleepTime += 10; if (!await WaitForContinueAsync(sleepTime, sleepTimeLimit, (end - start).TotalSeconds)) { break; } } } } catch (Exception e) { Logger.LogError($"{Id} exited by exception: {e}"); } finally { await ExitAsync(); } }