Skip to content

Commit e880c05

Browse files
committed
fix:修复Client中共享变量conn的可见性问题
1 parent 4df75dd commit e880c05

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

znet/client.go

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net"
77
"net/url"
8+
"sync"
89
"time"
910

1011
"github.com/aceld/zinx/zdecoder"
@@ -26,6 +27,8 @@ type Client struct {
2627
version string
2728
// Connection instance 连接实例
2829
conn ziface.IConnection
30+
// Connection instance 连接实例的锁,保证可见性
31+
connMux sync.Mutex
2932
// Hook function called on connection start 该client的连接创建时Hook函数
3033
onConnStart func(conn ziface.IConnection)
3134
// Hook function called on connection stop 该client的连接断开时的Hook函数
@@ -121,6 +124,7 @@ func (c *Client) Restart() {
121124
}
122125

123126
// Create a raw socket and get net.Conn (创建原始Socket,得到net.Conn)
127+
var connect ziface.IConnection
124128
switch c.version {
125129
case "websocket":
126130
wsAddr := fmt.Sprintf("ws://%s:%d", c.Ip, c.Port)
@@ -137,7 +141,7 @@ func (c *Client) Restart() {
137141
return
138142
}
139143
// Create Connection object
140-
c.conn = newWsClientConn(c, wsConn)
144+
connect = newWsClientConn(c, wsConn)
141145

142146
default:
143147
var conn net.Conn
@@ -166,19 +170,19 @@ func (c *Client) Restart() {
166170
}
167171
}
168172
// Create Connection object
169-
c.conn = newClientConn(c, conn)
173+
connect = newClientConn(c, conn)
170174
}
171175

172-
zlog.Ins().InfoF("[START] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", c.conn.LocalAddr(), c.conn.RemoteAddr())
176+
zlog.Ins().InfoF("[START] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", connect.LocalAddr(), connect.RemoteAddr())
173177
// HeartBeat detection
174178
if c.hc != nil {
175179
// Bind connection and heartbeat detector after connection is successfully established
176180
// (创建连接成功,绑定连接与心跳检测器)
177-
c.hc.BindConn(c.conn)
181+
c.hc.BindConn(connect)
178182
}
179183

180184
// Start connection
181-
go c.conn.Start()
185+
go connect.Start()
182186

183187
select {
184188
case <-c.exitChan:
@@ -237,8 +241,11 @@ func (c *Client) StartHeartBeatWithOption(interval time.Duration, option *ziface
237241
}
238242

239243
func (c *Client) Stop() {
240-
zlog.Ins().InfoF("[STOP] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", c.conn.LocalAddr(), c.conn.RemoteAddr())
241-
c.conn.Stop()
244+
con := c.Conn()
245+
if con != nil {
246+
zlog.Ins().InfoF("[STOP] Zinx Client LocalAddr: %s, RemoteAddr: %s\n", con.LocalAddr(), con.RemoteAddr())
247+
con.Stop()
248+
}
242249
c.exitChan <- struct{}{}
243250
close(c.exitChan)
244251
close(c.ErrChan)
@@ -249,9 +256,17 @@ func (c *Client) AddRouter(msgID uint32, router ziface.IRouter) {
249256
}
250257

251258
func (c *Client) Conn() ziface.IConnection {
259+
c.connMux.Lock()
260+
defer c.connMux.Unlock()
252261
return c.conn
253262
}
254263

264+
func (c *Client) setConn(con ziface.IConnection) {
265+
c.connMux.Lock()
266+
defer c.connMux.Unlock()
267+
c.conn = con
268+
}
269+
255270
func (c *Client) SetOnConnStart(hookFunc func(ziface.IConnection)) {
256271
c.onConnStart = hookFunc
257272
}

0 commit comments

Comments
 (0)