Skip to content

Latest commit

 

History

History
31 lines (25 loc) · 768 Bytes

README.md

File metadata and controls

31 lines (25 loc) · 768 Bytes

Global Lock

A simple, but reliable distributed lock which does not require a dedicated server

Installation

dotnet add package SynchronizationUtils.GlobalLock

Usage

// Register the service with the IoC container
servises.AddGlobalLock(storageConnectionString);

using var globalLock = serviceProvider.GetRequiredService<IGlobalLock>();
await using var lease = await globalLock.TryAcquire(resource, scope);

if (lease.IsAcquired)
{
    // Got exclusive access to the resource across all nodes/processes
    // Do something with it..
}
else
{
    // Hm, that's unfortunate - the resource is being owned by another process
    // Let's wait while it is available..

    await lease.Wait();

    // Got it, it is safe to proceed now..
}