Skip to content

fix delay #543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 6 additions & 1 deletion src/KubernetesClient/LeaderElection/LeaderElector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ private async Task<bool> TryAcquireOrRenew(CancellationToken cancellationToken)

private async Task AcquireAsync(CancellationToken cancellationToken)
{
var delay = (int)config.RetryPeriod.TotalMilliseconds;
for (; ; )
{
try
{
var delay = config.RetryPeriod.Milliseconds;
var acq = TryAcquireOrRenew(cancellationToken);

if (await Task.WhenAny(acq, Task.Delay(delay, cancellationToken))
Expand All @@ -203,8 +203,13 @@ private async Task AcquireAsync(CancellationToken cancellationToken)
{
return;
}

// wait RetryPeriod since acq return immediately
await Task.Delay(delay, cancellationToken).ConfigureAwait(false);
}

// else timeout

delay = (int)(delay * JitterFactor);
}
finally
Expand Down
16 changes: 13 additions & 3 deletions tests/KubernetesClient.Tests/LeaderElection/LeaderElectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,9 @@ public void LeaderElection()

testLeaderElectionLatch.Wait(TimeSpan.FromSeconds(10));

Assert.Equal(7, electionHistory.Count);


Assert.True(electionHistory.SequenceEqual(
new[]
{
Expand Down Expand Up @@ -252,12 +255,19 @@ public void LeaderElectionWithRenewDeadline()

countdown.Wait(TimeSpan.FromSeconds(10));

Assert.Equal(9, electionHistory.Count);
// TODO flasky
// Assert.Equal(9, electionHistory.Count);

// Assert.True(electionHistory.SequenceEqual(new[]
// {
// "create record", "try update record", "update record", "try update record", "update record",
// "try update record", "try update record", "try update record", "try update record",
// }));

Assert.True(electionHistory.SequenceEqual(new[]
Assert.True(electionHistory.Take(7).SequenceEqual(new[]
{
"create record", "try update record", "update record", "try update record", "update record",
"try update record", "try update record", "try update record", "try update record",
"try update record", "try update record",
}));

Assert.True(leadershipHistory.SequenceEqual(new[] { "get leadership", "start leading", "stop leading" }));
Expand Down