Skip to content
This repository was archived by the owner on Sep 18, 2020. It is now read-only.

Commit 42aaba2

Browse files
committed
pkg/agent: Stop goroutines and sleeps via top-level stop channel
1 parent f59b327 commit 42aaba2

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

pkg/agent/agent.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ func (k *Klocksmith) process(stop <-chan struct{}) error {
104104
}
105105

106106
// watch update engine for status updates
107-
watchUpdateStatusStop := make(chan struct{})
108-
go k.watchUpdateStatus(k.updateStatusCallback, watchUpdateStatusStop)
107+
go k.watchUpdateStatus(k.updateStatusCallback, stop)
109108

110109
// block until constants.AnnotationOkToReboot is set
111110
for {
@@ -118,9 +117,6 @@ func (k *Klocksmith) process(stop <-chan struct{}) error {
118117
glog.Warningf("error waiting for an ok-to-reboot: %v", err)
119118
}
120119

121-
// stop watching the update status by closing the channel
122-
close(watchUpdateStatusStop)
123-
124120
// set constants.AnnotationRebootInProgress and drain self
125121
anno = map[string]string{
126122
constants.AnnotationRebootInProgress: constants.True,
@@ -168,8 +164,7 @@ func (k *Klocksmith) process(stop <-chan struct{}) error {
168164
k.lc.Reboot(false)
169165

170166
// cross fingers
171-
time.Sleep(24 * 7 * time.Hour)
172-
167+
sleepOrDone(24*7*time.Hour, stop)
173168
return nil
174169
}
175170

@@ -351,3 +346,17 @@ func (k *Klocksmith) getPodsForDeletion() ([]v1.Pod, error) {
351346

352347
return pods, nil
353348
}
349+
350+
// sleepOrDone pauses the current goroutine until the done channel receives
351+
// or until at least the duration d has elapsed, whichever comes first. This
352+
// is similar to time.Sleep(d), except it can be interrupted.
353+
func sleepOrDone(d time.Duration, done <-chan struct{}) {
354+
sleep := time.NewTimer(d)
355+
defer sleep.Stop()
356+
select {
357+
case <-sleep.C:
358+
return
359+
case <-done:
360+
return
361+
}
362+
}

0 commit comments

Comments
 (0)