madelson / DistributedLock

A .NET library for distributed synchronization
MIT License
1.86k stars 192 forks source link

Use Distributed lock to manage multiple calls to the same Azure function #166

Closed Protheon closed 1 year ago

Protheon commented 1 year ago

Hi,

Suppose there is an azure function that is called multiple times and we want to ensure that these calls use a shared resource that is allocated only on the first call.

Is this package appropriate to handle concurrency between these instances so that the first call creates the resource and subsequent invocations use that resource, or are there any caveats?

RG

madelson commented 1 year ago

@Protheon I'm not sure I exactly follow, but that sounds like the kind of thing this library is for. For example let's say you have code like this that is being called by multiple threads/processes/machines at one time:

using (await myDistributedLock.AcquireAsync())
{
    if (resource not initialized) { InitializeResource(); }
}
UseResource();

This will ensure that the resource only gets initialized once.

Protheon commented 1 year ago

tyvm. i'm giving it a try!