perf(MongoDB): reduce allocations and improve robustness in lock hot paths#276
Open
joesdu wants to merge 4 commits intomadelson:masterfrom
Open
perf(MongoDB): reduce allocations and improve robustness in lock hot paths#276joesdu wants to merge 4 commits intomadelson:masterfrom
joesdu wants to merge 4 commits intomadelson:masterfrom
Conversation
…paths - Cache IMongoCollection<T> reference in constructor instead of calling GetCollection on every TryAcquireAsync invocation - Promote immutable BsonDocument sub-expressions (expiredOrMissing, newFencingToken) to static readonly fields; cache newExpiresAt as an instance field (depends on expiry) to avoid rebuilding ~10 BsonDocument objects per busy-wait iteration - Move TTL index initialization to the start of TryAcquireAsync so it is triggered on every acquisition attempt, not only on success - Cache ownerFilter and renewUpdate in InnerHandle constructor; lease renewal (~every 10 s) no longer allocates new filter/update objects - Wrap ReleaseLockAsync in try-catch so network failures during Dispose do not surface unexpected exceptions to callers (TTL index provides eventual cleanup) - Add internal constructor accepting pre-parsed MongoDistributedLockOptions so MongoDistributedSynchronizationProvider can parse options once and reuse the result across all CreateLock calls - Fix stale comment "foo" → "expiresAt" in CheckIfIndexExists - Upgraded Microsoft.SourceLink.GitHub, Microsoft.Build.Tasks.Git, and Microsoft.SourceLink.Common to version 10.0.201 in DistributedLock.ZooKeeper, DistributedLock, and DistributedLockTaker. - Updated System.IO.Hashing to version 10.0.5 and added its dependencies. - Increased MongoDB.Bson and MongoDB.Driver versions to 3.7.1. - Updated System.Diagnostics.DiagnosticSource to version 10.0.5. - Adjusted DistributedLock.Oracle and DistributedLock.SqlServer versions to 1.0.5 and 1.0.7 respectively. - Made various transitive dependency updates to ensure compatibility and stability.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR optimizes the MongoDB-based distributed lock implementation to reduce per-acquire/renew allocations and improve operational robustness (TTL index creation timing, safer dispose behavior), while also updating several NuGet dependencies across the solution.
Changes:
- MongoDB lock hot-path optimizations: cache
IMongoCollection, reuse BSON sub-expressions, and cache renewal filter/update definitions. - Robustness adjustments: trigger TTL index initialization on every acquisition attempt; make release-on-dispose resilient to transient failures.
- Dependency refresh: bump MongoDB driver/BSON, SourceLink/build tooling, DiagnosticSource, System.IO.Hashing, and related transitive pins across projects.
Reviewed changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| src/DistributedLock.MongoDB/MongoDistributedLock.cs | Caches collection/expressions and reduces allocations in acquire/renew; adjusts TTL init timing; swallows release failures on dispose. |
| src/DistributedLock.MongoDB/MongoIndexInitializer.cs | Makes index-creation helper static and fixes TTL index existence check logic. |
| src/DistributedLock.MongoDB/MongoDistributedSynchronizationProvider.cs | Parses Mongo lock options once in provider and reuses them when creating locks. |
| src/Directory.Packages.props | Central version bumps (e.g., MongoDB.Driver, SourceLink, DiagnosticSource). |
| src/DistributedLock.MongoDB/packages.lock.json | Updates locked dependency graph for MongoDB package changes. |
| src/DistributedLock/packages.lock.json | Updates locked dependency graph for core package changes (SourceLink/System.IO.Hashing/etc.). |
| src/DistributedLock.Azure/packages.lock.json | Updates locked dependency graph for Azure package changes and transitive updates. |
| src/DistributedLock.Core/packages.lock.json | Updates locked dependency graph (notably SourceLink/System.IO.Hashing changes). |
| src/DistributedLock.FileSystem/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLock.MySql/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLock.Oracle/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLock.Postgres/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLock.Redis/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLock.SqlServer/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLock.Tests/packages.lock.json | Updates locked dependency graph for tests (now only contains net8.0 graph). |
| src/DistributedLock.WaitHandles/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLock.ZooKeeper/packages.lock.json | Updates locked dependency graph for SourceLink/System.IO.Hashing and transitive updates. |
| src/DistributedLockTaker/packages.lock.json | Updates locked dependency graph (MongoDB driver and other dependency pins). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…ockDocument>> for improved initialization
…goDistributedLock
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.