-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ConcurrentRequestQueue depends on .NET ConcurrentQueue
- Loading branch information
Showing
5 changed files
with
303 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
// | ||
// Copyright (c) 2004-2018 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen | ||
// | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions | ||
// are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// * Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// * Neither the name of Jaroslaw Kowalski nor the names of its | ||
// contributors may be used to endorse or promote products derived from this | ||
// software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
// THE POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
|
||
#if NET4_5 || NET4_0 | ||
|
||
namespace NLog.Targets.Wrappers | ||
{ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
using NLog.Common; | ||
|
||
/// <summary> | ||
/// Concurrent Asynchronous request queue. | ||
/// </summary> | ||
class ConcurrentRequestQueue : IAsyncRequestQueue | ||
{ | ||
private readonly ConcurrentQueue<AsyncLogEventInfo> _logEventInfoQueue = new ConcurrentQueue<AsyncLogEventInfo>(); | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the AsyncRequestQueue class. | ||
/// </summary> | ||
/// <param name="requestLimit">Request limit.</param> | ||
/// <param name="overflowAction">The overflow action.</param> | ||
public ConcurrentRequestQueue(int requestLimit, AsyncTargetWrapperOverflowAction overflowAction) | ||
{ | ||
RequestLimit = requestLimit; | ||
OnOverflow = overflowAction; | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the request limit. | ||
/// </summary> | ||
public int RequestLimit { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the action to be taken when there's no more room in | ||
/// the queue and another request is enqueued. | ||
/// </summary> | ||
public AsyncTargetWrapperOverflowAction OnOverflow { get; set; } | ||
|
||
public bool IsEmpty => _logEventInfoQueue.IsEmpty; | ||
|
||
/// <summary> | ||
/// Gets the number of requests currently in the queue. | ||
/// </summary> | ||
public int Count => (int)_count; | ||
private long _count; | ||
|
||
/// <summary> | ||
/// Enqueues another item. If the queue is overflown the appropriate | ||
/// action is taken as specified by <see cref="OnOverflow"/>. | ||
/// </summary> | ||
/// <param name="logEventInfo">The log event info.</param> | ||
/// <returns>Queue was empty before enqueue</returns> | ||
public bool Enqueue(AsyncLogEventInfo logEventInfo) | ||
{ | ||
long currentCount = Interlocked.Increment(ref _count); | ||
if (currentCount > RequestLimit) | ||
{ | ||
InternalLogger.Debug("Async queue is full"); | ||
switch (OnOverflow) | ||
{ | ||
case AsyncTargetWrapperOverflowAction.Discard: | ||
{ | ||
InternalLogger.Debug("Discarding one element from queue"); | ||
do | ||
{ | ||
if (_logEventInfoQueue.TryDequeue(out var _)) | ||
{ | ||
currentCount = Interlocked.Decrement(ref _count); | ||
break; | ||
} | ||
currentCount = Interlocked.Read(ref _count); | ||
} while (currentCount > RequestLimit); | ||
} | ||
break; | ||
case AsyncTargetWrapperOverflowAction.Block: | ||
{ | ||
bool firstYield = true; | ||
SpinWait spinWait = new SpinWait(); | ||
do | ||
{ | ||
spinWait.SpinOnce(); | ||
if (firstYield && spinWait.NextSpinWillYield) | ||
{ | ||
firstYield = false; | ||
InternalLogger.Debug("Blocking because the overflow action is Block..."); | ||
} | ||
currentCount = Interlocked.Read(ref _count); | ||
} while (currentCount > RequestLimit); | ||
} | ||
break; | ||
} | ||
} | ||
_logEventInfoQueue.Enqueue(logEventInfo); | ||
return currentCount == 1; | ||
} | ||
|
||
/// <summary> | ||
/// Dequeues a maximum of <c>count</c> items from the queue | ||
/// and adds returns the list containing them. | ||
/// </summary> | ||
/// <param name="count">Maximum number of items to be dequeued (-1 means everything).</param> | ||
/// <returns>The array of log events.</returns> | ||
public AsyncLogEventInfo[] DequeueBatch(int count) | ||
{ | ||
if (_logEventInfoQueue.IsEmpty) | ||
return Internal.ArrayHelper.Empty<AsyncLogEventInfo>(); | ||
|
||
if (_count < count) | ||
count = (int)_count; | ||
|
||
var resultEvents = new List<AsyncLogEventInfo>(count); | ||
for (int i = 0; i < count; ++i) | ||
{ | ||
if (_logEventInfoQueue.TryDequeue(out var item)) | ||
{ | ||
Interlocked.Decrement(ref _count); | ||
resultEvents.Add(item); | ||
} | ||
else | ||
break; | ||
} | ||
|
||
if (resultEvents.Count == 0) | ||
return Internal.ArrayHelper.Empty<AsyncLogEventInfo>(); | ||
else | ||
return resultEvents.ToArray(); | ||
} | ||
|
||
/// <summary> | ||
/// Dequeues into a preallocated array, instead of allocating a new one | ||
/// </summary> | ||
/// <param name="count">Maximum number of items to be dequeued</param> | ||
/// <param name="result">Preallocated list</param> | ||
public void DequeueBatch(int count, IList<AsyncLogEventInfo> result) | ||
{ | ||
for (int i = 0; i < count; ++i) | ||
{ | ||
if (_logEventInfoQueue.TryDequeue(out var item)) | ||
{ | ||
Interlocked.Decrement(ref _count); | ||
result.Add(item); | ||
} | ||
else | ||
break; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Clears the queue. | ||
/// </summary> | ||
public void Clear() | ||
{ | ||
while (!_logEventInfoQueue.IsEmpty) | ||
_logEventInfoQueue.TryDequeue(out var _); | ||
Interlocked.Exchange(ref _count, 0); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// Copyright (c) 2004-2018 Jaroslaw Kowalski <jaak@jkowalski.net>, Kim Christensen, Julian Verdurmen | ||
// | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions | ||
// are met: | ||
// | ||
// * Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// | ||
// * Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// * Neither the name of Jaroslaw Kowalski nor the names of its | ||
// contributors may be used to endorse or promote products derived from this | ||
// software without specific prior written permission. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
// THE POSSIBILITY OF SUCH DAMAGE. | ||
// | ||
|
||
using System.Collections.Generic; | ||
using NLog.Common; | ||
|
||
namespace NLog.Targets.Wrappers | ||
{ | ||
interface IAsyncRequestQueue | ||
{ | ||
bool IsEmpty { get; } | ||
int RequestLimit { get; set; } | ||
AsyncTargetWrapperOverflowAction OnOverflow { get; set; } | ||
|
||
bool Enqueue(AsyncLogEventInfo logEventInfo); | ||
AsyncLogEventInfo[] DequeueBatch(int count); | ||
void DequeueBatch(int count, IList<AsyncLogEventInfo> result); | ||
void Clear(); | ||
} | ||
} |
Oops, something went wrong.