Skip to content

Commit 10658fe

Browse files
authored
CSHARP-3431: Make MongoClient IDisposable (#1461)
1 parent 2086b5a commit 10658fe

File tree

78 files changed

+598
-602
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+598
-602
lines changed

benchmarks/MongoDB.Driver.Benchmarks/BenchmarkHelper.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Linq;
2121
using MongoDB.Bson;
2222
using MongoDB.Driver;
23-
using MongoDB.Driver.TestHelpers;
2423

2524
namespace MongoDB.Benchmarks
2625
{
@@ -79,13 +78,16 @@ public static class MongoConfiguration
7978
public const string PerfTestDatabaseName = "perftest";
8079
public const string PerfTestCollectionName = "corpus";
8180

82-
public static DisposableMongoClient CreateDisposableClient()
81+
public static IMongoClient CreateClient()
8382
{
8483
var mongoUri = Environment.GetEnvironmentVariable("MONGODB_URI");
85-
var client = mongoUri != null ? new MongoClient(mongoUri) : new MongoClient();
84+
var settings = mongoUri != null ? MongoClientSettings.FromConnectionString(mongoUri) : new();
85+
settings.ClusterSource = DisposingClusterSource.Instance;
86+
87+
var client = new MongoClient(settings);
8688
client.DropDatabase(PerfTestDatabaseName);
8789

88-
return new DisposableMongoClient(client, null);
90+
return client;
8991
}
9092
}
9193
}

benchmarks/MongoDB.Driver.Benchmarks/LibmongocryptBindingBenchmark.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
using MongoDB.Bson.TestHelpers;
2222
using MongoDB.Driver;
2323
using MongoDB.Driver.Encryption;
24-
using MongoDB.Driver.TestHelpers;
2524

