madelson / DistributedLock

A .NET library for distributed synchronization
MIT License
1.75k stars 182 forks source link

About Distribute Locking System using Redis #169

Open KandarpPatel07 opened 9 months ago

KandarpPatel07 commented 9 months ago

Hi I have one doubt regarding the use of the library in order to implement the feature of a distributed locking system like there is one scenario Let's say service-1 occupies a lock on resource-1 and during handling the process it goes in an infinite loop and never returns hence the lock that is acquired is never released is there any mechanism that the key that is set in the redis gets timed out and automatically delete

madelson commented 9 months ago

Hi @KandarpPatel07 thanks for your interest in the library. Currently, Redis locks do operate on a timeout but they have "auto-renewal" set up such that the process holding the lock will periodically re-up the timeout if the lock has not been released. Typically, you don't want the process holding the lock to silently lose it just because an operation took longer than expected.

That said, if you want to mandate an absolute timeout for the lock hold, today you can do so like this:

await using (var handle = await myDistributedLock.AcquireAsync())
{
       // force-release the lock after 10 minutes
       Task.Delay(TimeSpan.FromMinutes(10)).ContinueWith(_ => handle.Dispose());

       // potential infinite loop here
}

If there was sufficient interest, potentially this could be built directly into the library as a new option.

madelson commented 3 months ago

Related (but different) ask: https://github.com/madelson/DistributedLock/issues/130