修改:内部指令执行串行执行,避免并发下操作 map 崩溃

git-svn-id: svn://47.119.165.148/zhub@109 e63fbceb-bcc3-4977-ac22-735b83d8d0f4
This commit is contained in:
lxy
2021-02-23 10:46:34 +00:00
parent d5ffd34b8b
commit c711cc10ae
2 changed files with 50 additions and 32 deletions

View File

@@ -8,6 +8,8 @@ import (
"zhub/conf"
)
var funChan = make(chan func(), 1000)
type ZDelay struct {
topic string
value string
@@ -56,9 +58,21 @@ func msgAccept(v Message) {
}
cmd := rcmd[0]
switch cmd {
case "groupid":
if strings.EqualFold(cmd, "groupid") {
c.groupid = rcmd[1]
return
} else if strings.EqualFold(cmd, "publish") {
if len(rcmd) != 3 {
c.send("-Error: publish para number!")
} else {
zsub.publish(rcmd[1], rcmd[2])
}
return
}
// 内部执行指令 加入执行队列
funChan <- func() {
switch cmd {
case "subscribe":
// subscribe x y z
for _, topic := range rcmd[1:] {
@@ -68,12 +82,6 @@ func msgAccept(v Message) {
for _, topic := range rcmd[1:] {
zsub.unsubscribe(c, topic)
}
case "publish":
if len(rcmd) != 3 {
c.send("-Error: publish para number!")
} else {
zsub.publish(rcmd[1], rcmd[2])
}
case "broadcast":
zsub.broadcast(rcmd[1], rcmd[2])
case "delay":
@@ -97,6 +105,7 @@ func msgAccept(v Message) {
c.send("-Error: default not supported:[" + strings.Join(rcmd, " ") + "]")
return
}
}
}
// delay topic value 100 -> publish topic value

View File

@@ -190,9 +190,18 @@ func (c *ZConn) appendTo(arr []*ZConn) []*ZConn {
2、启动服务监听
*/
func ServerStart(addr string) {
conf.GetStr("data.dir", "data")
go func() {
for {
fun, ok := <-funChan
if !ok {
break
}
fun()
}
}()
// 重新加载[定时、延时]
go zsub.reloadTimerConfig()
go zsub.reloadDelay()