Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix concurrency write map panic #338

Merged
merged 4 commits into from
Dec 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package config

import (
"github.com/BurntSushi/toml"
"sync"
)

const (
Expand Down Expand Up @@ -134,6 +135,17 @@ type RecommendConfig struct {
EnableItemBasedRecommend bool `toml:"enable_item_based_recommend"`
EnableColRecommend bool `toml:"enable_collaborative_recommend"`
EnableClickThroughPrediction bool `toml:"enable_click_through_prediction"`
exploreRecommendLock *sync.Mutex
zhangzhenghao marked this conversation as resolved.
Show resolved Hide resolved
}

func (config *RecommendConfig) GetExploreRecommend(key string) (value float64, exist bool) {
if config == nil {
return 0.0, false
}
config.exploreRecommendLock.Lock()
defer config.exploreRecommendLock.Unlock()
value, exist = config.ExploreRecommend[key]
return
}

// LoadDefaultIfNil loads default settings if config is nil.
Expand All @@ -155,6 +167,7 @@ func (config *RecommendConfig) LoadDefaultIfNil() *RecommendConfig {
EnableItemBasedRecommend: false,
EnableColRecommend: true,
EnableClickThroughPrediction: false,
exploreRecommendLock: &sync.Mutex{},
}
}
return config
Expand Down Expand Up @@ -282,6 +295,7 @@ func (config *Config) FillDefault(meta toml.MetaData) {
if !meta.IsDefined("recommend", "enable_click_through_prediction") {
config.Recommend.EnableClickThroughPrediction = defaultRecommendConfig.EnableClickThroughPrediction
}
config.Recommend.exploreRecommendLock = &sync.Mutex{}
}

// LoadConfig loads configuration from toml file.
Expand Down
8 changes: 8 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ func TestLoadConfig(t *testing.T) {
assert.Equal(t, 1, config.Recommend.RefreshRecommendPeriod)
assert.Equal(t, []string{"item_based", "latest"}, config.Recommend.FallbackRecommend)
assert.Equal(t, map[string]float64{"popular": 0.1, "latest": 0.2}, config.Recommend.ExploreRecommend)
value, exist := config.Recommend.GetExploreRecommend("popular")
assert.Equal(t, true, exist)
assert.Equal(t, 0.1, value)
value, exist = config.Recommend.GetExploreRecommend("latest")
assert.Equal(t, true, exist)
assert.Equal(t, 0.2, value)
_, exist = config.Recommend.GetExploreRecommend("unknown")
assert.Equal(t, false, exist)
assert.Equal(t, "similar", config.Recommend.ItemNeighborType)
assert.Equal(t, "similar", config.Recommend.UserNeighborType)
assert.True(t, config.Recommend.EnableColRecommend)
Expand Down
4 changes: 2 additions & 2 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,11 +713,11 @@ func (w *Worker) exploreRecommend(exploitRecommend []cache.Scored, excludeSet *s
localExcludeSet := excludeSet.Copy()
// create thresholds
explorePopularThreshold := 0.0
if threshold, exist := w.cfg.Recommend.ExploreRecommend["popular"]; exist {
if threshold, exist := w.cfg.Recommend.GetExploreRecommend("popular"); exist {
explorePopularThreshold = threshold
}
exploreLatestThreshold := explorePopularThreshold
if threshold, exist := w.cfg.Recommend.ExploreRecommend["latest"]; exist {
if threshold, exist := w.cfg.Recommend.GetExploreRecommend("latest"); exist {
exploreLatestThreshold += threshold
}
// load popular items
Expand Down