From b08d2a712ba9f8c0b4e35e9e8a42f50b7216e345 Mon Sep 17 00:00:00 2001 From: sakno Date: Fri, 19 Jul 2024 00:55:21 +0300 Subject: [PATCH] Added LeaseId property to expose fencing token --- src/DotNext.Tests/Threading/Leases/LeaseTests.cs | 3 ++- .../Threading/Leases/LeaseConsumer.cs | 14 ++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/DotNext.Tests/Threading/Leases/LeaseTests.cs b/src/DotNext.Tests/Threading/Leases/LeaseTests.cs index 5adf162db..786319e3d 100644 --- a/src/DotNext.Tests/Threading/Leases/LeaseTests.cs +++ b/src/DotNext.Tests/Threading/Leases/LeaseTests.cs @@ -90,7 +90,8 @@ public static async Task ConsumerTokenState() True(consumer.Token.IsCancellationRequested); True(consumer.Expiration.IsExpired); - True((await consumer.TryAcquireAsync())); + True(await consumer.TryAcquireAsync()); + Equal(1UL, consumer.LeaseId.Version); False(consumer.Token.IsCancellationRequested); False(consumer.Expiration.IsExpired); diff --git a/src/DotNext.Threading/Threading/Leases/LeaseConsumer.cs b/src/DotNext.Threading/Threading/Leases/LeaseConsumer.cs index c36a92a16..0f23bb4d6 100644 --- a/src/DotNext.Threading/Threading/Leases/LeaseConsumer.cs +++ b/src/DotNext.Threading/Threading/Leases/LeaseConsumer.cs @@ -78,6 +78,12 @@ private TimeSpan AdjustTimeToLive(TimeSpan originalTtl) /// Gets lease expiration timeout. /// public ref readonly Timeout Expiration => ref timeout; + + /// + /// Gets the lease version. + /// + /// The returned value can be used as a fencing token. + public LeaseIdentity LeaseId => identity; private ValueTask CancelAndStopTimerAsync() { @@ -181,7 +187,7 @@ public async Task ExecuteAsync(Func ExecuteAsync(Func Fork(Func> function, CancellationToken token) - => Task.Run(() => function(token), token); + static Task Fork(Func> function, CancellationToken token) + => Task.Run(() => function(token), token); + } /// /// Performs a call to across the application boundaries.