HostLoom.Locking 0.4.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package HostLoom.Locking --version 0.4.0
                    
NuGet\Install-Package HostLoom.Locking -Version 0.4.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="HostLoom.Locking" Version="0.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HostLoom.Locking" Version="0.4.0" />
                    
Directory.Packages.props
<PackageReference Include="HostLoom.Locking" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add HostLoom.Locking --version 0.4.0
                    
#r "nuget: HostLoom.Locking, 0.4.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.
#:package HostLoom.Locking@0.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=HostLoom.Locking&version=0.4.0
                    
Install as a Cake Addin
#tool nuget:?package=HostLoom.Locking&version=0.4.0
                    
Install as a Cake Tool

HostLoom.Locking

HostLoom.Locking is the backend-neutral distributed lock kernel: the consumer contract IDistributedLock, the backend contract ILockProvider, the composed DistributedLock that turns a provider into a lock with leases, retries, owner tokens, and lost-lease detection, and an in-process provider with the same state machine a distributed backend has. The package references only Microsoft.Extensions.Logging.Abstractions; every runtime type has a public constructor, so a lock composes without a container.

The lock is coordination, not correctness. It keeps two instances from doing the same work at the same time. It does not make persisted state correct: a lease can expire while an action is still running, and a provider can lose a lease it granted. Database transactions, row locks, unique constraints, and idempotency records own correctness. Design every action so that running it twice, or running it after the lease was lost, is safe.

var options = new LockingOptions { Namespace = "billing" };
IDistributedLock locks = new DistributedLock(options, new InMemoryLockProvider());

// Acquire, run, release in a finally. Contention past the retry policy throws
// LockNotAcquiredException; a backend failure throws LockProviderUnavailableException.
await locks.ExecuteWithLockAsync(
    $"invoice:{invoiceId}",
    async token => await SettleAsync(invoiceId, token),
    new LockOptions { Lease = TimeSpan.FromSeconds(10), OnLost = LostLeaseBehavior.Cancel },
    cancellationToken);

// Skip if busy: one attempt, no wait, null when another owner holds the key.
await using var handle = await locks.TryAcquireAsync("nightly-report", cancellationToken: cancellationToken);
if (handle is not null)
{
    await RunReportAsync(handle.LostToken);
}

Keys are opaque strings without whitespace or control characters. The composed lock prefixes them as {namespace}:lock:{key} before they reach a provider, so consumers never repeat the prefix. Build a key from a credential with LockKey.FromSensitive, which hashes the value so it never reaches the provider, a log line, or a span.

Every lease has an owner token generated per acquisition; release and extension succeed only for the owner. The handle's LostToken is cancelled, IsHeld turns false, and hostloom.lock.lost increments when the lease expires on the local clock or the provider refuses an extend or a release. With LockOptions.OnLost = LostLeaseBehavior.Cancel the token handed to the action is cancelled too; the default, Observe, keeps the action running and only reports the loss.

LockRetryPolicy shapes the wait between attempts and never depends on HostLoom.Pipelines. The default reproduces the platform's historical behaviour: ten retries at a linear 50 ms step with up to 50 ms of additive jitter, about 3 s in total; LockOptions.MaxWait is a hard wall-clock bound on top of it, and TimeSpan.Zero makes exactly one attempt. LockingOptions.Enabled = false is single-instance mode: a startup warning, hostloom.lock.enabled = 0, and every action running immediately.

LockingProbe.Describe(lock) reports the composition without executing anything. Metrics and activities live under the HostLoom.Locking meter and activity source. Install HostLoom.Locking.DependencyInjection to register the lock with the built-in .NET container.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (6)

Showing the top 5 NuGet packages that depend on HostLoom.Locking:

Package Downloads
HostLoom.Locking.DependencyInjection

Microsoft.Extensions.DependencyInjection registration, options validation, and readiness for HostLoom locking.

HostLoom.Locking.Testing

Container-free lock composition, scripted, recording, and fault-injecting provider decorators for testing HostLoom locking consumers.

HostLoom.Locking.Pipelines

Distributed-lock filter for HostLoom pipelines, built on the HostLoom locking kernel.

HostLoom.Leadership

Lease-based leader election for HostLoom over the distributed lock: one elector per role, explicit renewal, a leadership token, and the honest guarantee statement.

HostLoom.Scheduling.Locking

Runs exclusive HostLoom schedules on one instance at a time by claiming them through the HostLoom distributed lock.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.17.0 0 10/25/2026
0.16.1 117 10/22/2026
0.16.0 267 10/14/2026
0.15.1 193 10/12/2026
0.15.0 184 10/4/2026
0.14.0 171 10/4/2026
0.13.0 190 9/28/2026
0.12.0 169 9/27/2026
0.11.0 169 9/25/2026
0.10.0 178 9/22/2026
0.9.0 172 9/22/2026
0.8.0 141 9/19/2026
0.7.0 140 9/17/2026
0.6.0 160 9/6/2026
0.5.0 145 9/5/2026
0.4.0 148 9/3/2026