-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[sdk-metrics] Exemplar concurrency updates (#5465)
Co-authored-by: Utkarsh Umesan Pillai <66651184+utpilla@users.noreply.github.com>
- Loading branch information
1 parent
b10f84a
commit 1a607c8
Showing
6 changed files
with
101 additions
and
37 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
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,37 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
namespace OpenTelemetry.Internal; | ||
|
||
// Note: Inspired by https://devblogs.microsoft.com/pfxteam/getting-random-numbers-in-a-thread-safe-way/ | ||
internal static class ThreadSafeRandom | ||
{ | ||
#if NET6_0_OR_GREATER | ||
public static int Next(int min, int max) | ||
{ | ||
return Random.Shared.Next(min, max); | ||
} | ||
#else | ||
private static readonly Random GlobalRandom = new(); | ||
|
||
[ThreadStatic] | ||
private static Random? localRandom; | ||
|
||
public static int Next(int min, int max) | ||
{ | ||
var local = localRandom; | ||
if (local == null) | ||
{ | ||
int seed; | ||
lock (GlobalRandom) | ||
{ | ||
seed = GlobalRandom.Next(); | ||
} | ||
|
||
localRandom = local = new Random(seed); | ||
} | ||
|
||
return local.Next(min, max); | ||
} | ||
#endif | ||
} |