Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/KubernetesClient/LeaderElection/LeaderElector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ public string GetLeader()
return observedRecord?.HolderIdentity;
}

public async Task RunAsync(CancellationToken cancellationToken = default)
/// <summary>
/// Tries to acquire and hold leadership once via a Kubernetes Lease resource.
/// Will complete the returned Task and not retry to acquire leadership again after leadership is lost once.
/// </summary>
/// <param name="cancellationToken">A token to cancel the operation.</param>
public async Task RunUntilLeadershipLostAsync(CancellationToken cancellationToken = default)
{
await AcquireAsync(cancellationToken).ConfigureAwait(false);

Expand Down Expand Up @@ -107,6 +112,32 @@ public async Task RunAsync(CancellationToken cancellationToken = default)
}
}

/// <summary>
/// Tries to acquire leadership via a Kubernetes Lease resource.
/// Will retry to acquire leadership again after leadership was lost.
/// </summary>
/// <returns>A Task which completes only on cancellation</returns>
/// <param name="cancellationToken">A token to cancel the operation.</param>
public async Task RunAndTryToHoldLeadershipForeverAsync(CancellationToken cancellationToken = default)
{
while (!cancellationToken.IsCancellationRequested)
{
await RunUntilLeadershipLostAsync(cancellationToken);
}
}

/// <summary>
/// Tries to acquire leadership once via a Kubernetes Lease resource.
/// Will complete the returned Task and not retry to acquire leadership again after leadership is lost once.
/// </summary>
/// <seealso cref="RunUntilLeadershipLostAsync"/>
/// <param name="cancellationToken">A token to cancel the operation.</param>
[Obsolete("Replaced by RunUntilLeadershipLostAsync to encode behavior in method name.")]
public Task RunAsync(CancellationToken cancellationToken = default)
{
return RunUntilLeadershipLostAsync(cancellationToken);
}

private async Task<bool> TryAcquireOrRenew(CancellationToken cancellationToken)
{
var l = config.Lock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void SimpleLeaderElection()
countdown.Signal();
};

leaderElector.RunAsync().Wait();
leaderElector.RunUntilLeadershipLostAsync().Wait();
});

countdown.Wait(TimeSpan.FromSeconds(10));
Expand Down Expand Up @@ -164,7 +164,7 @@ public void LeaderElection()
lockAStopLeading.Set();
};

leaderElector.RunAsync().Wait();
leaderElector.RunUntilLeadershipLostAsync().Wait();
});


Expand All @@ -186,7 +186,7 @@ public void LeaderElection()
testLeaderElectionLatch.Signal();
};

leaderElector.RunAsync().Wait();
leaderElector.RunUntilLeadershipLostAsync().Wait();
});

testLeaderElectionLatch.Wait(TimeSpan.FromSeconds(15));
Expand Down Expand Up @@ -272,7 +272,7 @@ public void LeaderElectionWithRenewDeadline()
countdown.Signal();
};

leaderElector.RunAsync().Wait();
leaderElector.RunUntilLeadershipLostAsync().Wait();
});

countdown.Wait(TimeSpan.FromSeconds(15));
Expand Down Expand Up @@ -305,7 +305,7 @@ public void LeaderElectionThrowException()

try
{
leaderElector.RunAsync().Wait();
leaderElector.RunUntilLeadershipLostAsync().Wait();
}
catch (Exception e)
{
Expand Down Expand Up @@ -362,7 +362,7 @@ public void LeaderElectionReportLeaderOnStart()
countdown.Signal();
};

Task.Run(() => leaderElector.RunAsync());
Task.Run(() => leaderElector.RunUntilLeadershipLostAsync());
countdown.Wait(TimeSpan.FromSeconds(10));

Assert.True(notifications.SequenceEqual(new[]
Expand Down Expand Up @@ -403,7 +403,7 @@ public void LeaderElectionShouldReportLeaderItAcquiresOnStart()
countdown.Signal();
};

Task.Run(() => leaderElector.RunAsync());
Task.Run(() => leaderElector.RunUntilLeadershipLostAsync());
countdown.Wait(TimeSpan.FromSeconds(10));

Assert.True(notifications.SequenceEqual(new[] { "foo1" }));
Expand Down