Skip to content

Conversation

@joesdu
Copy link

@joesdu joesdu commented Oct 21, 2025

Added the DistributedLock.MongoDB project, providing a MongoDB-based distributed lock implementation that supports both synchronous and asynchronous lock operations. Added core classes MongoDistributedLock, MongoDistributedLockHandle, MongoDistributedSynchronizationOptionsBuilder, and MongoDistributedSynchronizationProvider to implement lock acquisition, release, automatic renewal, and configuration functions.

Updated project configuration, added dependency on MongoDB.Driver, and supported multiple target frameworks (.NET 8, .NET Standard 2.1, .NET Framework 4.7.2). Added usage instructions and code examples in README.md.

Added comprehensive test coverage, including basic functionality, competition scenarios, lock loss handling, and support for custom collection names, ensuring the reliability of the functionality. Updated dependency versions and optimized internal access configuration for the test project.

Added the DistributedLock.MongoDB project, providing a MongoDB-based distributed lock implementation that supports both synchronous and asynchronous lock operations. Added core classes `MongoDistributedLock`, `MongoDistributedLockHandle`, `MongoDistributedSynchronizationOptionsBuilder`, and `MongoDistributedSynchronizationProvider` to implement lock acquisition, release, automatic renewal, and configuration functions.

Updated project configuration, added dependency on `MongoDB.Driver`, and supported multiple target frameworks (.NET 8, .NET Standard 2.1, .NET Framework 4.7.2). Added usage instructions and code examples in `README.md`.

Added comprehensive test coverage, including basic functionality, competition scenarios, lock loss handling, and support for custom collection names, ensuring the reliability of the functionality. Updated dependency versions and optimized internal access configuration for the test project.
Copilot AI review requested due to automatic review settings October 21, 2025 09:36
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds MongoDB-based distributed lock support to the DistributedLock library. It introduces a new DistributedLock.MongoDB project that implements distributed locking using MongoDB as the backing store, supporting both synchronous and asynchronous operations with automatic lock renewal.

Key changes:

  • New MongoDB lock implementation with automatic extension and expiry mechanisms
  • Support for multiple target frameworks (.NET 8, .NET Standard 2.1, .NET Framework 4.7.2)
  • Comprehensive test coverage including contention scenarios and lock loss handling

Reviewed Changes

Copilot reviewed 26 out of 27 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/DistributedLock.MongoDB/MongoDistributedLock.cs Core lock implementation with acquisition logic and MongoDB operations
src/DistributedLock.MongoDB/MongoDistributedLockHandle.cs Handle class managing lock lifecycle, extension, and release
src/DistributedLock.MongoDB/MongoDistributedSynchronizationProvider.cs Provider implementation for creating MongoDB locks
src/DistributedLock.MongoDB/MongoDistributedSynchronizationOptionsBuilder.cs Options builder for configuring lock behavior (expiry, extension cadence, busy wait)
src/DistributedLock.MongoDB/MongoLockDocument.cs MongoDB document model for storing lock state
src/DistributedLock.MongoDB/README.md Usage documentation and examples
src/DistributedLock.Tests/Tests/MongoDB/* Test suite covering basic functionality, contention, and edge cases
src/Directory.Packages.props Added MongoDB.Driver package dependency
src/DistributedLock.sln Added MongoDB project to solution
src/DistributedLock/packages.lock.json Updated with MongoDB dependencies (DnsClient, SharpCompress, Snappier, ZstdSharp.Port)

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

将字段和构造函数的访问修饰符从 internal 修改为 private,以增强封装性。修复注释中的拼写错误并优化格式,提升代码的可读性和维护性。
在 `AssemblyAttributes.cs` 文件中,新增了对 `DistributedLock.MongoDB` 的 `InternalsVisibleTo` 特性声明。
此更改允许 `DistributedLock.MongoDB` 程序集访问当前程序集的内部成员。
其他现有的 `InternalsVisibleTo` 特性未受影响。
在 `PublicAPI.Unshipped.txt` 文件中,删除了 `Medallion.Threading.MongoDB.MongoDistributedLock+#nullable enable` 行。这意味着不再启用该文件中的可空引用类型功能,可能会影响到代码的可空性检查和相关的编译器警告。
@joesdu joesdu changed the title feat: Added support for MongoDB distributed locks feat: Add MongoDB distributed locks Implementation Oct 22, 2025
在 `MongoDistributedLock.IDistributedLock.cs` 文件中,`MongoDistributedLock` 类实现了 `IDistributedLock` 接口的 `TryAcquire` 方法,新增了自动生成的方法以支持分布式锁的获取。此外,更新了文档注释,修正了异步获取锁的代码示例格式。
@madelson
Copy link
Owner

Let me know when you're ready for me to take a look @joesdu !

@joesdu
Copy link
Author

joesdu commented Oct 28, 2025

Let me know when you're ready for me to take a look @joesdu !

Yes, it is ready now.

joesdu and others added 6 commits November 9, 2025 14:44
This update introduces fencing tokens for MongoDB distributed locks. The token increments automatically upon successful lock acquisition and is exposed to the caller, facilitating idempotency and preventing concurrency issues caused by lock loss. A new UseAdaptiveBackoff configuration is added to support adaptive exponential backoff, improving performance and MongoDB's load handling in high-concurrency scenarios. The auto-renewal mechanism is more robust, supporting retries when encountering a MongoException. Distributed tracing diagnostics are integrated for .NET 8.0+. Compatibility and dependencies are updated to support net10.0.
joesdu added 4 commits January 7, 2026 11:22
将MongoDistributedLock构造函数中默认的锁集合名称从"distributedLocks"修改为"distributed.locks",以统一集合命名规范。此更改会影响新建锁时的默认存储集合。
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 37 out of 37 changed files in this pull request and generated 5 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@madelson
Copy link
Owner

madelson commented Jan 9, 2026

This looks great @joesdu ! I took a pass through and added my comments. When you've had a chance to go through those let me know and I'll start pushing this along towards a release. This weekend I hope to have time to test out the code directly.

joesdu added 2 commits January 9, 2026 19:44
…ion, and Robustness Enhancement

- Documents are supplemented with instructions for MongoDB installation and Docker startup, setting the default collection name to distributed.locks
- Lock key adds UTF-8 length validation, throws exceptions for limits exceeded, preventing index overflow
- Adaptive backoff logic is removed, simplifying the retry mechanism
- Optimization of index initialization logic, enhancing idempotency and thread safety
- Adjustment of default values for MongoLockDocument fields, with more comprehensive comments
- Restructuring of MongoDistributedLockHandle, supporting synchronous/asynchronous release, and more robust resource management
- Addition of unit tests related to key validation, removal of GetSafeName tests, and retention of case sensitivity tests
- Standardize code style and comments to improve maintainability
@joesdu
Copy link
Author

joesdu commented Jan 9, 2026

This looks great @joesdu ! I took a pass through and added my comments. When you've had a chance to go through those let me know and I'll start pushing this along towards a release. This weekend I hope to have time to test out the code directly.

I have already adjusted and responded to all the comments, and the code has been submitted. Thank you for taking the time to discuss and test this implementation.

将IMongoDatabase.GetCollection的模拟调用由单参数(collName)改为双参数(collName, null),以显式匹配MongoDB驱动的重载方法签名,确保测试时参数一致性,避免因参数不匹配导致的测试失败。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants