DistributedLock.Mongo
2.0.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package DistributedLock.Mongo --version 2.0.0
NuGet\Install-Package DistributedLock.Mongo -Version 2.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DistributedLock.Mongo" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add DistributedLock.Mongo --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: DistributedLock.Mongo, 2.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install DistributedLock.Mongo as a Cake Addin #addin nuget:?package=DistributedLock.Mongo&version=2.0.0 // Install DistributedLock.Mongo as a Cake Tool #tool nuget:?package=DistributedLock.Mongo&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
mongo-lock
Exclusive distributed locking for .NET and MongoDB.
Usage
const string сonnectionString = "--mognodb connection string--";
MongoClient client = new MongoClient(сonnectionString);
IMongoDatabase database = client.GetDatabase("sample");
// Regular collection, not a capped!
IMongoCollection<LockAcquire<Guid>> locks = database.GetCollection<LockAcquire<Guid>>("locks");
// This collection should be a capped! https://docs.mongodb.com/manual/core/capped-collections/
// The size of the capped collection should be enough to put all active locks.
// One ReleaseSignal is about 32 bytes, so for 100,000 simultaneously locks,
// you need a capped collection size ~3 megabytes
IMongoCollection<ReleaseSignal> signals = database.GetCollection<ReleaseSignal>("signals");
Guid lockId = Guid.Parse("BF431614-4FB0-4489-84AA-D3EFEEF6BE7E");
// Guid lockId is a name of your distributed lock. You can also use string, int, etc.
MongoLock<Guid> mongoLock = new MongoLock<Guid>(locks, signals, lockId);
// Try to acquire exclusve lock. Lifetime for created lock is 30 secounds
IAcquire acquire = await mongoLock.AcquireAsync(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(10));
try
{
if (acquire.Acquired)
{
// critical section, it cannot be executed by more than one thread on any server at a time
// ...
// ...
}
else
{
// Timeout! Maybe another thread did not release the lock... We can try again or throw excepton
}
}
finally
{
// if (acquire.Acquired) no need to do it manually
await mongoLock.ReleaseAsync(acquire);
}
How it works?
- The lock is acquiring by the
lockId
. It can beGuid
,string
,int
or any supported by MongoDB.Driver type. - When you try to acquire the lock, the document with specifed lockId is added to the
locks
collection OR updates if it exists. - When the lock is released, the document is updated and a new document is added to the
signals
capped collection - When the lock is awaiting, the tailable cursor with server-side awaiting is used. Details: https://docs.mongodb.com/manual/reference/method/cursor.tailable/
Lifetime
is a period of time during which the lock is valid. After this time, the lock is "released" automatically and can be acquired again. It prevents deadlocks.- Do not use a long
timeout
, this can throw exception by MongoDB Driver. A normal timeout is no more than 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- MongoDB.Driver (>= 2.14.1)
-
.NETStandard 2.1
- MongoDB.Driver (>= 2.14.1)
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 | 22,365 | 5/5/2024 |
2.0.0 | 259,404 | 1/19/2022 |
2.0.0-beta | 570 | 1/19/2022 |
1.0.0 | 87,782 | 1/3/2019 |
* Changed Target Framework to netstandard2.0 and netstandard2.1
* MongoDB.Driver updated to 2.14.1