Skip to content

Commit 9398e38

Browse files
Merge pull request #153 from Workiva/aid_garbage_collection
Add an explicit timer and stop it
2 parents ba2c3af + 5b82c81 commit 9398e38

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

futures/futures.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,13 @@ func (f *Future) setItem(item interface{}, err error) {
7272

7373
func listenForResult(f *Future, ch Completer, timeout time.Duration, wg *sync.WaitGroup) {
7474
wg.Done()
75+
t := time.NewTimer(timeout)
7576
select {
7677
case item := <-ch:
7778
f.setItem(item, nil)
78-
case <-time.After(timeout):
79-
f.setItem(nil, fmt.Errorf(`Timeout after %f seconds.`, timeout.Seconds()))
79+
t.Stop() // we want to trigger GC of this timer as soon as it's no longer needed
80+
case <-t.C:
81+
f.setItem(nil, fmt.Errorf(`timeout after %f seconds`, timeout.Seconds()))
8082
}
8183
}
8284

0 commit comments

Comments
 (0)