Skip to content

youscan/AzureMutex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Distributed mutex based on Azure Blobs

CI NuGet

.Net implementation of a distributed lock on top of Azure Blobs.

Diclaimer: if you think you need it, you might doing it wrong :)

Jokes aside, we do use it for some cases. Typical example involves a process running on several nodes (for redundancy). E.g. periodic sync with a 3rd party system, a database migration, etc.

Recommended usage

Recommended usage is via an extension method that takes a long-running task as a parameter and ensures it runs as a single instance (in a single process):

var mutex = new BlobMutex("storage-account-connection", "mutex-container", "blob-name");

// 1. Blocks until lock is held or `CancellationToken` cancelled
// 2. Executes `LongRunningTask` while periodically renewing the lock taken. 
// 3. Runs either until operation is finished or `CancellationToken` canceled
// 4. Terminates operation in case the lock is lost (e.g. because of connectivity issues)
//    with a `LeaseLostException`
await mutex.RunSingleInstance(LongRunningTask, CancellationToken.None); 

async Task LongRunningTask(CancellationToken cancellation) {
    do {
        await Task.Delay(TimeSpan.FromMinutes(10), cancellation);
    }
    while (!cancellation.IsCancellationRequested)
}

References