modify close old connect connection logic

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

View File

@@ -292,22 +292,32 @@ func (b *Broker) handleConnection(typ int, conn net.Conn, idx uint64) {
switch typ {
case CLIENT:
msgPool = MSGPool[idx%MessagePoolNum].GetPool()
c.mp = msgPool
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)
case ROUTER:
msgPool = MSGPool[(MessagePoolNum + idx)].GetPool()
c.mp = msgPool
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)
}
if exist {
log.Warn("client or routers exist, close old...")
ol, ok := old.(*client)
if ok {
ol.Close()
}
}
c.readLoop(msgPool)
c.readLoop()
}
func (b *Broker) ConnectToRouters() {

View File

@@ -35,6 +35,7 @@ type client struct {
route *route
status int
smu sync.RWMutex
mp *MessagePool
subs map[string]*subscription
rsubs map[string]*subInfo
}
@@ -84,8 +85,9 @@ func (c *client) init() {
c.info.remoteIP = strings.Split(c.conn.RemoteAddr().String(), ":")[0]
}
func (c *client) readLoop(msgPool *MessagePool) {
func (c *client) readLoop() {
nc := c.conn
msgPool := c.mp
if nc == nil || msgPool == nil {
return
}