From fbb2cddd4ba82c32d71bb4359a113cbf4923d655 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Mon, 12 Jun 2023 10:34:16 +0800 Subject: [PATCH 1/3] client_batch: tiny optimize Signed-off-by: crazycs520 --- internal/client/client_batch.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/client/client_batch.go b/internal/client/client_batch.go index 216cd8075..17481b87c 100644 --- a/internal/client/client_batch.go +++ b/internal/client/client_batch.go @@ -549,14 +549,15 @@ func (c *batchCommandsClient) waitConnReady() (err error) { metrics.TiKVBatchClientWaitEstablish.Observe(time.Since(start).Seconds()) }() dialCtx, cancel := context.WithTimeout(context.Background(), c.dialTimeout) + // Trigger idle connection to reconnection + // Put it outside loop to avoid unnecessary reconnecting. + c.conn.Connect() for { s := c.conn.GetState() if s == connectivity.Ready { cancel() break } - // Trigger idle connection to reconnection - c.conn.Connect() if !c.conn.WaitForStateChange(dialCtx, s) { cancel() err = dialCtx.Err() From b530619bc80efb8fc2d89ee05eedef81fc7231b8 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Mon, 12 Jun 2023 10:48:02 +0800 Subject: [PATCH 2/3] address comment Signed-off-by: crazycs520 --- internal/client/client_batch.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/client/client_batch.go b/internal/client/client_batch.go index 17481b87c..f4daa9d27 100644 --- a/internal/client/client_batch.go +++ b/internal/client/client_batch.go @@ -551,7 +551,9 @@ func (c *batchCommandsClient) waitConnReady() (err error) { dialCtx, cancel := context.WithTimeout(context.Background(), c.dialTimeout) // Trigger idle connection to reconnection // Put it outside loop to avoid unnecessary reconnecting. - c.conn.Connect() + if c.conn.GetState() == connectivity.Idle { + c.conn.Connect() + } for { s := c.conn.GetState() if s == connectivity.Ready { From 24e699f9b89ea36106212f23d061611df2cdc400 Mon Sep 17 00:00:00 2001 From: crazycs520 Date: Mon, 12 Jun 2023 11:00:07 +0800 Subject: [PATCH 3/3] refine Signed-off-by: crazycs520 --- internal/client/client_batch.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/client/client_batch.go b/internal/client/client_batch.go index f4daa9d27..3c1129bc5 100644 --- a/internal/client/client_batch.go +++ b/internal/client/client_batch.go @@ -541,7 +541,8 @@ func (c *batchCommandsClient) failPendingRequests(err error) { } func (c *batchCommandsClient) waitConnReady() (err error) { - if c.conn.GetState() == connectivity.Ready { + state := c.conn.GetState() + if state == connectivity.Ready { return } start := time.Now() @@ -551,7 +552,7 @@ func (c *batchCommandsClient) waitConnReady() (err error) { dialCtx, cancel := context.WithTimeout(context.Background(), c.dialTimeout) // Trigger idle connection to reconnection // Put it outside loop to avoid unnecessary reconnecting. - if c.conn.GetState() == connectivity.Idle { + if state == connectivity.Idle { c.conn.Connect() } for {