var timer *time.Timer
func A() {
timer.Stop() // cancel old timer
go B() // new timer
}
func B() {
timer = time.NewTimer(100 * time.Millisecond)
select {
case <- timer.C:
// do something for timeout, like change state
}
}
Function A and B are all in different goroutines.
Say A is in a RPC goroutine. When application receives RPC request, it will cancel the old timer in B, and start a new timer in another goroutine.
The doc say:
Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.
So how to break the select in B to avoid goroutine leak?