public IncomingMessageThrottler( IRateLimiterRepository rateLimiterRepository, int messageLimitPerMinute = 10) { _rateLimiterRepository = rateLimiterRepository; _messageLimitPerMinute = messageLimitPerMinute; _lastRefreshTime = DateTime.Now; // RefreshOldRecords(); }
public async Task <bool> Process(IEnumerable <IWorkItem> items, IRateLimiterRepository repo, IDispatcherValidateProgressBatch progressDispatcher) { try { var rnd = new Random(); const int MaxAttempts = 10; foreach (var item in items) { int i = 0; item.Result = await GetResult(item); while (item.Result.Contains("OVER_QUERY_LIMIT") && i < MaxAttempts) { item.Result = await GetResult(item); await Task.Delay(rnd.Next(850, 1000)); i++; _logger.Debug(string.Format("OVER_QUERY_LIMIT, attempt: {0}", i)); } if (i < MaxAttempts) { await repo.SaveResult(item); } if (progressDispatcher != null) // avoid the case where there is no hub at all { await progressDispatcher.Send(new MessageProgressBatch { ID = item.HydrantId }, new Task(() => { })); } } } catch (Exception ex) { _logger.ErrorException("Error Task Process: ", ex); } return(true); }