DistributedLock icon indicating copy to clipboard operation
DistributedLock copied to clipboard

Composite locks

Open moeen opened this issue 5 months ago • 3 comments

This pull request introduces new composite lock methods for various lock types via code generation.

Auto Generated Codes

  • src/DistributedLock.Core/DistributedLockProviderExtensions.cs
  • src/DistributedLock.Core/DistributedReaderWriterLockProviderExtensions.cs
  • src/DistributedLock.Core/DistributedSemaphoreProviderExtensions.cs
  • src/DistributedLock.Core/DistributedUpgradeableReaderWriterLockProviderExtensions.cs

moeen avatar Jul 07 '25 09:07 moeen

Is there a better alternative to this PR?

wuzxc1230123 avatar Dec 01 '25 10:12 wuzxc1230123

@madelson

Hi! Sorry for the delay - I was busy and totally forgot about this. I've now resolved all the comments and added the tests as discussed. The PR should be ready for another review. Thanks for your patience!

Sample use with the Redis Provider:

var connection = await ConnectionMultiplexer.ConnectAsync("localhost:6379");
var provider = new RedisDistributedSynchronizationProvider(connection.GetDatabase());

const string FromAccount = "account:alice";
const string ToAccount = "account:bob";
const decimal Amount = 100m;

var accounts = new[] { FromAccount, ToAccount };

await using (var handle = await provider.TryAcquireAllLocksAsync(accounts, TimeSpan.FromSeconds(30)))
{
    if (handle != null)
    {
        Console.WriteLine($"Transferring ${Amount} from {FromAccount} to {ToAccount}");
        await Task.Delay(1000);
        Console.WriteLine("Transfer completed successfully");
    }
    else
    {
        Console.WriteLine("Failed to acquire locks - transfer aborted");
    }
}

await connection.CloseAsync();

moeen avatar Dec 01 '25 12:12 moeen

@moeen Dear Boss, could you please merge the code and provide the nget feature? I really need this functionality—thank you very much!

wuzxc1230123 avatar Dec 02 '25 14:12 wuzxc1230123