Skip to content

Commit

Permalink
ConcurrentRequestQueue - Always see queue as empty after having reach…
Browse files Browse the repository at this point in the history
…ed RequestLimit
  • Loading branch information
snakefoot authored and 304NotModified committed May 28, 2019
1 parent 27fb515 commit 705c39c
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/NLog/Targets/Wrappers/ConcurrentRequestQueue.cs
Expand Up @@ -79,6 +79,7 @@ public ConcurrentRequestQueue(int requestLimit, AsyncTargetWrapperOverflowAction
public override bool Enqueue(AsyncLogEventInfo logEventInfo)
{
long currentCount = Interlocked.Increment(ref _count);
bool queueWasEmpty = currentCount == 1; // Inserted first item in empty queue
if (currentCount > RequestLimit)
{
InternalLogger.Debug("Async queue is full");
Expand All @@ -91,18 +92,19 @@ public override bool Enqueue(AsyncLogEventInfo logEventInfo)
if (_logEventInfoQueue.TryDequeue(out var lostItem))
{
InternalLogger.Debug("Discarding one element from queue");
currentCount = Interlocked.Decrement(ref _count);
queueWasEmpty = Interlocked.Decrement(ref _count) == 1 || queueWasEmpty;
OnLogEventDropped(lostItem.LogEvent);
break;
}
Interlocked.Decrement(ref _count);
currentCount = Interlocked.Increment(ref _count);
currentCount = Interlocked.Read(ref _count);
queueWasEmpty = true;
} while (currentCount > RequestLimit);
}
break;
case AsyncTargetWrapperOverflowAction.Block:
{
currentCount = WaitForBelowRequestLimit();
WaitForBelowRequestLimit();
queueWasEmpty = true;
}
break;
case AsyncTargetWrapperOverflowAction.Grow:
Expand All @@ -115,10 +117,10 @@ public override bool Enqueue(AsyncLogEventInfo logEventInfo)
}
}
_logEventInfoQueue.Enqueue(logEventInfo);
return currentCount == 1; // Inserted first item in empty queue
return queueWasEmpty;
}

private long WaitForBelowRequestLimit()
private void WaitForBelowRequestLimit()
{
long currentCount;
bool lockTaken = false;
Expand Down Expand Up @@ -146,8 +148,6 @@ private long WaitForBelowRequestLimit()
if (lockTaken)
Monitor.Exit(_logEventInfoQueue);
}

return currentCount;
}

private long TrySpinWaitForLowerCount()
Expand All @@ -157,7 +157,6 @@ private long TrySpinWaitForLowerCount()
SpinWait spinWait = new SpinWait();
for (int i = 0; i <= 20; ++i)
{
Interlocked.Decrement(ref _count);
if (spinWait.NextSpinWillYield)
{
if (firstYield)
Expand All @@ -166,7 +165,7 @@ private long TrySpinWaitForLowerCount()
}

spinWait.SpinOnce();
currentCount = Interlocked.Increment(ref _count);
currentCount = Interlocked.Read(ref _count);
if (currentCount <= RequestLimit)
break;
}
Expand Down

0 comments on commit 705c39c

Please sign in to comment.