2625
namespace MongoDB.Benchmarks
2726
{
@@ -31,7 +30,7 @@ public class LibmongocryptBindingBenchmark
3130
private const string LocalMasterKey = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk";
3231

3332
private byte[] _encryptedValuesDocumentBytes;
34-
private DisposableMongoClient _disposableKeyVaultClient;
33+
private IMongoClient _disposableKeyVaultClient;
3534
private IAutoEncryptionLibMongoCryptController _libMongoCryptController;
3635

3736
[Params(1)]
@@ -56,8 +55,9 @@ public void Setup()
5655

5756
var clientSettings = MongoClientSettings.FromConnectionString("mongodb://localhost");
5857
clientSettings.AutoEncryptionOptions = autoEncryptionOptions;
58+
clientSettings.ClusterSource = DisposingClusterSource.Instance;
5959

60-
_disposableKeyVaultClient = new DisposableMongoClient(new MongoClient(clientSettings), null);
60+
_disposableKeyVaultClient = new MongoClient(clientSettings);
6161

6262
var keyVaultDatabase = _disposableKeyVaultClient.GetDatabase(keyVaultNamespace.DatabaseNamespace.DatabaseName);
6363
keyVaultDatabase.DropCollection(keyVaultNamespace.CollectionName);

benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/FindManyBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace MongoDB.Benchmarks.MultiDoc
2626
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
2727
public class FindManyBenchmark
2828
{
29-
private DisposableMongoClient _client;
29+
private IMongoClient _client;
3030
private IMongoCollection<BsonDocument> _collection;
3131
private BsonDocument _tweetDocument;
3232

@@ -36,7 +36,7 @@ public class FindManyBenchmark
3636
[GlobalSetup]
3737
public void Setup()
3838
{
39-
_client = MongoConfiguration.CreateDisposableClient();
39+
_client = MongoConfiguration.CreateClient();
4040
_tweetDocument = ReadExtendedJson("single_and_multi_document/tweet.json");
4141
_collection = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName).GetCollection<BsonDocument>(MongoConfiguration.PerfTestCollectionName);
4242

benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSDownloadBenchmark.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
using System.IO;
1717
using BenchmarkDotNet.Attributes;
1818
using MongoDB.Bson;
19+
using MongoDB.Driver;
1920
using MongoDB.Driver.GridFS;
20-
using MongoDB.Driver.TestHelpers;
2121
using static MongoDB.Benchmarks.BenchmarkHelper;
2222

2323
namespace MongoDB.Benchmarks.MultiDoc
@@ -26,7 +26,7 @@ namespace MongoDB.Benchmarks.MultiDoc
2626
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
2727
public class GridFsDownloadBenchmark
2828
{
29-
private DisposableMongoClient _client;
29+
private IMongoClient _client;
3030
private ObjectId _fileId;
3131
private GridFSBucket _gridFsBucket;
3232

@@ -36,7 +36,7 @@ public class GridFsDownloadBenchmark
3636
[GlobalSetup]
3737
public void Setup()
3838
{
39-
_client = MongoConfiguration.CreateDisposableClient();
39+
_client = MongoConfiguration.CreateClient();
4040
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
4141
_fileId = _gridFsBucket.UploadFromStream("gridfstest", File.OpenRead($"{DataFolderPath}single_and_multi_document/gridfs_large.bin"));
4242
}

benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/GridFSUploadBenchmark.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
using System.IO;
1717
using BenchmarkDotNet.Attributes;
18+
using MongoDB.Driver;
1819
using MongoDB.Driver.GridFS;
19-
using MongoDB.Driver.TestHelpers;
2020
using static MongoDB.Benchmarks.BenchmarkHelper;
2121

2222
namespace MongoDB.Benchmarks.MultiDoc
@@ -25,7 +25,7 @@ namespace MongoDB.Benchmarks.MultiDoc
2525
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
2626
public class GridFsUploadBenchmark
2727
{
28-
private DisposableMongoClient _client;
28+
private IMongoClient _client;
2929
private byte[] _fileBytes;
3030
private GridFSBucket _gridFsBucket;
3131

@@ -35,7 +35,7 @@ public class GridFsUploadBenchmark
3535
[GlobalSetup]
3636
public void Setup()
3737
{
38-
_client = MongoConfiguration.CreateDisposableClient();
38+
_client = MongoConfiguration.CreateClient();
3939
_fileBytes = File.ReadAllBytes($"{DataFolderPath}single_and_multi_document/gridfs_large.bin");
4040
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
4141
}

benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/InsertManyLargeBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.MultiDoc
2727
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
2828
public class InsertManyLargeBenchmark
2929
{
30-
private DisposableMongoClient _client;
30+
private IMongoClient _client;
3131
private IMongoCollection<BsonDocument> _collection;
3232
private IMongoDatabase _database;
3333
private IEnumerable<BsonDocument> _largeDocuments;
@@ -38,7 +38,7 @@ public class InsertManyLargeBenchmark
3838
[GlobalSetup]
3939
public void Setup()
4040
{
41-
_client = MongoConfiguration.CreateDisposableClient();
41+
_client = MongoConfiguration.CreateClient();
4242
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);
4343

4444
var largeDocument = ReadExtendedJson("single_and_multi_document/large_doc.json");

benchmarks/MongoDB.Driver.Benchmarks/MultiDoc/InsertManySmallBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.MultiDoc
2727
[BenchmarkCategory(DriverBenchmarkCategory.MultiBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
2828
public class InsertManySmallBenchmark
2929
{
30-
private DisposableMongoClient _client;
30+
private IMongoClient _client;
3131
private IMongoCollection<BsonDocument> _collection;
3232
private IMongoDatabase _database;
3333
private IEnumerable<BsonDocument> _smallDocuments;
@@ -38,7 +38,7 @@ public class InsertManySmallBenchmark
3838
[GlobalSetup]
3939
public void Setup()
4040
{
41-
_client = MongoConfiguration.CreateDisposableClient();
41+
_client = MongoConfiguration.CreateClient();
4242
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);
4343

4444
var smallDocument = ReadExtendedJson("single_and_multi_document/small_doc.json");

benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileDownloadBenchmark.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
using System.IO;
1818
using BenchmarkDotNet.Attributes;
1919
using MongoDB.Bson.TestHelpers;
20+
using MongoDB.Driver;
2021
using MongoDB.Driver.GridFS;
21-
using MongoDB.Driver.TestHelpers;
2222
using static MongoDB.Benchmarks.BenchmarkHelper;
2323

2424
namespace MongoDB.Benchmarks.ParallelBench
@@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.ParallelBench
2727
[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
2828
public class GridFSMultiFileDownloadBenchmark
2929
{
30-
private DisposableMongoClient _client;
30+
private IMongoClient _client;
3131
private GridFSBucket _gridFsBucket;
3232
private string _tmpDirectoryPath;
3333
private ConcurrentQueue<(string, int)> _filesToDownload;
@@ -38,7 +38,7 @@ public class GridFSMultiFileDownloadBenchmark
3838
[GlobalSetup]
3939
public void Setup()
4040
{
41-
_client = MongoConfiguration.CreateDisposableClient();
41+
_client = MongoConfiguration.CreateClient();
4242
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
4343
_gridFsBucket.Drop();
4444
_tmpDirectoryPath = $"{DataFolderPath}parallel/tmpGridFS";

benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/GridFSMultiFileUploadBenchmark.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
using System.IO;
1818
using BenchmarkDotNet.Attributes;
1919
using MongoDB.Bson.TestHelpers;
20+
using MongoDB.Driver;
2021
using MongoDB.Driver.GridFS;
21-
using MongoDB.Driver.TestHelpers;
2222
using static MongoDB.Benchmarks.BenchmarkHelper;
2323

2424
namespace MongoDB.Benchmarks.ParallelBench
@@ -27,7 +27,7 @@ namespace MongoDB.Benchmarks.ParallelBench
2727
[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.WriteBench, DriverBenchmarkCategory.DriverBench)]
2828
public class GridFSMultiFileUploadBenchmark
2929
{
30-
private DisposableMongoClient _client;
30+
private IMongoClient _client;
3131
private GridFSBucket _gridFsBucket;
3232
private ConcurrentQueue<(string, int)> _filesToUpload;
3333

@@ -37,7 +37,7 @@ public class GridFSMultiFileUploadBenchmark
3737
[GlobalSetup]
3838
public void Setup()
3939
{
40-
_client = MongoConfiguration.CreateDisposableClient();
40+
_client = MongoConfiguration.CreateClient();
4141
_gridFsBucket = new GridFSBucket(_client.GetDatabase(MongoConfiguration.PerfTestDatabaseName));
4242
_filesToUpload = new ConcurrentQueue<(string, int)>();
4343
}

benchmarks/MongoDB.Driver.Benchmarks/ParallelBench/MultiFileExportBenchmark.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace MongoDB.Benchmarks.ParallelBench
3030
[BenchmarkCategory(DriverBenchmarkCategory.ParallelBench, DriverBenchmarkCategory.ReadBench, DriverBenchmarkCategory.DriverBench)]
3131
public class MultiFileExportBenchmark
3232
{
33-
private DisposableMongoClient _client;
33+
private IMongoClient _client;
3434
private IMongoCollection<BsonDocument> _collection;
3535
private IMongoDatabase _database;
3636
private string _tmpDirectoryPath;
@@ -42,7 +42,7 @@ public class MultiFileExportBenchmark
4242
[GlobalSetup]
4343
public void Setup()
4444
{
45-
_client = MongoConfiguration.CreateDisposableClient();
45+
_client = MongoConfiguration.CreateClient();
4646
_database = _client.GetDatabase(MongoConfiguration.PerfTestDatabaseName);
4747
_collection = _database.GetCollection<BsonDocument>(MongoConfiguration.PerfTestCollectionName);
4848
_tmpDirectoryPath = $"{DataFolderPath}parallel/tmpLDJSON";

0 commit comments

Comments
 (0)