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 10, 2023
1 parent 6639876 commit e83f2e8
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 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,16 @@ internal AccessKey AccessKey
{
get
{
_accessKey ??= new AadAccessKey(_serviceEndpoint, _tokenCredential, ServerEndpoint);
if (_accessKey is null)
{
lock (_lock)
{
if (_accessKey is null)
{
_accessKey ??= new AadAccessKey(_serviceEndpoint, _tokenCredential, ServerEndpoint);
}
}
}
return _accessKey;
}
private init => _accessKey = value;
Expand Down

0 comments on commit e83f2e8

Please sign in to comment.