DistributedLock.Mongo
2.1.0
dotnet add package DistributedLock.Mongo --version 2.1.0
NuGet\Install-Package DistributedLock.Mongo -Version 2.1.0
<PackageReference Include="DistributedLock.Mongo" Version="2.1.0" />
paket add DistributedLock.Mongo --version 2.1.0
#r "nuget: DistributedLock.Mongo, 2.1.0"
// Install DistributedLock.Mongo as a Cake Addin #addin nuget:?package=DistributedLock.Mongo&version=2.1.0 // Install DistributedLock.Mongo as a Cake Tool #tool nuget:?package=DistributedLock.Mongo&version=2.1.0
DistributedLock.Mongo: Exclusive Distributed Locking for .NET
DistributedLock.Mongo is a class library available on NuGet that enables exclusive distributed locking mechanisms for applications using .NET and MongoDB. It facilitates safe coordination of operations across different system components by managing access to shared resources. For more details or to download the library, visit the NuGet page.
Usage
using DistributedLock.Mongo;
using MongoDB.Driver;
using System;
const string connectionString = "mongodb://localhost:27017/"; // MongoDB connection string
var client = new MongoClient(connectionString);
var database = client.GetDatabase("sample-db");
// This is a regular collection, not a capped one.
var locks = database.GetCollection<LockAcquire<Guid>>("locks");
// Ensure this collection is capped. Refer to MongoDB's documentation on capped collections:
// https://docs.mongodb.com/manual/core/capped-collections/
// The capped collection size should be sufficient to accommodate all active locks. Each ReleaseSignal is estimated at 32 bytes,
// so approximately 3 megabytes will be required to handle up to 100,000 simultaneous locks.
var signals = database.GetCollection<ReleaseSignal>("signals");
// The 'lockId' variable serves as the identifier for your distributed lock. It can be a Guid, string, int, or any other suitable type
// depending on your application's requirements and the characteristics of the locks being implemented.
var lockId = Guid.Parse("BF431614-4FB0-4489-84AA-D3EFEEF6BE7E");
var mongoLock = new MongoLock<Guid>(locks, signals, lockId);
// Attempt to acquire an exclusive lock. The lifetime for the created lock is 30 seconds.
await using (var acquire = await mongoLock.AcquireAsync(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(10)))
{
if (acquire.Acquired)
{
// Critical section: This block cannot be executed by more than one thread on any server at a time.
// ...
// ...
}
else
{
// Timeout occurred! It's possible another thread has not released the lock yet.
// Consider retrying or handling this scenario with an exception.
}
}
// Alternatively, you can handle the lock using a try...finally block as shown below:
var anotherAcquire = await mongoLock.AcquireAsync(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(10));
try
{
// if (anotherAcquire.Acquired)
// { ... }
}
finally
{
// if (acquire.Acquired) no need to do it manually
await mongoLock.ReleaseAsync(anotherAcquire);
}
How the Distributed Lock Works
- Lock Identification: The lock is identified by
lockId
, which can be of types such asGuid
,string
,int
, or any other type supported by the MongoDB.Driver. - Acquisition: When attempting to acquire the lock, a document corresponding to the specified
lockId
is either added to thelocks
collection or updated if it already exists. - Release: Upon releasing the lock, the existing document in the
locks
collection is updated, and a new document is added to thesignals
collection, which is capped. - Lock Waiting: While waiting for the lock, a tailable cursor is used for server-side waiting. For more details, see MongoDB's tailable cursor documentation.
- Lifetime: The lifetime of the lock specifies the duration for which the lock remains valid. After this period, the lock is automatically considered "released" and becomes available for acquisition again. This feature helps prevent deadlocks.
- Timeout Caution: Avoid setting a long
timeout
as it can lead to exceptions thrown by the MongoDB Driver. Typically, a timeout should not exceed 1-2 minutes.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- MongoDB.Driver (>= 2.25.0)
-
.NETStandard 2.1
- MongoDB.Driver (>= 2.25.0)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on DistributedLock.Mongo:
Package | Downloads |
---|---|
Pipoburgos.SharedKernel.Infrastructure.Mongo
Mongo infrastructure implementations |
|
Maranics.EventSyncService.SDK.Persistence
Event Sync Service SDK persistence extensions |
|
Maranics.Notifications.SDK.Persistence
Notification Service SDK persistence extensions |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.1.0 | 21,057 | 5/5/2024 |
2.0.0 | 258,665 | 1/19/2022 |
2.0.0-beta | 570 | 1/19/2022 |
1.0.0 | 87,780 | 1/3/2019 |
* IAcquire now implements IAsyncDisposable
* MongoDB.Driver updated to 2.25.0