From 813e26755ff2d5e4cea736a84a159d04af38a7af Mon Sep 17 00:00:00 2001 From: hoteas <925970985@qq.com> Date: Fri, 20 Mar 2026 13:52:23 +0800 Subject: [PATCH] =?UTF-8?q?refactor(db):=20=E4=BC=98=E5=8C=96=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BA=8B=E5=8A=A1=E4=B8=AD=E7=9A=84=E4=BA=92=E6=96=A5?= =?UTF-8?q?=E9=94=81=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构 queryWithRetry 和 execWithRetry 方法中的互斥锁逻辑,提升代码可读性 - 确保在测试事务中对 testMu 的锁定和解锁操作更加清晰,避免潜在的并发问题 --- db/query.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/db/query.go b/db/query.go index d53c1ce..6129e16 100644 --- a/db/query.go +++ b/db/query.go @@ -63,15 +63,21 @@ func (that *HoTimeDB) queryWithRetry(query string, retried bool, args ...interfa processedArgs := that.processArgs(args) if that.testTx != nil { - if that.testMu != nil { that.testMu.Lock() } + if that.testMu != nil { + that.testMu.Lock() + } resl, err = that.testTx.Query(query, processedArgs...) that.LastErr.SetError(err) if err != nil { - if that.testMu != nil { that.testMu.Unlock() } + if that.testMu != nil { + that.testMu.Unlock() + } return nil } result := that.Row(resl) - if that.testMu != nil { that.testMu.Unlock() } + if that.testMu != nil { + that.testMu.Unlock() + } return result } else if that.Tx != nil { resl, err = that.Tx.Query(query, processedArgs...) @@ -129,9 +135,13 @@ func (that *HoTimeDB) execWithRetry(query string, retried bool, args ...interfac processedArgs := that.processArgs(args) if that.testTx != nil { - if that.testMu != nil { that.testMu.Lock() } + if that.testMu != nil { + that.testMu.Lock() + } resl, e = that.testTx.Exec(query, processedArgs...) - if that.testMu != nil { that.testMu.Unlock() } + if that.testMu != nil { + that.testMu.Unlock() + } } else if that.Tx != nil { resl, e = that.Tx.Exec(query, processedArgs...) } else {