Ejemplo n.º 1
0
 public void ReportLatency(StoredLatency storedLatency)
 {
     lock (_lockObject)
     {
         _storedLatencyCount += storedLatency.NumItems;
         _storedLatencyTime  += storedLatency.Time;
     }
 }
        public override async Task ProcessPendingItems(CancellationToken cancellationToken)
        {
            while (!cancellationToken.IsCancellationRequested)
            {
                bool anyDataProcessed = false;

                SwitchDictionaries();
                Interlocked.Increment(ref _numSwitchDirectories);
                var processItems = ProcessItems;
                while (processItems.Values.Any())
                {
                    int storedItems             = 0;
                    IStreamPersisterBatch batch = null;

                    while (processItems.Values.Any() && !cancellationToken.IsCancellationRequested)
                    {
                        if (_persister != null)
                        {
                            if (_persister.SupportsBatches && batch == null)
                            {
                                batch = await _persister.CreateBatch(_entityType);
                            }

                            var processItemPair = processItems.First();
                            if (processItems.Remove(processItemPair.Key))
                            {
                                StoredLatency storedLatency;
                                if (processItemPair.Value.DeleteItem != null)
                                {
                                    storedLatency = await _persister.Delete(processItemPair.Value.DeleteItem, batch);
                                }
                                else if (processItemPair.Value.UpsertItem != null)
                                {
                                    storedLatency = await _persister.Upsert(processItemPair.Value.UpsertItem, batch);
                                }
                                else
                                {
                                    storedLatency = new StoredLatency()
                                    {
                                        NumItems = 0, Time = 0.0
                                    }
                                };
                                ReportLatency(storedLatency);
                                storedItems++;
                                anyDataProcessed = true;

                                Interlocked.Increment(ref _numProcessedItems);
                            }
                        }
                        else
                        {
                            Interlocked.Add(ref _numProcessedItems, processItems.Count);
                            processItems.Clear();
                        }
                    }

                    if (batch != null)
                    {
                        var storedLatency = await batch.Commit();

                        ReportLatency(storedLatency);
                    }
                }

                DisplayLatency();
                await Task.Delay(anyDataProcessed?_holdOffBusy : _holdOffIdle);
            }
        }