clean up code (#2509)

This commit is contained in:
Reiley Yang 2021-10-25 12:58:45 -07:00 committed by GitHub
parent 884b224508
commit d81ad4052f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 32 deletions

View File

@ -117,7 +117,7 @@ namespace OpenTelemetry
{
Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds));
if (Interlocked.Increment(ref this.shutdownCount) > 1)
if (Interlocked.CompareExchange(ref this.shutdownCount, 1, 0) != 0)
{
return false; // shutdown already called
}

View File

@ -30,7 +30,7 @@ namespace OpenTelemetry.Metrics
private readonly TaskCompletionSource<bool> shutdownTcs = new TaskCompletionSource<bool>();
private AggregationTemporality preferredAggregationTemporality = CumulativeAndDelta;
private AggregationTemporality supportedAggregationTemporality = CumulativeAndDelta;
private int shutdownTimeout = int.MinValue;
private int shutdownCount;
private TaskCompletionSource<bool> collectionTcs;
public BaseProvider ParentProvider { get; private set; }
@ -76,7 +76,7 @@ namespace OpenTelemetry.Metrics
{
Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds));
var kickoff = false;
var shouldRunCollect = false;
var tcs = this.collectionTcs;
if (tcs == null)
@ -87,39 +87,34 @@ namespace OpenTelemetry.Metrics
if (tcs == null)
{
kickoff = true;
shouldRunCollect = true;
tcs = new TaskCompletionSource<bool>();
this.collectionTcs = tcs;
}
}
}
if (!shouldRunCollect)
{
return Task.WaitAny(tcs.Task, this.shutdownTcs.Task, Task.Delay(timeoutMilliseconds)) == 0 ? tcs.Task.Result : false;
}
var result = false;
try
{
if (kickoff)
lock (this.onCollectLock)
{
lock (this.onCollectLock)
{
this.collectionTcs = null;
bool result = this.OnCollect(timeoutMilliseconds);
tcs.TrySetResult(result);
return result;
}
this.collectionTcs = null;
result = this.OnCollect(timeoutMilliseconds);
}
return Task.WaitAny(tcs.Task, this.shutdownTcs.Task, Task.Delay(timeoutMilliseconds)) == 0 ? tcs.Task.Result : false;
}
catch (Exception)
{
if (kickoff)
{
tcs.TrySetResult(false);
// TODO: OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Collect), ex);
}
return false;
// TODO: OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Shutdown), ex);
}
tcs.TrySetResult(result);
return result;
}
/// <summary>
@ -144,27 +139,23 @@ namespace OpenTelemetry.Metrics
{
Guard.InvalidTimeout(timeoutMilliseconds, nameof(timeoutMilliseconds));
if (Interlocked.CompareExchange(ref this.shutdownTimeout, timeoutMilliseconds, int.MinValue) != int.MinValue)
if (Interlocked.CompareExchange(ref this.shutdownCount, 1, 0) != 0)
{
return false; // shutdown already called
}
this.shutdownTimeout = timeoutMilliseconds;
var result = false;
try
{
bool result = this.OnShutdown(this.shutdownTimeout);
this.shutdownTcs.TrySetResult(result);
return result;
result = this.OnShutdown(timeoutMilliseconds);
}
catch (Exception)
{
this.shutdownTcs.TrySetResult(false);
// TODO: OpenTelemetrySdkEventSource.Log.SpanProcessorException(nameof(this.Shutdown), ex);
return false;
}
this.shutdownTcs.TrySetResult(result);
return result;
}
/// <inheritdoc/>