Skip to content

Commit e94de12

Browse files
authored
Merge pull request #811 from brucezlata/fixcosmosclientleak
fix cosmos client leak
2 parents 50869e1 + ebb1689 commit e94de12

File tree

6 files changed

+75
-18
lines changed

6 files changed

+75
-18
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Microsoft.Azure.Cosmos;
2+
3+
namespace WorkflowCore.Providers.Azure.Interface
4+
{
5+
public interface ICosmosClientFactory
6+
{
7+
CosmosClient GetCosmosClient();
8+
}
9+
}

src/providers/WorkflowCore.Providers.Azure/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ public static WorkflowOptions UseAzureServiceBusEventHub(
2828

2929
public static WorkflowOptions UseCosmosDbPersistence(this WorkflowOptions options, string connectionString, string databaseId)
3030
{
31-
options.Services.AddTransient<ICosmosDbProvisioner>(sp => new CosmosDbProvisioner(connectionString, sp.GetService<ILoggerFactory>()));
32-
options.UsePersistence(sp => new CosmosDbPersistenceProvider(connectionString, databaseId, sp.GetService<ICosmosDbProvisioner>()));
31+
options.Services.AddSingleton<ICosmosClientFactory>(sp => new CosmosClientFactory(connectionString));
32+
options.Services.AddTransient<ICosmosDbProvisioner>(sp => new CosmosDbProvisioner(sp.GetService<ICosmosClientFactory>(), sp.GetService<ILoggerFactory>()));
33+
options.UsePersistence(sp => new CosmosDbPersistenceProvider(sp.GetService<ICosmosClientFactory>(), databaseId, sp.GetService<ICosmosDbProvisioner>()));
3334
return options;
3435
}
3536
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using System;
2+
using Microsoft.Azure.Cosmos;
3+
using WorkflowCore.Providers.Azure.Interface;
4+
5+
namespace WorkflowCore.Providers.Azure.Services
6+
{
7+
public class CosmosClientFactory : ICosmosClientFactory, IDisposable
8+
{
9+
private bool isDisposed = false;
10+
11+
private CosmosClient _client;
12+
13+
public CosmosClientFactory(string connectionString)
14+
{
15+
_client = new CosmosClient(connectionString);
16+
}
17+
18+
public CosmosClient GetCosmosClient()
19+
{
20+
return this._client;
21+
}
22+
23+
/// <summary>
24+
/// Dispose of cosmos client
25+
/// </summary>
26+
public void Dispose()
27+
{
28+
this.Dispose(true);
29+
}
30+
31+
/// <summary>
32+
/// Dispose of cosmos client
33+
/// </summary>
34+
/// <param name="disposing">True if disposing</param>
35+
protected virtual void Dispose(bool disposing)
36+
{
37+
if (!this.isDisposed)
38+
{
39+
if (disposing)
40+
{
41+
this._client.Dispose();
42+
}
43+
44+
this.isDisposed = true;
45+
}
46+
}
47+
}
48+
}

src/providers/WorkflowCore.Providers.Azure/Services/CosmosDbPersistenceProvider.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ public class CosmosDbPersistenceProvider : IPersistenceProvider
1919

2020
private ICosmosDbProvisioner _provisioner;
2121
private string _dbId;
22-
private CosmosClient _client;
22+
private ICosmosClientFactory _clientFactory;
2323
private Lazy<Container> _workflowContainer;
2424
private Lazy<Container> _eventContainer;
2525
private Lazy<Container> _subscriptionContainer;
2626

27-
public CosmosDbPersistenceProvider(string connectionString, string dbId, ICosmosDbProvisioner provisioner)
27+
public CosmosDbPersistenceProvider(ICosmosClientFactory clientFactory, string dbId, ICosmosDbProvisioner provisioner)
2828
{
2929
_provisioner = provisioner;
3030
_dbId = dbId;
31-
_client = new CosmosClient(connectionString);
32-
_workflowContainer = new Lazy<Container>(() => _client.GetDatabase(_dbId).GetContainer(WorkflowContainerName));
33-
_eventContainer = new Lazy<Container>(() => _client.GetDatabase(_dbId).GetContainer(EventContainerName));
34-
_subscriptionContainer = new Lazy<Container>(() => _client.GetDatabase(_dbId).GetContainer(SubscriptionContainerName));
31+
_clientFactory = clientFactory;
32+
_workflowContainer = new Lazy<Container>(() => _clientFactory.GetCosmosClient().GetDatabase(_dbId).GetContainer(WorkflowContainerName));
33+
_eventContainer = new Lazy<Container>(() => _clientFactory.GetCosmosClient().GetDatabase(_dbId).GetContainer(EventContainerName));
34+
_subscriptionContainer = new Lazy<Container>(() => _clientFactory.GetCosmosClient().GetDatabase(_dbId).GetContainer(SubscriptionContainerName));
3535
}
3636

3737
public async Task ClearSubscriptionToken(string eventSubscriptionId, string token)
@@ -70,7 +70,7 @@ public async Task<string> CreateNewWorkflow(WorkflowInstance workflow)
7070

7171
public void EnsureStoreExists()
7272
{
73-
_provisioner.Provision(_dbId).Wait();
73+
_provisioner.Provision(_dbId).ConfigureAwait(false).GetAwaiter().GetResult();
7474
}
7575

7676
public async Task<Event> GetEvent(string id)

src/providers/WorkflowCore.Providers.Azure/Services/CosmosDbProvisioner.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ namespace WorkflowCore.Providers.Azure.Services
99
public class CosmosDbProvisioner : ICosmosDbProvisioner
1010
{
1111

12-
private CosmosClient _client;
12+
private ICosmosClientFactory _clientFactory;
1313

14-
public CosmosDbProvisioner(string connectionString, ILoggerFactory loggerFactory)
14+
public CosmosDbProvisioner(ICosmosClientFactory clientFactory, ILoggerFactory loggerFactory)
1515
{
16-
_client = new CosmosClient(connectionString);
16+
_clientFactory = clientFactory;
1717
}
1818

1919
public async Task Provision(string dbId)
2020
{
21-
var dbResp = await _client.CreateDatabaseIfNotExistsAsync(dbId);
21+
var dbResp = await _clientFactory.GetCosmosClient().CreateDatabaseIfNotExistsAsync(dbId);
2222
var wfIndexPolicy = new IndexingPolicy();
2323
wfIndexPolicy.IncludedPaths.Add(new IncludedPath { Path = @"/*" });
2424
wfIndexPolicy.ExcludedPaths.Add(new ExcludedPath { Path = @"/ExecutionPointers/?" });
@@ -32,6 +32,5 @@ public async Task Provision(string dbId)
3232
dbResp.Database.CreateContainerIfNotExistsAsync(new ContainerProperties(CosmosDbPersistenceProvider.SubscriptionContainerName, @"/id"))
3333
);
3434
}
35-
3635
}
37-
}
36+
}

