新增:zconn.appendTo 方法,调整代码风格

git-svn-id: svn://47.119.165.148/zhub@65 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-01-10 18:02:12 +00:00
parent 01ced95223
commit 320a94af2d
4 changed files with 51 additions and 52 deletions

View File

@@ -71,6 +71,9 @@ func (c *Client) reconn() (err error) {
for topic, _ := range c.subFun { for topic, _ := range c.subFun {
c.subscribes(topic) c.subscribes(topic)
} }
for topic, _ := range c.timerFun {
c.timer(topic)
}
break break
} }
} }
@@ -146,6 +149,11 @@ func (c *Client) Timer(topic string, expr string, fun func()) {
c.send("timer", topic, expr) c.send("timer", topic, expr)
} }
// todo: save client timers info
func (c *Client) timer(topic string) {
c.send("timer", topic)
}
/* /*
// subscribe topic // subscribe topic
--- ---

View File

@@ -2,14 +2,15 @@ package main
import ( import (
"log" "log"
"strconv"
"testing" "testing"
"time" "time"
"zhub/cli" "zhub/cli"
) )
func TestCli(t *testing.T) { func TestCli(t *testing.T) {
//client, err := cli.Create("39.108.56.246:1216", "") client, err := cli.Create("39.108.56.246:7070", "")
client, err := cli.Create("127.0.0.1:1216", "topic-x") //client, err := cli.Create("127.0.0.1:1216", "topic-x")
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
@@ -24,12 +25,12 @@ func TestCli(t *testing.T) {
log.Println("收到 t------------------x 定时消息") log.Println("收到 t------------------x 定时消息")
}) })
/*go func() { go func() {
for i := 0; i < 100000; i++ { for i := 0; i < 100000; i++ {
client.Publish("a", strconv.Itoa(i)) client.Publish("a", strconv.Itoa(i))
time.Sleep(time.Second) time.Sleep(time.Second)
} }
}()*/ }()
client.Subscribe("a", func(v string) { client.Subscribe("a", func(v string) {
log.Println("收到主题 a 消息 " + v) log.Println("收到主题 a 消息 " + v)

View File

@@ -11,7 +11,7 @@ import (
) )
var ( var (
zsub ZSub = ZSub{ zsub = ZSub{
topics: make(map[string]*ZTopic), topics: make(map[string]*ZTopic),
timers: make(map[string]*ZTimer), timers: make(map[string]*ZTimer),
} }
@@ -59,32 +59,22 @@ func (s *ZSub) subscribe(c *ZConn, topic string) { // 新增订阅 zconn{}
ztopic.groups[c.groupid] = zgroup ztopic.groups[c.groupid] = zgroup
} }
_conns := make([]*ZConn, 0) zgroup.conns = c.appendTo(zgroup.conns)
for _, conn := range zgroup.conns {
if conn == c {
continue
}
_conns = append(_conns, conn)
}
_conns = append(_conns, c)
zgroup.conns = _conns
// 这是 ZConn for i, item := range c.topics {
_topics := c.topics if strings.EqualFold(item, topic) {
for _, _topic := range c.topics { c.topics = append(c.topics[:i], c.topics[:i+1]...)
if strings.EqualFold(_topic, topic) {
continue
} }
_topics = append(_topics, _topic)
} }
_topics = append(_topics, topic) c.topics = append(c.topics, topic)
c.topics = _topics
} }
/* /*
取消订阅: 取消订阅:
*/ */
func (s *ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{} func (s *ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
s.Lock()
defer s.Unlock()
ztopic := s.topics[topic] //ZTopic ztopic := s.topics[topic] //ZTopic
if ztopic == nil { if ztopic == nil {
return return
@@ -95,21 +85,17 @@ func (s *ZSub) unsubscribe(c *ZConn, topic string) { // 取消订阅 zconn{}
return return
} }
_conns := make([]*ZConn, 0) for i, item := range zgroup.conns {
for _, conn := range zgroup.conns { if item == c {
if conn == c { zgroup.conns = append(zgroup.conns[:i], zgroup.conns[:i+1]...)
continue
} }
_conns = append(_conns, c)
} }
zgroup.conns = _conns
} }
/* /*
发送主题消息 accept topic message
1、写入主题消息列表_zdb 1、send message to topic's chan
2、回复消息写入成功 2、feedback send success to sender, and sending message to topic's subscripts
3、推送主题消息
*/ */
func (s *ZSub) publish(topic string, msg string) { func (s *ZSub) publish(topic string, msg string) {
s.Lock() s.Lock()
@@ -123,12 +109,12 @@ func (s *ZSub) publish(topic string, msg string) {
} }
func (s *ZSub) close(c *ZConn) { func (s *ZSub) close(c *ZConn) {
// 订阅 // sub
for _, topic := range c.topics { for _, topic := range c.topics {
s.unsubscribe(c, topic) s.unsubscribe(c, topic)
} }
// 延时 // daly
// timer conn close // timer conn close
for _, topic := range c.timers { // fixme: 数据逻辑交叉循环 for _, topic := range c.timers { // fixme: 数据逻辑交叉循环
@@ -140,14 +126,23 @@ func (s *ZSub) close(c *ZConn) {
(*c.conn).Close() (*c.conn).Close()
} }
// ================== ZHub 服务 ===================================== func (c *ZConn) appendTo(arr []*ZConn) []*ZConn {
for i, item := range arr {
if item == c {
arr = append(arr[:i], arr[:i+1]...)
}
}
return append(arr, c)
}
// ================== ZHub server =====================================
func ServerStart(host string, port int) { func ServerStart(host string, port int) {
listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port)) listen, err := net.Listen("tcp", fmt.Sprintf("%s:%d", host, port))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
return return
} }
log.Printf("_zdb started listen on: %s:%d \n", host, port) log.Printf("zhub started listen on: %s:%d \n", host, port)
// 启动消息监听处理 // 启动消息监听处理
go func() { go func() {

View File

@@ -1,7 +1,6 @@
package zsub package zsub
import ( import (
"fmt"
"github.com/robfig/cron" "github.com/robfig/cron"
"strings" "strings"
) )
@@ -13,7 +12,13 @@ type ZTimer struct {
cron *cron.Cron cron *cron.Cron
} }
/*
1、["timer", topic, expr]
2、["timer", topic]
*/
func (s *ZSub) timer(rcmd []string, c *ZConn) { func (s *ZSub) timer(rcmd []string, c *ZConn) {
s.Lock()
defer s.Unlock()
timer := s.timers[rcmd[1]] timer := s.timers[rcmd[1]]
if timer == nil { if timer == nil {
timer = &ZTimer{ timer = &ZTimer{
@@ -22,18 +27,10 @@ func (s *ZSub) timer(rcmd []string, c *ZConn) {
} }
s.timers[rcmd[1]] = timer s.timers[rcmd[1]] = timer
} }
timer.conns = c.appendTo(timer.conns)
_conns := make([]*ZConn, 0) // todo: when timer.expr changed send message to all the timers subscribe
for _, conn := range timer.conns { if len(rcmd) == 3 && !strings.EqualFold(timer.expr, rcmd[2]) {
if conn == c {
continue
}
_conns = append(_conns, conn)
}
_conns = append(_conns, c)
timer.conns = _conns
if !strings.EqualFold(timer.expr, rcmd[2]) {
timer.expr = rcmd[2] timer.expr = rcmd[2]
if timer.cron != nil { if timer.cron != nil {
timer.cron.Stop() timer.cron.Stop()
@@ -52,14 +49,12 @@ func (s *ZSub) timer(rcmd []string, c *ZConn) {
} }
s.timers[rcmd[1]] = timer s.timers[rcmd[1]] = timer
fmt.Println("xx")
} }
func (t *ZTimer) close(c *ZConn) { func (t *ZTimer) close(c *ZConn) {
for i, conn := range t.conns { for i, item := range t.conns {
if conn.conn == c.conn { if item.conn == c.conn {
t.conns = append(t.conns[:i], t.conns[i+1:]...) t.conns = append(t.conns[:i], t.conns[i+1:]...)
} }
} }
t.conns = append(t.conns, c)
} }