diff --git a/config/config.go b/config/config.go index 76b03b425..6ed4c8957 100644 --- a/config/config.go +++ b/config/config.go @@ -16,6 +16,7 @@ package config import ( "github.com/BurntSushi/toml" + "sync" ) const ( @@ -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 +} + +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. @@ -155,6 +167,7 @@ func (config *RecommendConfig) LoadDefaultIfNil() *RecommendConfig { EnableItemBasedRecommend: false, EnableColRecommend: true, EnableClickThroughPrediction: false, + exploreRecommendLock: sync.Mutex{}, } } return config @@ -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. diff --git a/config/config_test.go b/config/config_test.go index c7216ae80..0f5147028 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) @@ -67,9 +75,9 @@ func TestLoadConfig(t *testing.T) { } func TestConfig_FillDefault(t *testing.T) { - var config Config + var config *Config meta, err := toml.Decode("", &config) assert.NoError(t, err) config.FillDefault(meta) - assert.Equal(t, *(*Config)(nil).LoadDefaultIfNil(), config) + assert.Equal(t, (*Config)(nil).LoadDefaultIfNil(), config) } diff --git a/worker/worker.go b/worker/worker.go index d7a50d9f2..b63899d07 100644 --- a/worker/worker.go +++ b/worker/worker.go @@ -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