Skip to content

Commit

Permalink
need be careful to use Timer.Reset
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Jun 30, 2023
1 parent b5c5c68 commit 5a56675
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
18 changes: 18 additions & 0 deletions client/tso_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,15 @@ func NewTSDeadline(
cancel context.CancelFunc,
) *TSDeadline {
timer := timerPool.Get().(*time.Timer)
// Stop the timer if it's not stopped.
if !timer.Stop() {
select {
case <-timer.C: // try to drain from the channel
default:
}
}
// We need be careful here, see more details in the comments of Timer.Reset.
// https://pkg.go.dev/time@master#Timer.Reset
timer.Reset(timeout)
return &TSDeadline{
timer: timer,
Expand Down Expand Up @@ -419,6 +428,15 @@ tsoBatchLoop:
if maxBatchWaitInterval >= 0 {
tbc.adjustBestBatchSize()
}
// Stop the timer if it's not stopped.
if !streamLoopTimer.Stop() {
select {
case <-streamLoopTimer.C: // try to drain from the channel
default:
}
}
// We need be careful here, see more details in the comments of Timer.Reset.
// https://pkg.go.dev/time@master#Timer.Reset
streamLoopTimer.Reset(c.option.timeout)
// Choose a stream to send the TSO gRPC request.
streamChoosingLoop:
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/tsoutil/tso_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ func NewTSDeadline(
cancel context.CancelFunc,
) *TSDeadline {
timer := timerPool.Get().(*time.Timer)
// Stop the timer if it's not stopped.
if !timer.Stop() {
select {
case <-timer.C: // try to drain from the channel
default:
}
}
// We need be careful here, see more details in the comments of Timer.Reset.
// https://pkg.go.dev/time@master#Timer.Reset
timer.Reset(timeout)
return &TSDeadline{
timer: timer,
Expand Down

0 comments on commit 5a56675

Please sign in to comment.