Skip to content

Commit

Permalink
perf:(menu): 优化GetCTX方法(处理data race)
Browse files Browse the repository at this point in the history
  • Loading branch information
yqchilde committed Dec 31, 2022
1 parent 21fa847 commit 58a93fa
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion engine/control/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func Register(service string, o *Options[*robot.Ctx]) *Engine {
log.Fatalf("[%s]插件优先级 %d 已被 %s 占用", service, priority, s)
}
priorityMap[priority] = service
log.Debugf("[%s]插件已注册, 优先级: %d", service, priority)
log.Printf("[%s]插件已注册, 优先级: %d", service, priority)
return newEngine(service, o)
}

Expand Down
12 changes: 8 additions & 4 deletions engine/robot/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"fmt"
"math/rand"
"runtime/debug"
"sync/atomic"
"time"

"github.com/yqchilde/wxbot/engine/pkg/log"
)

var (
BotConfig *Config // 机器人配置
Framework atomic.Value // 当前机器人框架
eventBuffer *EventBuffer // 事件缓冲区
)

Expand Down Expand Up @@ -238,11 +240,13 @@ func GetCTX() *Ctx {
case <-t.C:
log.Fatal("[robot] 获取CTX超时")
default:
if BotConfig != nil {
t.Stop()
return &Ctx{framework: BotConfig.Framework}
framework := Framework.Load()
if framework == nil {
time.Sleep(time.Second)
continue
}
time.Sleep(time.Second)
t.Stop()
return &Ctx{framework: framework.(IFramework)}
}
}
}
1 change: 1 addition & 0 deletions framework/qianxun/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func New(botWxId, apiUrl, apiToken string, servePort uint) *Framework {
}

func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
robot.Framework.Store(f)
http.HandleFunc("/wxbot/callback", func(w http.ResponseWriter, r *http.Request) {
recv, err := io.ReadAll(r.Body)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions framework/vlw/callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func New(botWxId, apiUrl, apiToken string, servePort uint) *Framework {
}

func (f *Framework) Callback(handler func(*robot.Event, robot.IFramework)) {
robot.Framework.Store(f)
http.HandleFunc("/wxbot/callback", func(w http.ResponseWriter, r *http.Request) {
recv, err := io.ReadAll(r.Body)
if err != nil {
Expand Down

0 comments on commit 58a93fa

Please sign in to comment.