File tree 1 file changed +12
-2
lines changed
1 file changed +12
-2
lines changed Original file line number Diff line number Diff line change 1
1
package time
2
2
3
3
import (
4
- "github.com/searKing/golang/go/sync/atomic"
5
4
"time"
5
+
6
+ "github.com/searKing/golang/go/sync/atomic"
6
7
)
7
8
8
9
// https://github.com/golang/go/issues/27169
@@ -12,14 +13,23 @@ type Timer struct {
12
13
chanConsumed atomic.Bool
13
14
}
14
15
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.
16
18
func (t * Timer ) ChanConsumed () {
17
19
t .chanConsumed .Store (true )
18
20
}
19
21
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,
20
27
func (t * Timer ) Reset (d time.Duration ) bool {
21
28
ret := t .Stop ()
22
29
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.
23
33
<- t .C
24
34
}
25
35
t .Timer .Reset (d )
You can’t perform that action at this time.
0 commit comments