Skip to content

Commit 1fdad70

Browse files
committed
use atomic variable for closed.
1 parent a464739 commit 1fdad70

File tree

1 file changed

+7
-9
lines changed

1 file changed

+7
-9
lines changed

connection.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"strconv"
1616
"strings"
1717
"sync"
18+
"sync/atomic"
1819
"time"
1920
)
2021

@@ -46,8 +47,11 @@ type mysqlConn struct {
4647
closech chan struct{}
4748
finished chan<- struct{}
4849

50+
// set non-zero when conn is closed, before closech is closed.
51+
// accessed atomically.
52+
closed int32
53+
4954
mu sync.Mutex // guards following fields
50-
closed bool // set true when conn is closed, before closech is closed
5155
canceledErr error // set non-nil if conn is canceled
5256
}
5357

@@ -110,15 +114,11 @@ func (mc *mysqlConn) Close() (err error) {
110114
// is called before auth or on auth failure because MySQL will have already
111115
// closed the network connection.
112116
func (mc *mysqlConn) cleanup() {
113-
mc.mu.Lock()
114-
defer mc.mu.Unlock()
115-
116-
if mc.closed {
117+
if atomic.SwapInt32(&mc.closed, 1) != 0 {
117118
return
118119
}
119120

120121
// Makes cleanup idempotent
121-
mc.closed = true
122122
close(mc.closech)
123123
if mc.netConn == nil {
124124
return
@@ -129,9 +129,7 @@ func (mc *mysqlConn) cleanup() {
129129
}
130130

131131
func (mc *mysqlConn) isBroken() bool {
132-
mc.mu.Lock()
133-
defer mc.mu.Unlock()
134-
return mc.closed
132+
return atomic.LoadInt32(&mc.closed) != 0
135133
}
136134

137135
func (mc *mysqlConn) error() error {

0 commit comments

Comments
 (0)