修改:代码结构、完成timer、sub/puh 测试

git-svn-id: svn://47.119.165.148/zhub@64 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-10 16:37:46 +00:00
parent f1b7a862f9
commit 01ced95223
9 changed files with 172 additions and 87 deletions

View File

@@ -27,11 +27,21 @@ func msgAccept(v Message) {
if len(rcmd) == 1 {
switch strings.ToLower(rcmd[0]) {
default:
// subscribe|unsubscribe|daly
if strings.Index(rcmd[0], "subscribe") == 0 || strings.Index(rcmd[0], "unsubscribe") == 0 || strings.Index(rcmd[0], "daly") == 0 {
// str start with strs anyone
var startWithAny = func(str string, strs ...string) bool {
for _, str := range strs {
if strings.Index(rcmd[0], str) == 0 {
return true
}
}
return false
}
arr := []string{"subscribe", "unsubscribe", "daly", "groupid"}
if startWithAny(rcmd[0], arr...) {
rcmd = strings.Split(rcmd[0], " ")
} else {
send(c.conn, "-Error: not supported! (tips: send help)")
send(c.conn, "-Error: not supported:"+rcmd[0])
return
}
}
@@ -39,8 +49,10 @@ func msgAccept(v Message) {
cmd := rcmd[0]
switch cmd {
case "groupid":
c.groupid = rcmd[1]
case "subscribe":
//subscribe x y z
// subscribe x y z
for _, topic := range rcmd[1:] {
zsub.subscribe(c, topic) // todo: 批量一次订阅
}
@@ -57,7 +69,7 @@ func msgAccept(v Message) {
case "daly":
daly(rcmd, c)
case "timer":
// todo Timer(rcmd, conn)
zsub.timer(rcmd, c)
default:
send(c.conn, "-Error: default not supported:["+strings.Join(rcmd, " ")+"]")
return
@@ -86,7 +98,7 @@ func daly(rcmd []string, c *ZConn) {
var wlock = sync.Mutex{}
// 发送消息
// send message
func send(conn *net.Conn, vs ...string) error {
wlock.Lock()
defer wlock.Unlock()

View File

@@ -2,32 +2,27 @@ package zsub
import "sync"
type ZGroup struct { //ZGroup
type ZGroup struct { // ZGroup
sync.Mutex
conns []*ZConn
offset int
chMsg chan string // 组消息即时投递
ztopic *ZTopic // 所属topic
}
func createZGroup(c *ZConn) *ZGroup {
zgroup := &ZGroup{
conns: []*ZConn{},
chMsg: make(chan string, 100),
}
// 开启消息推送
func (g *ZGroup) init() {
go func() {
for {
msg, ok := <-zgroup.chMsg
msg, ok := <-g.chMsg
if !ok {
break
}
for _, c := range zgroup.conns {
(*c.conn).Write([]byte(msg))
zgroup.offset++
if len(g.conns) == 0 {
continue
}
send(g.conns[0].conn, "message", g.ztopic.topic, msg)
g.offset++
}
}()
return zgroup
}

View File

@@ -6,11 +6,15 @@ import (
"log"
"net"
"strconv"
"strings"
"sync"
)
var (
zsub ZSub
zsub ZSub = ZSub{
topics: make(map[string]*ZTopic),
timers: make(map[string]*ZTimer),
}
)
type ZSub struct {
@@ -32,16 +36,26 @@ type ZConn struct { //ZConn
2、加入到对应组别如果是第一次的消费组 offset从当前 mcount 开始
3、若有待消费消息启动消费
*/
func (s ZSub) subscribe(c *ZConn, topic string) { // 新增订阅 zconn{}
func (s *ZSub) subscribe(c *ZConn, topic string) { // 新增订阅 zconn{}
ztopic := s.topics[topic] //ZTopic
if ztopic == nil {
ztopic = &ZTopic{groups: map[string]*ZGroup{}}
ztopic = &ZTopic{
groups: map[string]*ZGroup{},
topic: topic,
chMsg: make(chan string, 100),
}
ztopic.init()
s.topics[topic] = ztopic
}
zgroup := ztopic.groups[c.groupid] //ZGroup
if zgroup == nil {
zgroup = &ZGroup{conns: []*ZConn{}}
zgroup = &ZGroup{
conns: []*ZConn{},
ztopic: ztopic,
chMsg: make(chan string, 1000),
}
zgroup.init()
ztopic.groups[c.groupid] = zgroup
}
@@ -54,12 +68,23 @@ func (s ZSub) subscribe(c *ZConn, topic string) { // 新增订阅 zconn{}
}
_conns = append(_conns, c)
zgroup.conns = _conns
// 这是 ZConn
_topics := c.topics
for _, _topic := range c.topics {
if strings.EqualFold(_topic, topic) {
continue
}
_topics = append(_topics, _topic)
}
_topics = append(_topics, topic)
c.topics = _topics
}
/*
取消订阅:
*/
func (s ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
func (s *ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
ztopic := s.topics[topic] //ZTopic
if ztopic == nil {
return
@@ -86,20 +111,18 @@ func (s ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
2、回复消息写入成功
3、推送主题消息
*/
func (s ZSub) publish(topic string, message string) {
func (s *ZSub) publish(topic string, msg string) {
s.Lock()
defer s.Unlock()
ztopic := s.topics[topic] //ZTopic
if ztopic == nil {
return
}
for _, zgroup := range ztopic.groups {
zgroup.chMsg <- message // 不同主题消费独立进行
}
ztopic.chMsg <- msg
ztopic.mcount++
}
func (s ZSub) close(c *ZConn) {
func (s *ZSub) close(c *ZConn) {
// 订阅
for _, topic := range c.topics {
s.unsubscribe(c, topic)
@@ -114,6 +137,7 @@ func (s ZSub) close(c *ZConn) {
timer.close(c)
}
}
(*c.conn).Close()
}
// ================== ZHub 服务 =====================================
@@ -146,14 +170,18 @@ func ServerStart(host string, port int) {
}
fmt.Println("conn start: ", conn.RemoteAddr())
go zsub.acceptHandler(&ZConn{conn: &conn})
go zsub.acceptHandler(&ZConn{
conn: &conn,
topics: []string{},
timers: []string{},
})
}
}
// 连接处理
func (s ZSub) acceptHandler(c *ZConn) {
func (s *ZSub) acceptHandler(c *ZConn) {
defer func() {
s.close(c) // 关闭连接
s.close(c) // close ZConn
}()
reader := bufio.NewReader(*c.conn)

View File

@@ -4,7 +4,6 @@ import (
"fmt"
"github.com/robfig/cron"
"strings"
"time"
)
type ZTimer struct {
@@ -14,7 +13,7 @@ type ZTimer struct {
cron *cron.Cron
}
func (s ZSub) timer(rcmd []string, c *ZConn) {
func (s *ZSub) timer(rcmd []string, c *ZConn) {
timer := s.timers[rcmd[1]]
if timer == nil {
timer = &ZTimer{
@@ -29,7 +28,7 @@ func (s ZSub) timer(rcmd []string, c *ZConn) {
if conn == c {
continue
}
_conns = append(_conns, c)
_conns = append(_conns, conn)
}
_conns = append(_conns, c)
timer.conns = _conns
@@ -42,7 +41,7 @@ func (s ZSub) timer(rcmd []string, c *ZConn) {
timer.cron = func() *cron.Cron {
c := cron.New()
c.AddFunc(timer.expr, func() {
fmt.Println(time.Now().Second())
//fmt.Println(time.Now().Second())
for _, conn := range timer.conns {
send(conn.conn, "timer", timer.topic)
}
@@ -56,7 +55,11 @@ func (s ZSub) timer(rcmd []string, c *ZConn) {
fmt.Println("xx")
}
func (t ZTimer) close(c *ZConn) {
// todo timer zconn
func (t *ZTimer) close(c *ZConn) {
for i, conn := range t.conns {
if conn.conn == c.conn {
t.conns = append(t.conns[:i], t.conns[i+1:]...)
}
}
t.conns = append(t.conns, c)
}

View File

@@ -6,9 +6,24 @@ type ZTopic struct { //ZTopic
sync.Mutex
groups map[string]*ZGroup
mcount int
topic string // 主题名称
chMsg chan string // 主题消息投递
}
// 主题消息发送
func (t *ZTopic) init() {
go func() {
for {
msg, ok := <-t.chMsg
if !ok {
break
}
for _, group := range t.groups {
group.chMsg <- msg
}
}
}()
}
//