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

Use Chewy.thread_local_data to access Thread.current #803

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
16 changes: 11 additions & 5 deletions lib/chewy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ module Chewy
class << self
attr_accessor :adapters

# A thread-local variables accessor
# @return [Hash]
def thread_local_data
Thread.current[:chewy] ||= {}
end

# Derives an index for the passed string identifier if possible.
#
# @example
Expand All @@ -86,7 +92,7 @@ def derive_name(index_name)
# Main elasticsearch-ruby client instance
#
def client
Thread.current[:chewy_client] ||= begin
Chewy.thread_local_data[:chewy_client] ||= begin
client_configuration = configuration.deep_dup
client_configuration.delete(:prefix) # used by Chewy, not relevant to Elasticsearch::Client
block = client_configuration[:transport_options].try(:delete, :proc)
Expand Down Expand Up @@ -138,15 +144,15 @@ def massacre
# city3.do_update! # index updated again
#
def strategy(name = nil, &block)
Thread.current[:chewy_strategy] ||= Chewy::Strategy.new
Chewy.thread_local_data[:chewy_strategy] ||= Chewy::Strategy.new
if name
if block
Thread.current[:chewy_strategy].wrap name, &block
Chewy.thread_local_data[:chewy_strategy].wrap name, &block
else
Thread.current[:chewy_strategy].push name
Chewy.thread_local_data[:chewy_strategy].push name
end
else
Thread.current[:chewy_strategy]
Chewy.thread_local_data[:chewy_strategy]
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/chewy/runtime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Chewy
module Runtime
def self.version
Thread.current[:chewy_runtime_version] ||= Version.new(Chewy.client.info['version']['number'])
Chewy.thread_local_data[:chewy_runtime_version] ||= Version.new(Chewy.client.info['version']['number'])
end
end
end
2 changes: 1 addition & 1 deletion lib/chewy/search/scoping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module ClassMethods
#
# @return [Array<Chewy::Search::Request>] array of scopes
def scopes
Thread.current[:chewy_scopes] ||= []
Chewy.thread_local_data[:chewy_scopes] ||= []
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/chewy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@
end

describe '.client' do
let!(:initial_client) { Thread.current[:chewy_client] }
let!(:initial_client) { Chewy.thread_local_data[:chewy_client] }
let(:faraday_block) { proc {} }
let(:mock_client) { double(:client) }
let(:expected_client_config) { {transport_options: {}} }

before do
Thread.current[:chewy_client] = nil
Chewy.thread_local_data[:chewy_client] = nil
allow(Chewy).to receive_messages(configuration: {transport_options: {proc: faraday_block}})

allow(::Elasticsearch::Client).to receive(:new).with(expected_client_config) do |*_args, &passed_block|
Expand All @@ -70,7 +70,7 @@

its(:client) { is_expected.to eq(mock_client) }

after { Thread.current[:chewy_client] = initial_client }
after { Chewy.thread_local_data[:chewy_client] = initial_client }
end

describe '.create_indices' do
Expand Down