Skip to content

Commit

Permalink
make whitelist thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
flyerhzm committed Feb 29, 2020
1 parent 656b225 commit 9cda9c2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 10 deletions.
17 changes: 8 additions & 9 deletions lib/bullet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class << self
:stacktrace_includes,
:stacktrace_excludes,
:skip_html_injection
attr_reader :whitelist
attr_accessor :add_footer, :orm_patches_applied

available_notifiers = UniformNotifier::AVAILABLE_NOTIFIERS.map { |notifier| "#{notifier}=" }
Expand Down Expand Up @@ -98,27 +97,27 @@ def stacktrace_excludes

def add_whitelist(options)
reset_whitelist
@whitelist[options[:type]][options[:class_name]] ||= []
@whitelist[options[:type]][options[:class_name]] << options[:association].to_sym
Thread.current[:whitelist][options[:type]][options[:class_name]] ||= []
Thread.current[:whitelist][options[:type]][options[:class_name]] << options[:association].to_sym
end

def delete_whitelist(options)
reset_whitelist
@whitelist[options[:type]][options[:class_name]] ||= []
@whitelist[options[:type]][options[:class_name]].delete(options[:association].to_sym)
@whitelist[options[:type]].delete_if { |_key, val| val.empty? }
Thread.current[:whitelist][options[:type]][options[:class_name]] ||= []
Thread.current[:whitelist][options[:type]][options[:class_name]].delete(options[:association].to_sym)
Thread.current[:whitelist][options[:type]].delete_if { |_key, val| val.empty? }
end

def get_whitelist_associations(type, class_name)
Array(@whitelist[type][class_name])
Array(Thread.current[:whitelist][type][class_name])
end

def reset_whitelist
@whitelist ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
Thread.current[:whitelist] ||= { n_plus_one_query: {}, unused_eager_loading: {}, counter_cache: {} }
end

def clear_whitelist
@whitelist = nil
Thread.current[:whitelist] = nil
end

def bullet_logger=(active)
Expand Down
2 changes: 1 addition & 1 deletion spec/bullet_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
it 'is deleted from the whitelist successfully' do
Bullet.add_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
Bullet.delete_whitelist(type: :n_plus_one_query, class_name: 'Klass', association: :department)
expect(Bullet.whitelist[:n_plus_one_query]).to eq({})
expect(Thread.current[:whitelist][:n_plus_one_query]).to eq({})
end
end

Expand Down

0 comments on commit 9cda9c2

Please sign in to comment.