🌱 use select with time.After instead of time.Sleep#13480
🌱 use select with time.After instead of time.Sleep#13480k8s-ci-robot merged 1 commit intokubernetes-sigs:mainfrom
Conversation
3fd8a4c to
67a4299
Compare
| return | ||
| } | ||
| time.Sleep(5 * time.Second) | ||
| case <-time.After(5 * time.Second): |
There was a problem hiding this comment.
Does using a 5 second time ticker? simplifies even more?
d37ba1b to
a20d4e1
Compare
| go func() { | ||
| for { | ||
| select { | ||
| case <-timeoutCtx.Done(): | ||
| return | ||
| default: | ||
| updatedDockerMachine := &infrav1.DockerMachine{} | ||
| if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil && | ||
| !updatedDockerMachine.DeletionTimestamp.IsZero() { | ||
| log.Info("Cancelling Bootstrap because the underlying machine has been deleted") | ||
| cancel() | ||
| return | ||
| } | ||
| time.Sleep(5 * time.Second) | ||
| case <-ticker.C: | ||
| } | ||
|
|
||
| updatedDockerMachine := &infrav1.DockerMachine{} | ||
| if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil && | ||
| !updatedDockerMachine.DeletionTimestamp.IsZero() { | ||
| log.Info("Cancelling Bootstrap because the underlying machine has been deleted") | ||
| cancel() | ||
| return | ||
| } | ||
| } | ||
| }() |
There was a problem hiding this comment.
I thought something like this,(may be wrong aswell)
go func() {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
for {
select {
case <-timeoutCtx.Done():
return
case <-ticker.C:
updatedDockerMachine := &infrav1.DockerMachine{}
err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine)
if err == nil && !updatedDockerMachine.DeletionTimestamp.IsZero() {
log.Info("Cancelling Bootstrap because the underlying machine has been deleted")
cancel()
return
}
}
}
}()
There was a problem hiding this comment.
Thanks for the suggestion! I moved the ticker creation inside the goroutine.
Since CAPI uses Go 1.25, tickers are automatically stopped by the garbage collector, so an explicit Stop() is not necessary. I intentionally omitted it to keep the code minimal.
I intentionally placed the logic after the select block rather than inside case <-ticker.C: to keep the nesting shallow and improve readability.
Signed-off-by: sivchari <shibuuuu5@gmail.com>
a20d4e1 to
0e46697
Compare
|
Thx! /approve /assign @Karthik-K-N |
|
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: sbueringer The full list of commands accepted by this bot can be found here. The pull request process is described here DetailsNeeds approval from an approver in each of these files:
Approvers can indicate their approval by writing |
|
/test pull-cluster-api-e2e-main-gke |
| go func() { | ||
| for { | ||
| select { | ||
| case <-timeoutCtx.Done(): | ||
| return | ||
| default: | ||
| updatedDockerMachine := &infrav1.DockerMachine{} | ||
| if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil && | ||
| !updatedDockerMachine.DeletionTimestamp.IsZero() { | ||
| log.Info("Cancelling Bootstrap because the underlying machine has been deleted") | ||
| cancel() | ||
| return | ||
| } | ||
| time.Sleep(5 * time.Second) | ||
| case <-ticker.C: | ||
| } | ||
|
|
||
| updatedDockerMachine := &infrav1.DockerMachine{} | ||
| if err := r.Get(ctx, client.ObjectKeyFromObject(dockerMachine), updatedDockerMachine); err == nil && | ||
| !updatedDockerMachine.DeletionTimestamp.IsZero() { | ||
| log.Info("Cancelling Bootstrap because the underlying machine has been deleted") | ||
| cancel() | ||
| return | ||
| } | ||
| } | ||
| }() |
|
LGTM label has been added. DetailsGit tree hash: 15a694c885bb441cce13eba35896d979699a09f4 |
What this PR does / why we need it:
use time.After instead of time.Sleep for receiving context cancellation while sleeping.
Which issue(s) this PR fixes (optional, in
fixes #<issue number>(, fixes #<issue_number>, ...)format, will close the issue(s) when PR gets merged):Fixes #