Skip to content

Commit

Permalink
-Adding newly added project references to R4.slnf and R5.slnf
Browse files Browse the repository at this point in the history
-Adding simple threadsafe logic for creating CosmosClient instance
  • Loading branch information
mahajan-xor committed Jul 2, 2024
1 parent 7a7650b commit b15ee62
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
3 changes: 3 additions & 0 deletions R4.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"src\\Microsoft.Health.Fhir.Azure\\Microsoft.Health.Fhir.Azure.csproj",
"src\\Microsoft.Health.Fhir.Core.UnitTests\\Microsoft.Health.Fhir.Core.UnitTests.csproj",
"src\\Microsoft.Health.Fhir.Core\\Microsoft.Health.Fhir.Core.csproj",
"src\Microsoft.Health.Fhir.CosmosDb.Core\Microsoft.Health.Fhir.CosmosDb.Core.csproj",
"src\Microsoft.Health.Fhir.CosmosDb.Initialization.UnitTests\Microsoft.Health.Fhir.CosmosDb.Initialization.UnitTests.csproj",
"src\Microsoft.Health.Fhir.CosmosDb.Initialization\Microsoft.Health.Fhir.CosmosDb.Initialization.csproj",
"src\\Microsoft.Health.Fhir.CosmosDb.UnitTests\\Microsoft.Health.Fhir.CosmosDb.UnitTests.csproj",
"src\\Microsoft.Health.Fhir.CosmosDb\\Microsoft.Health.Fhir.CosmosDb.csproj",
"src\\Microsoft.Health.Fhir.R4.Api\\Microsoft.Health.Fhir.R4.Api.csproj",
Expand Down
3 changes: 3 additions & 0 deletions R5.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"src\\Microsoft.Health.Fhir.Azure\\Microsoft.Health.Fhir.Azure.csproj",
"src\\Microsoft.Health.Fhir.Core.UnitTests\\Microsoft.Health.Fhir.Core.UnitTests.csproj",
"src\\Microsoft.Health.Fhir.Core\\Microsoft.Health.Fhir.Core.csproj",
"src\Microsoft.Health.Fhir.CosmosDb.Core\Microsoft.Health.Fhir.CosmosDb.Core.csproj",
"src\Microsoft.Health.Fhir.CosmosDb.Initialization.UnitTests\Microsoft.Health.Fhir.CosmosDb.Initialization.UnitTests.csproj",
"src\Microsoft.Health.Fhir.CosmosDb.Initialization\Microsoft.Health.Fhir.CosmosDb.Initialization.csproj",
"src\\Microsoft.Health.Fhir.CosmosDb.UnitTests\\Microsoft.Health.Fhir.CosmosDb.UnitTests.csproj",
"src\\Microsoft.Health.Fhir.CosmosDb\\Microsoft.Health.Fhir.CosmosDb.csproj",
"src\\Microsoft.Health.Fhir.R5.Api\\Microsoft.Health.Fhir.R5.Api.csproj",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class FhirCosmosClientInitializer : ICosmosClientInitializer
private readonly ILogger<FhirCosmosClientInitializer> _logger;
private readonly Func<IEnumerable<RequestHandler>> _requestHandlerFactory;
private readonly RetryExceptionPolicyFactory _retryExceptionPolicyFactory;
private readonly ConcurrentDictionary<string, CosmosClient> _cosmosClients;
private static CosmosClient _cosmosClient;
private readonly object _lockObject;

public FhirCosmosClientInitializer(
Expand All @@ -48,7 +48,6 @@ public FhirCosmosClientInitializer(
_requestHandlerFactory = requestHandlerFactory;
_retryExceptionPolicyFactory = retryExceptionPolicyFactory;
_logger = logger;
_cosmosClients = new ConcurrentDictionary<string, CosmosClient>();
_lockObject = new object();
}

Expand All @@ -60,24 +59,16 @@ public CosmosClient CreateCosmosClient(CosmosDataStoreConfiguration configuratio
string key = string.IsNullOrEmpty(configuration.Host) ? "local" : configuration.Host;

Check warning

Code scanning / CodeQL

Useless assignment to local variable Warning

This assignment to
key
is useless, since its value is never read.

// Thread-safe logic to ensure that a single instance of CosmosClient is created per host.
if (_cosmosClients.TryGetValue(key, out CosmosClient client1))
if (_cosmosClient != null)
{
return client1;
return _cosmosClient;
}
else
{
lock (_lockObject)
{
if (_cosmosClients.TryGetValue(key, out CosmosClient client2))
{
return client2;
}
else
{
var client = CreateCosmosClientInternal(configuration);
_cosmosClients.TryAdd(key, client);
return client;
}
_cosmosClient = CreateCosmosClientInternal(configuration);

Check notice

Code scanning / CodeQL

Static field written by instance method Note

Write to static field from instance method, property, or constructor.
return _cosmosClient;
}
}
}
Expand Down

0 comments on commit b15ee62

Please sign in to comment.