src/providers/WorkflowCore.Providers.Azure/WorkflowCore.Providers.Azure.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
- Provides distributed lock management on Workflow Core
88
- Provides Queueing support on Workflow Core</Description>
99
<PackageTags>workflow workflowcore dlm</PackageTags>
10-
<Version>3.1.0</Version>
10+
<Version>3.2.0</Version>
1111
<PackageTargetFallback Condition=" '$(TargetFramework)' == 'netstandard1.3' ">$(PackageTargetFallback);dnxcore50</PackageTargetFallback>
1212
<PackageProjectUrl>https://github.com/danielgerlag/workflow-core</PackageProjectUrl>
1313
<PackageLicenseUrl>https://github.com/danielgerlag/workflow-core/blob/master/LICENSE.md</PackageLicenseUrl>
1414
<RepositoryType>git</RepositoryType>
1515
<RepositoryUrl>https://github.com/danielgerlag/workflow-core.git</RepositoryUrl>
1616
<Authors>Daniel Gerlag</Authors>
17-
<AssemblyVersion>3.1.0.0</AssemblyVersion>
18-
<FileVersion>3.1.0.0</FileVersion>
17+
<AssemblyVersion>3.2.0.0</AssemblyVersion>
18+
<FileVersion>3.2.0.0</FileVersion>
1919
</PropertyGroup>
2020

2121
<ItemGroup>

0 commit comments

Comments
 (0)