From 5a566758ac9747532e7fc6dd8aff3b3ca0bdf95d Mon Sep 17 00:00:00 2001 From: lhy1024 Date: Fri, 30 Jun 2023 12:41:20 +0800 Subject: [PATCH] need be careful to use Timer.Reset Signed-off-by: lhy1024 --- client/tso_dispatcher.go | 18 ++++++++++++++++++ pkg/utils/tsoutil/tso_dispatcher.go | 9 +++++++++ 2 files changed, 27 insertions(+) diff --git a/client/tso_dispatcher.go b/client/tso_dispatcher.go index 6c644e2a6e0..1f5d6411ea6 100644 --- a/client/tso_dispatcher.go +++ b/client/tso_dispatcher.go @@ -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, @@ -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: diff --git a/pkg/utils/tsoutil/tso_dispatcher.go b/pkg/utils/tsoutil/tso_dispatcher.go index 875b1ab925f..9db0d27a3ab 100644 --- a/pkg/utils/tsoutil/tso_dispatcher.go +++ b/pkg/utils/tsoutil/tso_dispatcher.go @@ -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,