Skip to content

Commit

Permalink
fix: add lock
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored and robinv8 committed Sep 4, 2024
1 parent b75662b commit c3c3670
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
4 changes: 4 additions & 0 deletions search-elasticsearch/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"fmt"
"github.com/apache/incubator-answer-plugins/util"
"strings"
"sync"

"github.com/apache/incubator-answer-plugins/search-elasticsearch/i18n"
"github.com/apache/incubator-answer/plugin"
Expand All @@ -40,6 +41,8 @@ type SearchEngine struct {
Config *SearchEngineConfig
Operator *Operator
syncer plugin.SearchSyncer
syncing bool
lock sync.Mutex
}

type SearchEngineConfig struct {
Expand All @@ -51,6 +54,7 @@ type SearchEngineConfig struct {
func init() {
plugin.Register(&SearchEngine{
Config: &SearchEngineConfig{},
lock: sync.Mutex{},
})
}

Expand Down
14 changes: 14 additions & 0 deletions search-elasticsearch/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,20 @@ import (

func (s *SearchEngine) sync() {
var page, pageSize = 1, 100
if s.syncing {
log.Warnf("es: syncing is running, skip")
return
}

go func() {
s.lock.Lock()
defer s.lock.Unlock()
if s.syncing {
log.Warnf("es: syncing is running, skip")
return
}

s.syncing = true
log.Info("es: start sync questions...")
page = 1
for {
Expand Down Expand Up @@ -68,6 +81,7 @@ func (s *SearchEngine) sync() {

page += 1
}
s.syncing = false
log.Info("es: sync done")
}()
}
Expand Down

0 comments on commit c3c3670

Please sign in to comment.