huacnlee / rails-settings-cached

Global settings for your Rails application.
Other
1.06k stars 202 forks source link

fix bug: thread safe problem #126

Closed easyhappy closed 5 years ago

easyhappy commented 7 years ago

主要是修复 线程安全问题

测试代码:

def test 
  user_one = User.find(1)
  user_two = User.find(1)
  items_1 = []

  a = Thread.new do
    puts "thread 1 start #{Time.now.strftime("%H:%M:%S.%N")}"
    10000.times.each do 
      s = candao.settings
      items_1 << s.where(:var => "a").to_sql
    end
    puts "thread 1 end #{Time.now.strftime("%H:%M:%S.%N")}"
  end

  items_2 = []
  b = Thread.new do
    puts "thread 2 start #{Time.now.strftime("%H:%M:%S.%N")}"
    10000.times.each do 
      s = wangdiantong.settings
      items_2 << s.where(:var => "a").to_sql
    end
    puts "thread 2 end #{Time.now.strftime("%H:%M:%S.%N")}"
  end
  a.join; b.join
  return [items_1, items_2]
end

items_1, items_2 = test

puts items_1.uniq;nil
puts items_2.uniq;nil

修复前: 运行代码后 会发现 items_1.uniq 和 items_2.uniq 都会包含

"SELECT \"settings\".* FROM \"settings\" WHERE \"settings\".\"thing_type\" = 'User' AND \"settings\".\"thing_id\" = 1 AND \"settings\".\"var\" = 'a'"

"SELECT \"settings\".* FROM \"settings\" WHERE \"settings\".\"thing_type\" = 'User' AND \"settings\".\"thing_id\" =2 AND \"settings\".\"var\" = 'a'"

修复后: 就不会交叉到一起了

codecov[bot] commented 7 years ago

Codecov Report

Merging #126 into master will decrease coverage by 21.94%. The diff coverage is 58.33%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master     #126       +/-   ##
===========================================
- Coverage   97.61%   75.66%   -21.95%     
===========================================
  Files          11       11               
  Lines         335      337        +2     
===========================================
- Hits          327      255       -72     
- Misses          8       82       +74
Impacted Files Coverage Δ
lib/rails-settings/scoped_settings.rb 75% <33.33%> (-25%) :arrow_down:
lib/rails-settings/settings.rb 77.61% <66.66%> (-20.88%) :arrow_down:
spec/rails-settings-cached/default_setting_spec.rb 63.41% <0%> (-36.59%) :arrow_down:
lib/rails-settings/base.rb 73.52% <0%> (-26.48%) :arrow_down:
lib/rails-settings/default.rb 71.42% <0%> (-25%) :arrow_down:
spec/rails-settings-cached/setting_spec.rb 77.86% <0%> (-22.14%) :arrow_down:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 848baa6...d522866. Read the comment docs.