From 2dccd58d29f94c54bc49dd549528854ba18f7b94 Mon Sep 17 00:00:00 2001 From: nolouch Date: Fri, 26 Jul 2024 15:51:17 +0800 Subject: [PATCH] tools: fix panic issue in tso bench Signed-off-by: nolouch --- tools/pd-tso-bench/main.go | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/tools/pd-tso-bench/main.go b/tools/pd-tso-bench/main.go index 3726373779e..1e59dfd2a2a 100644 --- a/tools/pd-tso-bench/main.go +++ b/tools/pd-tso-bench/main.go @@ -386,20 +386,27 @@ func reqWorker(ctx context.Context, pdClients []pd.Client, clientIdx int, durCh var ticker *time.Ticker if *maxTSOSendIntervalMilliseconds > 0 { sleepBeforeGetTS := time.Duration(rand.Intn(*maxTSOSendIntervalMilliseconds)) * time.Millisecond - ticker = time.NewTicker(sleepBeforeGetTS) - select { - case <-reqCtx.Done(): - case <-ticker.C: - totalSleepBeforeGetTS += sleepBeforeGetTS + if sleepBeforeGetTS > 0 { + ticker = time.NewTicker(sleepBeforeGetTS) + select { + case <-reqCtx.Done(): + case <-ticker.C: + totalSleepBeforeGetTS += sleepBeforeGetTS + } } } _, _, err = pdCli.GetLocalTS(reqCtx, *dcLocation) if errors.Cause(err) == context.Canceled { - ticker.Stop() + if ticker != nil { + ticker.Stop() + } + return } if err == nil { - ticker.Stop() + if ticker != nil { + ticker.Stop() + } break } log.Error(fmt.Sprintf("%v", err))