Skip to content

Commit 618685d

Browse files
committed
fix(chat): concurrent map write
1 parent 62a44be commit 618685d

4 files changed

Lines changed: 28 additions & 12 deletions

File tree

chat/agent.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"reflect"
1010
"strconv"
1111
"strings"
12-
"sync"
1312
"time"
1413

1514
"github.com/RomiChan/syncx"
@@ -21,11 +20,17 @@ import (
2120
"gopkg.in/yaml.v3"
2221

2322
"github.com/FloatTech/floatbox/binary"
23+
"github.com/FloatTech/zbputils/control"
2424
"github.com/FloatTech/zbputils/vevent"
2525
zero "github.com/wdvxdr1123/ZeroBot"
2626
"github.com/wdvxdr1123/ZeroBot/message"
2727
)
2828

29+
const (
30+
StateKeyAgentHooked = zero.StateKeyPrefixKeep + "_chat_ag_hooked__"
31+
StateKeyAgentTriggered = zero.StateKeyPrefixKeep + "_chat_ag_triggered__"
32+
)
33+
2934
// agentchar 将 char.yaml 内容嵌入为默认 agent 性格
3035
//
3136
//go:embed char.yaml
@@ -255,8 +260,6 @@ func truncatecopy(params map[string]any) map[string]any {
255260
return cpp
256261
}
257262

258-
var logevmu sync.Mutex
259-
260263
func logev(ctx *zero.Ctx) {
261264
gid := ctx.Event.GroupID
262265
if gid == 0 {
@@ -270,27 +273,23 @@ func logev(ctx *zero.Ctx) {
270273
logrus.Debugln("[chat] agent", gid, "add ev:", binary.BytesToString(data))
271274
AgentOf(ctx.Event.SelfID, "aichat").AddEvent(gid, ev)
272275

273-
logevmu.Lock()
274-
if _, ok := ctx.State[zero.StateKeyPrefixKeep+"_chat_ag_hooked__"]; ok {
275-
logevmu.Unlock()
276+
mp := ctx.State[control.StateKeySyncxState].(*syncx.Map[string, any])
277+
278+
_, hasvalue := mp.LoadOrStore(StateKeyAgentHooked, struct{}{})
279+
if hasvalue {
276280
return
277281
}
278-
ctx.State[zero.StateKeyPrefixKeep+"_chat_ag_hooked__"] = struct{}{}
279-
logevmu.Unlock()
280282

281283
vevent.HookCtxCaller(ctx, vevent.NewAPICallerReturnHook(
282284
ctx, func(req zero.APIRequest, rsp zero.APIResponse, _ error) {
283285
gid := ctx.Event.GroupID
284286
if gid == 0 {
285287
gid = -ctx.Event.UserID
286288
}
287-
logevmu.Lock()
288-
if _, ok := ctx.State[zero.StateKeyPrefixKeep+"_chat_ag_triggered__"]; ok {
289-
logevmu.Unlock()
289+
if _, ok := mp.Load(StateKeyAgentTriggered); ok {
290290
logrus.Debugln("[chat] agent", gid, "skip agent triggered requ:", &req)
291291
return
292292
}
293-
logevmu.Unlock()
294293
if req.Action != "send_private_msg" &&
295294
req.Action != "send_group_msg" &&
296295
req.Action != "delete_msg" {

control/ctx.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package control
2+
3+
import (
4+
"github.com/RomiChan/syncx"
5+
zero "github.com/wdvxdr1123/ZeroBot"
6+
)
7+
8+
const (
9+
StateKeySyncxState = zero.StateKeyPrefixKeep + "_ctrl_syncx_state__"
10+
)
11+
12+
func addsyncxstate(ctx *zero.Ctx) bool {
13+
ctx.State[StateKeySyncxState] = &syncx.Map[string, any]{}
14+
return true
15+
}

control/defaultengine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ func init() {
1515
// 防止自触发
1616
return ctx.Event.UserID != ctx.Event.SelfID || ctx.Event.PostType != "message"
1717
},
18+
addsyncxstate,
1819
)
1920
}

control/engine.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ func newengine(service string, prio int, o *ctrl.Options[*zero.Ctx]) (e *Engine)
4040
return ctx.Event.UserID != ctx.Event.SelfID || ctx.Event.PostType != "message"
4141
},
4242
newctrl(service, o),
43+
addsyncxstate,
4344
)
4445
e.prio = prio
4546
e.service = service

0 commit comments

Comments
 (0)