修改:连接断开后消息推送携程未回收bug

git-svn-id: svn://47.119.165.148/zhub@94 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-29 06:14:17 +00:00
parent 7746b768bd
commit d6dda9e968
3 changed files with 35 additions and 24 deletions

View File

@@ -17,17 +17,21 @@ func (g *ZGroup) appendTo(c *ZConn) {
c.appendTo(g.conns)
go func() { // 每个连接开启一个携程发送数据
for {
msg, ok := <-g.chMsg
if !ok {
break
}
select {
case msg, ok := <-g.chMsg:
if !ok {
return
}
err := c.send("message", g.ztopic.topic, msg)
if err != nil { // 失败处理
g.chMsg <- msg
break
err := c.send("message", g.ztopic.topic, msg)
if err != nil { // 失败处理
g.chMsg <- msg
return
}
atomic.AddInt32(&g.offset, 1)
case <-c.stoped:
return
}
atomic.AddInt32(&g.offset, 1)
}
}()
}

View File

@@ -28,6 +28,16 @@ type ZConn struct { //ZConn
groupid string
topics []string
timers []string // 订阅、定时调度分别创建各自连接
stoped chan int // 关闭信号量
}
func NewZConn(conn *net.Conn) *ZConn {
return &ZConn{
conn: conn,
topics: []string{},
timers: []string{},
stoped: make(chan int, 0),
}
}
/*
@@ -130,6 +140,7 @@ func (s *ZSub) broadcast(topic, msg string) {
}
func (s *ZSub) close(c *ZConn) {
close(c.stoped)
// sub
for _, topic := range c.topics {
s.unsubscribe(c, topic)
@@ -205,11 +216,8 @@ func ServerStart(addr string) {
}
log.Println("conn start: ", conn.RemoteAddr())
go zsub.acceptHandler(&ZConn{
conn: &conn,
topics: []string{},
timers: []string{},
})
zConn := NewZConn(&conn)
go zsub.acceptHandler(zConn)
}
}