Skip to content

Commit 109ca1d

Browse files
PSanetratg123
andauthored
Improve docs and naming of LeaderElector methods. (#1468)
* Improve docs and naming of `LeaderElector` methods. Also introduce `LeaderElector.RunAndTryToHoldLeadershipForeverAsync()` * fix build --------- Co-authored-by: Boshi Lian <farmer1992@gmail.com>
1 parent 1bae457 commit 109ca1d

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

src/KubernetesClient/LeaderElection/LeaderElector.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ public string GetLeader()
4949
return observedRecord?.HolderIdentity;
5050
}
5151

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

@@ -107,6 +112,32 @@ public async Task RunAsync(CancellationToken cancellationToken = default)
107112
}
108113
}
109114

115+
/// <summary>
116+
/// Tries to acquire leadership via a Kubernetes Lease resource.
117+
/// Will retry to acquire leadership again after leadership was lost.
118+
/// </summary>
119+
/// <returns>A Task which completes only on cancellation</returns>
120+
/// <param name="cancellationToken">A token to cancel the operation.</param>
121+
public async Task RunAndTryToHoldLeadershipForeverAsync(CancellationToken cancellationToken = default)
122+
{
123+
while (!cancellationToken.IsCancellationRequested)
124+
{
125+
await RunUntilLeadershipLostAsync(cancellationToken).ConfigureAwait(false);
126+
}
127+
}
128+
129+
/// <summary>
130+
/// Tries to acquire leadership once via a Kubernetes Lease resource.
131+
/// Will complete the returned Task and not retry to acquire leadership again after leadership is lost once.
132+
/// </summary>
133+
/// <seealso cref="RunUntilLeadershipLostAsync"/>
134+
/// <param name="cancellationToken">A token to cancel the operation.</param>
135+
[Obsolete("Replaced by RunUntilLeadershipLostAsync to encode behavior in method name.")]
136+
public Task RunAsync(CancellationToken cancellationToken = default)
137+
{
138+
return RunUntilLeadershipLostAsync(cancellationToken);
139+
}
140+
110141
private async Task<bool> TryAcquireOrRenew(CancellationToken cancellationToken)
111142
{
112143
var l = config.Lock;

tests/KubernetesClient.Tests/LeaderElection/LeaderElectionTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public void SimpleLeaderElection()
7171
countdown.Signal();
7272
};
7373

74-
leaderElector.RunAsync().Wait();
74+
leaderElector.RunUntilLeadershipLostAsync().Wait();
7575
});
7676

7777
countdown.Wait(TimeSpan.FromSeconds(10));
@@ -164,7 +164,7 @@ public void LeaderElection()
164164
lockAStopLeading.Set();
165165
};
166166

167-
leaderElector.RunAsync().Wait();
167+
leaderElector.RunUntilLeadershipLostAsync().Wait();
168168
});
169169

170170

@@ -186,7 +186,7 @@ public void LeaderElection()
186186
testLeaderElectionLatch.Signal();
187187
};
188188

189-
leaderElector.RunAsync().Wait();
189+
leaderElector.RunUntilLeadershipLostAsync().Wait();
190190
});
191191

192192
testLeaderElectionLatch.Wait(TimeSpan.FromSeconds(15));
@@ -272,7 +272,7 @@ public void LeaderElectionWithRenewDeadline()
272272
countdown.Signal();
273273
};
274274

275-
leaderElector.RunAsync().Wait();
275+
leaderElector.RunUntilLeadershipLostAsync().Wait();
276276
});
277277

278278
countdown.Wait(TimeSpan.FromSeconds(15));
@@ -305,7 +305,7 @@ public void LeaderElectionThrowException()
305305

306306
try
307307
{
308-
leaderElector.RunAsync().Wait();
308+
leaderElector.RunUntilLeadershipLostAsync().Wait();
309309
}
310310
catch (Exception e)
311311
{
@@ -362,7 +362,7 @@ public void LeaderElectionReportLeaderOnStart()
362362
countdown.Signal();
363363
};
364364

365-
Task.Run(() => leaderElector.RunAsync());
365+
Task.Run(() => leaderElector.RunUntilLeadershipLostAsync());
366366
countdown.Wait(TimeSpan.FromSeconds(10));
367367

368368
Assert.True(notifications.SequenceEqual(new[]
@@ -403,7 +403,7 @@ public void LeaderElectionShouldReportLeaderItAcquiresOnStart()
403403
countdown.Signal();
404404
};
405405

406-
Task.Run(() => leaderElector.RunAsync());
406+
Task.Run(() => leaderElector.RunUntilLeadershipLostAsync());
407407
countdown.Wait(TimeSpan.FromSeconds(10));
408408

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

0 commit comments

Comments
 (0)