Replace calls to time.After with time.NewTimer for explicit stopping

This commit is contained in:
Daniel Smith
2015-05-04 14:29:33 -07:00
parent 838b59cb4d
commit 16a6fb8ef7
6 changed files with 48 additions and 18 deletions

View File

@@ -106,17 +106,24 @@ var (
errorStopRequested = errors.New("Stop requested")
)
// resyncChan returns a channel which will receive something when a resync is required.
func (r *Reflector) resyncChan() <-chan time.Time {
// resyncChan returns a channel which will receive something when a resync is
// required, and a cleanup function.
func (r *Reflector) resyncChan() (<-chan time.Time, func() bool) {
if r.resyncPeriod == 0 {
return neverExitWatch
return neverExitWatch, func() bool { return false }
}
return time.After(r.resyncPeriod)
// The cleanup function is required: imagine the scenario where watches
// always fail so we end up listing frequently. Then, if we don't
// manually stop the timer, we could end up with many timers active
// concurrently.
t := time.NewTimer(r.resyncPeriod)
return t.C, t.Stop
}
func (r *Reflector) listAndWatch(stopCh <-chan struct{}) {
var resourceVersion string
resyncCh := r.resyncChan()
resyncCh, cleanup := r.resyncChan()
defer cleanup()
list, err := r.listerWatcher.List()
if err != nil {