Skip to content

Commit

Permalink
imp: 减少自定义回复很多的骰子的内存申请量 (#1010)
Browse files Browse the repository at this point in the history
  • Loading branch information
fy0 authored Aug 17, 2024
1 parent 6f2766e commit 7f09d4c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
24 changes: 22 additions & 2 deletions dice/ext_reply_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,26 @@ func (m *ReplyConditionTextMatch) Clean() {
m.Value = strings.TrimSpace(m.Value)
}

type replyRegexCacheType struct {
cache SyncMap[string, *regexp.Regexp]
}

func (r *replyRegexCacheType) compile(expr string) *regexp.Regexp {
if re, ok := r.cache.Load(expr); ok {
return re
}

if ret, err := regexp.Compile(expr); err == nil {
r.cache.Store(expr, ret)
return ret
} else {
r.cache.Store(expr, nil)
return nil
}
}

var replyRegexCache replyRegexCacheType

func (m *ReplyConditionTextMatch) Check(ctx *MsgContext, _ *Message, _ *CmdArgs, cleanText string) bool {
var ret bool
switch m.MatchType {
Expand All @@ -80,8 +100,8 @@ func (m *ReplyConditionTextMatch) Check(ctx *MsgContext, _ *Message, _ *CmdArgs,
case "matchSuffix":
ret = strings.HasSuffix(strings.ToLower(cleanText), strings.ToLower(m.Value))
case "matchRegex":
re, err := regexp.Compile(m.Value)
if err == nil {
re := replyRegexCache.compile(m.Value)
if re != nil {
lst := re.FindStringSubmatch(cleanText)
gName := re.SubexpNames()
for index, s := range lst {
Expand Down
2 changes: 1 addition & 1 deletion dice/im_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,7 @@ func (s *IMSession) OnGroupMemberJoined(ctx *MsgContext, msg *Message) {
// 借助类似操作系统信号量的思路来做一个互斥锁
var muxAutoQuit sync.Mutex
var groupLeaveNum int
var platformRE = regexp.MustCompile(`^(.*)-Group:`)

// LongTimeQuitInactiveGroup 另一种退群方案,其中minute代表间隔多久执行一次,num代表一次退几个群(每次退群之间有10秒的等待时间)
func (s *IMSession) LongTimeQuitInactiveGroup(threshold, hint time.Time, roundIntervalMinute int, groupsPerRound int) {
Expand All @@ -1386,7 +1387,6 @@ func (s *IMSession) LongTimeQuitInactiveGroup(threshold, hint time.Time, roundIn
defer muxAutoQuit.Unlock()

groupLeaveNum = 0
platformRE := regexp.MustCompile(`^(.*)-Group:`)
selectedGroupEndpoints := []*GroupEndpointPair{} // 创建一个存放 grp 和 ep 组合的切片

// Pinenutn: Range模板 ServiceAtNew重构代码
Expand Down

0 comments on commit 7f09d4c

Please sign in to comment.