Skip to content

Commit

Permalink
fix(Doc): 修正偶发情况下帮助文档的map并发访问 (sealdice#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
PaienNate authored Oct 8, 2024
1 parent 1cbfb4e commit 62b30f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
25 changes: 19 additions & 6 deletions dice/builtin_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (d *Dice) registerCoreCommands() {
}

if id != "" {
text, exists := d.Parent.Help.TextMap[id]
text, exists := d.Parent.Help.TextMap.Load(id)
if exists {
content := d.Parent.Help.GetContent(text, 0)
ReplyToSender(ctx, msg, fmt.Sprintf("词条: %s:%s\n%s", text.PackageName, text.Title, content))
Expand Down Expand Up @@ -334,11 +334,21 @@ func (d *Dice) registerCoreCommands() {
}

hasSecond := len(search.Hits) >= 2
best := d.Parent.Help.TextMap[search.Hits[0].ID]
best, ok := d.Parent.Help.TextMap.Load(search.Hits[0].ID)
if !ok {
d.Logger.Errorf("加载d.Parent.Help.TextMap.Load(search.Hits[0].ID)->(%s)的数据出现错误!", search.Hits[0].ID)
ReplyToSender(ctx, msg, "未找到搜索结果,出现数据加载错误!")
return CmdExecuteResult{Matched: true, Solved: true}
}
others := ""

for _, i := range search.Hits {
t := d.Parent.Help.TextMap[i.ID]
t, ok := d.Parent.Help.TextMap.Load(i.ID)
if !ok {
d.Logger.Errorf("加载d.Parent.Help.TextMap.Load(search.Hits[0].ID)->(%s)的数据出现错误!", search.Hits[0].ID)
ReplyToSender(ctx, msg, "未找到搜索结果,出现数据加载错误!")
return CmdExecuteResult{Matched: true, Solved: true}
}
if t.Group != "" && t.Group != HelpBuiltinGroup {
others += fmt.Sprintf("[%s][%s]【%s:%s】 匹配度%.2f\n", i.ID, t.Group, t.PackageName, t.Title, i.Score)
} else {
Expand Down Expand Up @@ -456,9 +466,12 @@ func (d *Dice) registerCoreCommands() {
search, _, _, _, err := d.Parent.Help.Search(ctx, cmdArgs.CleanArgs, true, 1, 1, "")
if err == nil {
if len(search.Hits) > 0 {
// 居然会出现 hits[0] 为nil的情况??
// a := d.Parent.ShortHelp.GetContent(search.Hits[0].ID)
a := d.Parent.Help.TextMap[search.Hits[0].ID]
a, ok := d.Parent.Help.TextMap.Load(search.Hits[0].ID)
if !ok {
d.Logger.Error("HELPDOC:读取ID对应的信息出现问题")
ReplyToSender(ctx, msg, "HELPDOC:读取ID对应的信息出现问题")
return CmdExecuteResult{Matched: true, Solved: true}
}
content := d.Parent.Help.GetContent(a, 0)
ReplyToSender(ctx, msg, fmt.Sprintf("%s:%s\n%s", a.PackageName, a.Title, content))
} else {
Expand Down
32 changes: 17 additions & 15 deletions dice/dice_help.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (e HelpTextItems) Len() int {
type HelpManager struct {
CurID uint64
Index bleve.Index
TextMap map[string]*HelpTextItem
TextMap SyncMap[string, *HelpTextItem] // map[string]*HelpTextItem
Parent *DiceManager
EngineType int
batch *bleve.Batch
Expand Down Expand Up @@ -107,8 +107,6 @@ func (m *HelpManager) loadSearchEngine() {
m.EngineType = 1 // 默认0,bleve
}

m.TextMap = map[string]*HelpTextItem{}

// not bleve
if m.EngineType != 0 {
return
Expand Down Expand Up @@ -486,7 +484,7 @@ func (m *HelpManager) AddItem(item HelpTextItem) error {
}

id := m.GetNextID()
m.TextMap[id] = &item
m.TextMap.Store(id, &item)

if m.EngineType == 0 {
if m.batch == nil {
Expand Down Expand Up @@ -577,10 +575,11 @@ func (m *HelpManager) Search(ctx *MsgContext, text string, titleOnly bool, pageS
items := HelpTextItems{}
var idLst []string

for id, v := range m.TextMap {
m.TextMap.Range(func(id string, v *HelpTextItem) bool {
items = append(items, v)
idLst = append(idLst, id)
}
return true
})

hits := search.DocumentMatchCollection{}
matches := fuzzy.FindFrom(text, items)
Expand Down Expand Up @@ -674,14 +673,15 @@ func (m *HelpManager) GetContent(item *HelpTextItem, depth int) string {
formattedIdx = right
name := txt[left+1 : right-1]
matched := false
// 注意: 效率不高
for _, v := range m.TextMap {
// 注意: 效率更加不高
m.TextMap.Range(func(key string, v *HelpTextItem) bool {
if v.Title == name {
result.WriteString(m.GetContent(v, depth+1))
matched = true
break
return false
}
}
return true
})
if !matched {
result.WriteByte('{')
result.WriteString(name)
Expand Down Expand Up @@ -923,8 +923,8 @@ func (m *HelpManager) GetHelpItemPage(pageNum, pageSize int, id, group, from, ti
}

if id != "" {
item := m.TextMap[id]
if item != nil &&
item, ok := m.TextMap.Load(id)
if ok &&
strings.Contains(item.Group, group) &&
strings.Contains(item.From, from) &&
strings.Contains(item.Title, title) {
Expand All @@ -941,8 +941,8 @@ func (m *HelpManager) GetHelpItemPage(pageNum, pageSize int, id, group, from, ti
}
return 0, HelpTextVos{}
}
temp := make(HelpTextVos, 0, len(m.TextMap))
for i, item := range m.TextMap {
temp := make(HelpTextVos, 0, m.TextMap.Len())
m.TextMap.Range(func(i string, item *HelpTextItem) bool {
if strings.Contains(item.Group, group) &&
strings.Contains(item.From, from) &&
strings.Contains(item.Title, title) {
Expand All @@ -957,7 +957,9 @@ func (m *HelpManager) GetHelpItemPage(pageNum, pageSize int, id, group, from, ti
vo.ID, _ = strconv.Atoi(i)
temp = append(temp, vo)
}
}
return true
})

sort.Sort(temp)

start := (pageNum - 1) * pageSize
Expand Down

0 comments on commit 62b30f0

Please sign in to comment.