File tree 1 file changed +7
-9
lines changed
1 file changed +7
-9
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,7 @@ import (
15
15
"strconv"
16
16
"strings"
17
17
"sync"
18
+ "sync/atomic"
18
19
"time"
19
20
)
20
21
@@ -46,8 +47,11 @@ type mysqlConn struct {
46
47
closech chan struct {}
47
48
finished chan <- struct {}
48
49
50
+ // set non-zero when conn is closed, before closech is closed.
51
+ // accessed atomically.
52
+ closed int32
53
+
49
54
mu sync.Mutex // guards following fields
50
- closed bool // set true when conn is closed, before closech is closed
51
55
canceledErr error // set non-nil if conn is canceled
52
56
}
53
57
@@ -110,15 +114,11 @@ func (mc *mysqlConn) Close() (err error) {
110
114
// is called before auth or on auth failure because MySQL will have already
111
115
// closed the network connection.
112
116
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 {
117
118
return
118
119
}
119
120
120
121
// Makes cleanup idempotent
121
- mc .closed = true
122
122
close (mc .closech )
123
123
if mc .netConn == nil {
124
124
return
@@ -129,9 +129,7 @@ func (mc *mysqlConn) cleanup() {
129
129
}
130
130
131
131
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
135
133
}
136
134
137
135
func (mc * mysqlConn ) error () error {
You can’t perform that action at this time.
0 commit comments