modify close old connect connection logic

This commit is contained in:
zhouyuyan committed 2017-10-26 15:57:19 +08:00
1 parent eeab0c6b7d
commit a7fb7f1912
2 files changed
+21 -9

No files matched your search

+18 -8
View File
@@ -292,22 +292,32 @@ func (b *Broker) handleConnection(typ int, conn net.Conn, idx uint64) {
switch typ { switch typ {
case CLIENT: case CLIENT:
msgPool = MSGPool[idx%MessagePoolNum].GetPool() msgPool = MSGPool[idx%MessagePoolNum].GetPool()
c.mp = msgPool
old, exist = b.clients.Load(cid) old, exist = b.clients.Load(cid)
if exist {
log.Warn("client exist, close old...")
ol, ok := old.(*client)
if ok {
ol.Close()
}
}
b.clients.Store(cid, c) b.clients.Store(cid, c)
case ROUTER: case ROUTER:
msgPool = MSGPool[(MessagePoolNum + idx)].GetPool() msgPool = MSGPool[(MessagePoolNum + idx)].GetPool()
c.mp = msgPool
old, exist = b.routes.Load(cid) old, exist = b.routes.Load(cid)
if exist {
log.Warn("router exist, close old...")
ol, ok := old.(*client)
if ok {
msg := &Message{client: c, packet: DisconnectdPacket}
ol.mp.queue <- msg
}
}
b.routes.Store(cid, c) b.routes.Store(cid, c)
} }
if exist { c.readLoop()
log.Warn("client or routers exist, close old...")
ol, ok := old.(*client)
if ok {
ol.Close()
}
}
c.readLoop(msgPool)
} }
func (b *Broker) ConnectToRouters() { func (b *Broker) ConnectToRouters() {
+3 -1
View File
@@ -35,6 +35,7 @@ type client struct {
route *route route *route
status int status int
smu sync.RWMutex smu sync.RWMutex
mp *MessagePool
subs map[string]*subscription subs map[string]*subscription
rsubs map[string]*subInfo rsubs map[string]*subInfo
} }
@@ -84,8 +85,9 @@ func (c *client) init() {
c.info.remoteIP = strings.Split(c.conn.RemoteAddr().String(), ":")[0] c.info.remoteIP = strings.Split(c.conn.RemoteAddr().String(), ":")[0]
} }
func (c *client) readLoop(msgPool *MessagePool) { func (c *client) readLoop() {
nc := c.conn nc := c.conn
msgPool := c.mp
if nc == nil || msgPool == nil { if nc == nil || msgPool == nil {
return return
} }