DistributedLock.Mongo 2.0.0-beta

This is a prerelease version of DistributedLock.Mongo.
There is a newer version of this package available.
See the version list below for details.
dotnet add package DistributedLock.Mongo --version 2.0.0-beta                
NuGet\Install-Package DistributedLock.Mongo -Version 2.0.0-beta                
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-beta" />                
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-beta                
#r "nuget: DistributedLock.Mongo, 2.0.0-beta"                
#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-beta&prerelease

// Install DistributedLock.Mongo as a Cake Tool
#tool nuget:?package=DistributedLock.Mongo&version=2.0.0-beta&prerelease                

mongo-lock

Exclusive distributed locking for .NET and MongoDB. It's available as Class Library (.NET Standart 1.5) on Nuget at https://www.nuget.org/packages/DistributedLock.Mongo/

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 be Guid, 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 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.

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

* Changed Target Framework to netstandard2.0 and netstandard2.1
     * MongoDB.Driver updated to 2.14.1