.
git-svn-id: svn://47.119.165.148/zhub@58 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
33
zsub/zgroup.go
Normal file
33
zsub/zgroup.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package zsub
|
||||
|
||||
import "sync"
|
||||
|
||||
type ZGroup struct { //ZGroup
|
||||
sync.Mutex
|
||||
conns []*ZConn
|
||||
offset int
|
||||
chMsg chan string // 组消息即时投递
|
||||
}
|
||||
|
||||
func createZGroup(c *ZConn) *ZGroup {
|
||||
zgroup := &ZGroup{
|
||||
conns: []*ZConn{},
|
||||
chMsg: make(chan string, 100),
|
||||
}
|
||||
|
||||
// 开启消息推送
|
||||
go func() {
|
||||
for {
|
||||
msg, ok := <-zgroup.chMsg
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
|
||||
for _, c := range zgroup.conns {
|
||||
(*c.conn).Write([]byte(msg))
|
||||
zgroup.offset++
|
||||
}
|
||||
}
|
||||
}()
|
||||
return zgroup
|
||||
}
|
||||
95
zsub/zsub.go
Normal file
95
zsub/zsub.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package zsub
|
||||
|
||||
import (
|
||||
"net"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var (
|
||||
zsub ZSub
|
||||
)
|
||||
|
||||
type ZSub struct {
|
||||
sync.Mutex
|
||||
topics map[string]*ZTopic
|
||||
}
|
||||
|
||||
|
||||
type ZConn struct { //ZConn
|
||||
conn *net.Conn
|
||||
groupid string
|
||||
topics []string
|
||||
}
|
||||
|
||||
/*
|
||||
新增订阅:
|
||||
1、找到对应主题信息
|
||||
2、加入到对应组别;如果是第一次的消费组 offset从当前 mcount 开始
|
||||
3、若有待消费消息启动消费
|
||||
*/
|
||||
func (s ZSub) subscribe(c *ZConn, topic string) { // 新增订阅 zconn{}
|
||||
ztopic := s.topics[topic] //ZTopic
|
||||
if ztopic == nil {
|
||||
ztopic = &ZTopic{groups: map[string]*ZGroup{}}
|
||||
s.topics[topic] = ztopic
|
||||
}
|
||||
|
||||
zgroup := ztopic.groups[c.groupid] //ZGroup
|
||||
if zgroup == nil {
|
||||
zgroup = &ZGroup{conns: []*ZConn{}}
|
||||
ztopic.groups[c.groupid] = zgroup
|
||||
}
|
||||
|
||||
_conns := make([]*ZConn, 0)
|
||||
for _, conn := range zgroup.conns {
|
||||
if conn == c {
|
||||
continue
|
||||
}
|
||||
_conns = append(_conns, conn)
|
||||
}
|
||||
_conns = append(_conns, c)
|
||||
zgroup.conns = _conns
|
||||
}
|
||||
|
||||
/*
|
||||
取消订阅:
|
||||
*/
|
||||
func (s ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
|
||||
ztopic := s.topics[topic] //ZTopic
|
||||
if ztopic == nil {
|
||||
return
|
||||
}
|
||||
|
||||
zgroup := ztopic.groups[c.groupid] //ZGroup
|
||||
if zgroup == nil {
|
||||
return
|
||||
}
|
||||
|
||||
_conns := make([]*ZConn, 0)
|
||||
for _, conn := range zgroup.conns {
|
||||
if conn == c {
|
||||
continue
|
||||
}
|
||||
_conns = append(_conns, c)
|
||||
}
|
||||
zgroup.conns = _conns
|
||||
}
|
||||
|
||||
/*
|
||||
发送主题消息
|
||||
1、写入主题消息列表(zdb)
|
||||
2、回复消息写入成功
|
||||
3、推送主题消息
|
||||
*/
|
||||
func (s ZSub) publish(topic string, message string) {
|
||||
s.Lock()
|
||||
defer s.Unlock()
|
||||
ztopic := s.topics[topic] //ZTopic
|
||||
if ztopic == nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, zgroup := range ztopic.groups {
|
||||
zgroup.chMsg <- message
|
||||
}
|
||||
}
|
||||
36
zsub/zsub_test.go
Normal file
36
zsub/zsub_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package zsub
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
sub := ZSub{
|
||||
topics: map[string]*ZTopic{},
|
||||
}
|
||||
|
||||
sub.subscribe(&ZConn{
|
||||
groupid: "a",
|
||||
}, "ab")
|
||||
|
||||
sub.subscribe(&ZConn{
|
||||
groupid: "b",
|
||||
}, "ab")
|
||||
|
||||
// -----------------
|
||||
|
||||
sub.subscribe(&ZConn{
|
||||
groupid: "b",
|
||||
}, "abx")
|
||||
|
||||
conn := ZConn{
|
||||
groupid: "a",
|
||||
}
|
||||
|
||||
sub.subscribe(&conn, "abx")
|
||||
|
||||
sub.unsubscribe(&conn, "abx")
|
||||
|
||||
fmt.Println(1)
|
||||
}
|
||||
14
zsub/ztopic.go
Normal file
14
zsub/ztopic.go
Normal file
@@ -0,0 +1,14 @@
|
||||
package zsub
|
||||
|
||||
import "sync"
|
||||
|
||||
type ZTopic struct { //ZTopic
|
||||
sync.Mutex
|
||||
groups map[string]*ZGroup
|
||||
mcount int
|
||||
chMsg chan string // 主题消息投递
|
||||
}
|
||||
|
||||
func createZTopic() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user