Skip to content

Commit 5b82c81

Browse files
Add an explicit timer and stop it after we get the result we want to aid GC.
1 parent ba2c3af commit 5b82c81

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)