Skip to content

Commit 302b426

Browse files
committed
[ADD] comments
1 parent 1a5dfd4 commit 302b426

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

Diff for: go/time/timer.go

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package time
22

33
import (
4-
"github.com/searKing/golang/go/sync/atomic"
54
"time"
5+
6+
"github.com/searKing/golang/go/sync/atomic"
67
)
78

89
// https://github.com/golang/go/issues/27169
@@ -12,14 +13,23 @@ type Timer struct {
1213
chanConsumed atomic.Bool
1314
}
1415

15-
//saw channel read, must be called after receiving value from timer chan
16+
// saw channel read, must be called after receiving value from timer chan
17+
// an example case is AfterFunc bellow.
1618
func (t *Timer) ChanConsumed() {
1719
t.chanConsumed.Store(true)
1820
}
1921

22+
// Reset changes the timer to expire after duration d.
23+
// It returns true if the timer had been active, false if the timer had
24+
// expired or been stopped.
25+
// Reset can be invoked anytime, which enhances std time.Reset
26+
// that should be invoked only on stopped or expired timers with drained channels,
2027
func (t *Timer) Reset(d time.Duration) bool {
2128
ret := t.Stop()
2229
if !ret && !t.chanConsumed.Load() {
30+
// drain the channel, prevents the Timer from blocking on Send to t.C by sendTime, t.C is reused.
31+
// The underlying Timer is not recovered by the garbage collector until the timer fires.
32+
// consume the channel only once for the channel can be triggered only one time at most before Stop is called.
2333
<-t.C
2434
}
2535
t.Timer.Reset(d)

0 commit comments

Comments
 (0)