Cosmos.Threading 1.0.0

dotnet add package Cosmos.Threading --version 1.0.0
NuGet\Install-Package Cosmos.Threading -Version 1.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="Cosmos.Threading" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Cosmos.Threading --version 1.0.0
#r "nuget: Cosmos.Threading, 1.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 Cosmos.Threading as a Cake Addin
#addin nuget:?package=Cosmos.Threading&version=1.0.0

// Install Cosmos.Threading as a Cake Tool
#tool nuget:?package=Cosmos.Threading&version=1.0.0

Cosmos.Threading

Build Status

NuGet Badge

A library that contains a simple cosmos based distributed mutex. The mutex allows processes to coordinate access to a shared set of resources.

Installing Cosmos.Threading

Install the Cosmos.Threading package via nuget package manager console:

Install-Package Cosmos.Threading

Initializing Cosmos.Threading Mutex

Cosmos.Threading Configuration

The cosmos threading mutex requires some configuration options to be set. This can be read from a configuration section from the app configuration.

  "Cosmos.Threading.Mutex": {
    "DatabaseId": "mutex-database",
    "ContainerName": "mutex-container"
  }
Setting Description
DatabaseId This is the name of the cosmos database that the mutex support containers need to be set.
ContainerName The name of the container to which mutex items will be read/written

Cosmos.Threading Initialization

Create a container to store the mutex instances. The application has full control over the definition of the container.

await _cosmosClient
    .GetDatabase(_options.Value.DatabaseId)
    .CreateContainerIfNotExistsAsync(
    new ContainerProperties()
    {
        Id = _options.Value.ContainerName,
        PartitionKeyPath = MutexInitialization.PartitionKeyPath
    });

To create the support containers in the cosmos database the MutexInitialization class must be used to initialize the mutex in the container.

// Initialize the default mutex
await _mutexInitialization.InitializeAsync();

// Initialize a named mutex
await _mutexInitialization.InitializeAsync(NamedMutexName);

Acquire And Release Mutex

The AcquireAsync(string, TimeSpan) method acquires the mutex. The string argument is the name of the owner attempting to acquire the mutex. The TimeSpan argument is the upper limit of the acquisition time for the mutex. After the TimeSpan the mutex can be acquired by another owner. This timeout value is to prevent a deadlocked or terminated process from holding on to the mutex.

if (await _mutex.AcquireAsync(Environment.MachineName, TimeSpan.FromSeconds(10)))
{
    // Do some work and then release
    await _mutex.ReleaseAsync(Environment.MachineName);
}

The AcquireAsync(string, string TimeSpan) method acquires a named mutex. The first string argument is the name of the owner attempting to acquire the mutex. The second string argument is the name of the mutex. The TimeSpan argument is the upper limit of the acquisition time for the mutex. After the TimeSpan the mutex can be acquired by another owner. This timeout value is to prevent a deadlocked or terminated process from holding on to the mutex.

if (await _mutex.AcquireAsync(Environment.MachineName, NamedMutexName, TimeSpan.FromSeconds(10)))
{
    // Do some work and then release
    await _mutex.ReleaseAsync(Environment.MachineName, NamedMutexName);
}
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 71 6/20/2024

1.0.0 Initial Version