修改:zgroup 订阅组 共同消费组消息

git-svn-id: svn://47.119.165.148/zhub@92 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-28 12:11:35 +00:00
parent c62e031076
commit 7746b768bd
3 changed files with 48 additions and 15 deletions

View File

@@ -10,8 +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 = "39.108.56.246:1216" //addr = "122.112.180.156:6066"
addr = "39.108.56.246:1216"
) )
func TestCli(t *testing.T) { func TestCli(t *testing.T) {
@@ -42,7 +43,7 @@ func TestCli(t *testing.T) {
func TestTimer(t *testing.T) { func TestTimer(t *testing.T) {
go func() { go func() {
client, _ := cli.Create(addr, "topic-2") client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) { client.Subscribe("ax", func(v string) {
log.Println("topic-1-ax: " + v) log.Println("topic-1-ax: " + v)
@@ -56,6 +57,29 @@ func TestTimer(t *testing.T) {
}) })
}() }()
go func() {
client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) {
log.Println("topic-3-ax: " + v)
})
}()
go func() {
client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) {
log.Println("topic-4-ax: " + v)
})
}()
go func() {
client, _ := cli.Create(addr, "topic-1")
client.Subscribe("ax", func(v string) {
log.Println("topic-5-ax: " + v)
})
}()
/*go func() { /*go func() {
client, _ := cli.Create(addr, "topic-2") client, _ := cli.Create(addr, "topic-2")
client.Timer("a", func() { client.Timer("a", func() {
@@ -97,8 +121,9 @@ func TestPublish(t *testing.T) {
if err != nil { if err != nil {
log.Println(err) log.Println(err)
} }
for i := 0; i < 30_0000; i++ { for i := 0; i < 100_0000; i++ {
client.Publish("ax", strconv.Itoa(i)) client.Publish("ax", strconv.Itoa(i))
//time.Sleep(time.Second)
} }
time.Sleep(time.Second) time.Sleep(time.Second)

View File

@@ -1,28 +1,33 @@
package zsub package zsub
import "sync" import (
"sync"
"sync/atomic"
)
type ZGroup struct { // ZGroup type ZGroup struct { // ZGroup
sync.Mutex sync.Mutex
conns []*ZConn conns []*ZConn
offset int offset int32
chMsg chan string // 组消息即时投递 chMsg chan string // 组消息即时投递
ztopic *ZTopic // 所属topic ztopic *ZTopic // 所属topic
} }
func (g *ZGroup) init() { func (g *ZGroup) appendTo(c *ZConn) {
go func() { c.appendTo(g.conns)
go func() { // 每个连接开启一个携程发送数据
for { for {
msg, ok := <-g.chMsg msg, ok := <-g.chMsg
if !ok { if !ok {
break break
} }
if len(g.conns) == 0 { err := c.send("message", g.ztopic.topic, msg)
continue if err != nil { // 失败处理
g.chMsg <- msg
break
} }
g.conns[0].send("message", g.ztopic.topic, msg) atomic.AddInt32(&g.offset, 1)
g.offset++
} }
}() }()
} }

View File

@@ -53,15 +53,15 @@ func (s *ZSub) subscribe(c *ZConn, topic string) { // 新增订阅 zconn{}
zgroup := ztopic.groups[c.groupid] //ZGroup zgroup := ztopic.groups[c.groupid] //ZGroup
if zgroup == nil { if zgroup == nil {
zgroup = &ZGroup{ zgroup = &ZGroup{
conns: []*ZConn{}, //conns: []*ZConn{},
ztopic: ztopic, ztopic: ztopic,
chMsg: make(chan string, 1000), chMsg: make(chan string, 1000),
} }
zgroup.init()
ztopic.groups[c.groupid] = zgroup ztopic.groups[c.groupid] = zgroup
} }
zgroup.conns = c.appendTo(zgroup.conns) //zgroup.conns = c.appendTo(zgroup.conns)
zgroup.appendTo(c)
for i, item := range c.topics { for i, item := range c.topics {
if strings.EqualFold(item, topic) { if strings.EqualFold(item, topic) {
@@ -156,6 +156,9 @@ func (s *ZSub) close(c *ZConn) {
} }
func (c *ZConn) appendTo(arr []*ZConn) []*ZConn { func (c *ZConn) appendTo(arr []*ZConn) []*ZConn {
if arr == nil {
arr = make([]*ZConn, 0)
}
for i, item := range arr { for i, item := range arr {
if item == c { if item == c {
arr = append(arr[:i], arr[i+1:]...) arr = append(arr[:i], arr[i+1:]...)