Skip to content

Commit

Permalink
add threadsafe lock
Browse files Browse the repository at this point in the history
  • Loading branch information
terencefan committed Jul 11, 2023
1 parent 6639876 commit 9ce2e5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/Microsoft.Azure.SignalR.Common/Endpoints/ServiceEndpoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public class ServiceEndpoint

private readonly TokenCredential _tokenCredential;

private AccessKey _accessKey;
private readonly object _lock = new object();

private volatile AccessKey _accessKey;

public string ConnectionString { get; }

Expand Down Expand Up @@ -80,7 +82,13 @@ internal AccessKey AccessKey
{
get
{
_accessKey ??= new AadAccessKey(_serviceEndpoint, _tokenCredential, ServerEndpoint);
if (_accessKey is null)
{
lock (_lock)
{
_accessKey ??= new AadAccessKey(_serviceEndpoint, _tokenCredential, ServerEndpoint);
}
}
return _accessKey;
}
private init => _accessKey = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Security.Claims;
using System.Threading;
using System.Threading.Tasks;

using Azure.Identity;

using Xunit;
Expand Down

0 comments on commit 9ce2e5b

Please sign in to comment.