Skip to content

Commit

Permalink
Improved LeaseUpdater efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
stidsborg committed Jan 24, 2025
1 parent 0b131c5 commit 9ed3b88
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions Core/Cleipnir.ResilientFunctions/CoreRuntime/LeaseUpdater.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Cleipnir.ResilientFunctions.Domain;
using Cleipnir.ResilientFunctions.Domain.Exceptions;
Expand All @@ -19,6 +19,7 @@ internal class LeaseUpdater : IDisposable
private readonly IFunctionStore _functionStore;
private readonly UnhandledExceptionHandler _unhandledExceptionHandler;
private volatile bool _disposed;
private readonly CancellationTokenSource _cts = new();

private LeaseUpdater(
FlowId flowId,
Expand Down Expand Up @@ -68,7 +69,7 @@ private async Task Start()
{
try
{
await Task.Delay(_leaseLength / 2);
await Task.Delay(_leaseLength / 2, _cts.Token);

if (_disposed) break;

Expand All @@ -84,6 +85,10 @@ private async Task Start()
_unhandledExceptionHandler.Invoke(UnexpectedStateException.LeaseUpdateFailed(_flowId));
}
}
catch (TaskCanceledException)
{
_disposed = true;
}
catch (Exception e)
{
_disposed = true;
Expand All @@ -97,8 +102,20 @@ private async Task Start()
}
}

RemoveFromLeaseUpdaters();
}

private void RemoveFromLeaseUpdaters()
{
#if DEBUG
_leaseUpdaters?.Remove(_storedId);
#endif
}

public void Dispose() => _disposed = true;
public void Dispose()
{
RemoveFromLeaseUpdaters();
_disposed = true;
_cts.Cancel();
}
}

0 comments on commit 9ed3b88

Please sign in to comment.