private void QueueConsumer() { var buffer = new List <T>(_batchSize); var cancellationToken = _cancellationTokenSource.Token; while (true) { T item; if (_queue.IsCompleted && _queue.Count == 0) { break; } //wait for a first item if (!_queue.TryTake(out item, -1, cancellationToken)) { continue; } buffer.Add(item); //take the rest but don't wait while (buffer.Count < _batchSize && _queue.TryTake(out item)) { buffer.Add(item); } //at this point we have at least one request to process var commands = buffer.ToArray(); buffer.Clear(); _handler.Accept(commands); } }
public int GetTotalOrdersCount(List <object> orders) { OrdersVisitor orderVisitor = new OrdersVisitor(); foreach (var item in orders) { IAccept acceptor = (IAccept)item; acceptor.Accept(orderVisitor); } return(orderVisitor.TotalOrdersCount); }
private async void Read(string shardId) { var shardIterator = await GetShardIterator(shardId); while (true) { if (_cancellationTokenSource.Token.IsCancellationRequested) { break; } var request = new GetRecordsRequest { Limit = 100, ShardIterator = shardIterator }; var response = await _client.GetRecordsAsync(request); if (response.Records == null) { break; //todo: raise exception or event } if (response.Records.Any()) { _lastSequenceNumber = response.Records.Last().SequenceNumber; response.Records.ForEach(record => { var chunk = (CommandChunk)_serializer.Deserialize(record.Data.ToArray()); _chunkHandler.Accept(chunk); }); } else { await Task.Delay(TimeSpan.FromMilliseconds(100)); } shardIterator = response.NextShardIterator; } }