async Task OnTick()
        {
            try
            {
                bool batchWasFull;
                do
                {
                    LogEvent next;
                    while (_waitingBatch.Count < _batchSizeLimit &&
                           _queue.TryDequeue(out next))
                    {
                        if (CanInclude(next))
                        {
                            _waitingBatch.Enqueue(next);
                        }
                    }

                    if (_waitingBatch.Count == 0)
                    {
                        await OnEmptyBatchAsync();

                        return;
                    }

                    await EmitBatchAsync(_waitingBatch);

                    batchWasFull = _waitingBatch.Count >= _batchSizeLimit;
                    _waitingBatch.Clear();
                    _status.MarkSuccess();
                }while (batchWasFull); // Otherwise, allow the period to elapse
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Exception while emitting periodic batch from {0}: {1}", this, ex);
                _status.MarkFailure();
            }
            finally
            {
                if (_status.ShouldDropBatch)
                {
                    _waitingBatch.Clear();
                }

                if (_status.ShouldDropQueue)
                {
                    LogEvent evt;
                    while (_queue.TryDequeue(out evt))
                    {
                    }
                }

                lock (_stateLock)
                {
                    if (!_unloading)
                    {
                        SetTimer(_status.NextInterval);
                    }
                }
            }
        }
 public void WhenABatchSucceedsTheStatusResets()
 {
     var bcs = new BatchedConnectionStatus(DefaultPeriod);
     bcs.MarkFailure();
     bcs.MarkFailure();
     bcs.MarkSuccess();
     Assert.AreEqual(DefaultPeriod, bcs.NextInterval);
 }
Пример #3
0
        void OnTick()
        {
            try
            {
                do
                {
                    LogEvent next;
                    while (_waitingBatch.Count < _batchSizeLimit &&
                           _queue.TryDequeue(out next))
                    {
                        if (CanInclude(next))
                        {
                            _waitingBatch.Enqueue(next);
                        }
                    }

                    if (_waitingBatch.Count == 0)
                    {
                        OnEmptyBatch();
                        return;
                    }

                    EmitBatch(_waitingBatch);
                    _waitingBatch.Clear();
                    _status.MarkSuccess();
                }while (true);
            }
            catch (Exception ex)
            {
                SelfLog.WriteLine("Exception while emitting periodic batch from {0}: {1}", this, ex);
                _status.MarkFailure();
            }
            finally
            {
                if (_status.ShouldDropBatch)
                {
                    _waitingBatch.Clear();
                }

                if (_status.ShouldDropQueue)
                {
                    LogEvent evt;
                    while (_queue.TryDequeue(out evt))
                    {
                    }
                }

                lock (_stateLock)
                {
                    if (!_unloading)
                    {
                        _timer.Change(_status.NextInterval, TimeSpan.FromMilliseconds(-1));
                    }
                }
            }
        }