@@ -54,6 +54,14 @@ type Timer struct {
54
54
// expired or been stopped.
55
55
// Stop does not close the channel, to prevent a read from the channel succeeding
56
56
// incorrectly.
57
+ //
58
+ // To prevent the timer firing after a call to Stop,
59
+ // check the return value and drain the channel. For example:
60
+ // if !t.Stop() {
61
+ // <-t.C
62
+ // }
63
+ // This cannot be done concurrent to other receives from the Timer's
64
+ // channel.
57
65
func (t * Timer ) Stop () bool {
58
66
if t .r .f == nil {
59
67
panic ("time: Stop called on uninitialized Timer" )
@@ -80,6 +88,20 @@ func NewTimer(d Duration) *Timer {
80
88
// Reset changes the timer to expire after duration d.
81
89
// It returns true if the timer had been active, false if the timer had
82
90
// expired or been stopped.
91
+ //
92
+ // To reuse an active timer, always call its Stop method first and—if it had
93
+ // expired—drain the value from its channel. For example:
94
+ // if !t.Stop() {
95
+ // <-t.C
96
+ // }
97
+ // t.Reset(d)
98
+ // This should not be done concurrent to other receives from the Timer's
99
+ // channel.
100
+ //
101
+ // Note that it is not possible to use Reset's return value correctly, as there
102
+ // is a race condition between draining the channel and the new timer expiring.
103
+ // Reset should always be used in concert with Stop, as described above.
104
+ // The return value exists to preserve compatibility with existing programs.
83
105
func (t * Timer ) Reset (d Duration ) bool {
84
106
if t .r .f == nil {
85
107
panic ("time: Reset called on uninitialized Timer" )
0 commit comments