Add max spin count limit to circular buffer (#1088)

* add max spin count limit

* fix typo
This commit is contained in:
Reiley Yang 2020-08-16 18:27:33 -07:00 committed by GitHub
parent ec5683adbc
commit 0d7ef909da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 4 deletions

View File

@ -87,11 +87,11 @@ namespace OpenTelemetry.Internal
}
/// <summary>
/// Attempts to add the specified item to the buffer.
/// Adds the specified item to the buffer.
/// </summary>
/// <param name="value">The value to add.</param>
/// <returns>Returns true if the item was added to the buffer successfully; false if the buffer is full.</returns>
public bool TryAdd(T value)
public bool Add(T value)
{
if (value == null)
{
@ -122,6 +122,57 @@ namespace OpenTelemetry.Internal
}
}
/// <summary>
/// Attempts to add the specified item to the buffer.
/// </summary>
/// <param name="value">The value to add.</param>
/// <param name="maxSpinCount">The maximum allowed spin count, when set to a negative number of zero, will spin indefinitely.</param>
/// <returns>Returns true if the item was added to the buffer successfully; false if the buffer is full or the spin count exeeded maxSpinCount.</returns>
public bool TryAdd(T value, int maxSpinCount)
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}
if (maxSpinCount <= 0)
{
return this.Add(value);
}
var spinCountDown = maxSpinCount;
while (true)
{
var tailSnapshot = this.tail;
var headSnapshot = this.head;
if (headSnapshot - tailSnapshot >= this.capacity)
{
return false; // buffer is full
}
var index = (int)(headSnapshot % this.capacity);
if (this.SwapIfNull(index, value))
{
if (Interlocked.CompareExchange(ref this.head, headSnapshot + 1, headSnapshot) == headSnapshot)
{
return true;
}
this.trait[index] = null;
}
spinCountDown--;
if (spinCountDown == 0)
{
return false; // exceeded maximum spin count
}
}
}
public IEnumerable<T> Consume(int count)
{
if (count <= 0)

View File

@ -113,7 +113,7 @@ namespace OpenTelemetry.Trace
/// <inheritdoc/>
public override void OnEnd(Activity activity)
{
if (this.queue.TryAdd(activity))
if (this.queue.TryAdd(activity, maxSpinCount: 50000))
{
if (this.queue.Count >= this.maxExportBatchSize)
{
@ -123,7 +123,7 @@ namespace OpenTelemetry.Trace
return; // enqueue succeeded
}
// drop item on the floor
// either queue is full or exceeded spin count, drop item on the floor
Interlocked.Increment(ref this.droppedCount);
}