修改:连接断开后消息推送携程未回收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

@@ -10,9 +10,9 @@ import (
var ( var (
//addr = "47.111.150.118:6066" //addr = "47.111.150.118:6066"
//addr = "127.0.0.1:1216" addr = "127.0.0.1:1216"
//addr = "122.112.180.156:6066" //addr = "122.112.180.156:6066"
addr = "39.108.56.246:1216" //addr = "39.108.56.246:1216"
) )
func TestCli(t *testing.T) { func TestCli(t *testing.T) {
@@ -45,14 +45,14 @@ func TestTimer(t *testing.T) {
go func() { go func() {
client, _ := cli.Create(addr, "topic-1") client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) { client.Subscribe("ax1", func(v string) {
log.Println("topic-1-ax: " + v) log.Println("topic-1-ax: " + v)
}) })
}() }()
go func() { go func() {
client, _ := cli.Create(addr, "topic-1") client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) { client.Subscribe("ax1", func(v string) {
log.Println("topic-2-ax: " + v) log.Println("topic-2-ax: " + v)
}) })
}() }()
@@ -60,12 +60,12 @@ func TestTimer(t *testing.T) {
go func() { go func() {
client, _ := cli.Create(addr, "topic-1") client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) { client.Subscribe("ax1", func(v string) {
log.Println("topic-3-ax: " + v) log.Println("topic-3-ax: " + v)
}) })
}() }()
go func() { /*go func() {
client, _ := cli.Create(addr, "topic-1") client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) { client.Subscribe("ax", func(v string) {
@@ -78,7 +78,7 @@ func TestTimer(t *testing.T) {
client.Subscribe("ax", func(v string) { client.Subscribe("ax", func(v string) {
log.Println("topic-5-ax: " + v) log.Println("topic-5-ax: " + v)
}) })
}() }()*/
/*go func() { /*go func() {
client, _ := cli.Create(addr, "topic-2") client, _ := cli.Create(addr, "topic-2")
@@ -121,9 +121,8 @@ func TestPublish(t *testing.T) {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
for i := 0; i < 100_0000; i++ { for i := 0; i < 10000; i++ {
client.Publish("ax", strconv.Itoa(i)) client.Publish("ax1", strconv.Itoa(i))
//time.Sleep(time.Second)
} }
time.Sleep(time.Second) time.Sleep(time.Second)

View File

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

View File

@@ -28,6 +28,16 @@ type ZConn struct { //ZConn
groupid string groupid string
topics []string topics []string
timers []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) { func (s *ZSub) close(c *ZConn) {
close(c.stoped)
// sub // sub
for _, topic := range c.topics { for _, topic := range c.topics {
s.unsubscribe(c, topic) s.unsubscribe(c, topic)
@@ -205,11 +216,8 @@ func ServerStart(addr string) {
} }
log.Println("conn start: ", conn.RemoteAddr()) log.Println("conn start: ", conn.RemoteAddr())
go zsub.acceptHandler(&ZConn{ zConn := NewZConn(&conn)
conn: &conn, go zsub.acceptHandler(zConn)
topics: []string{},
timers: []string{},
})
} }
} }