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

Update fallback host logic to reflect comment #162

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/algolia/client.rb
Expand Up @@ -399,7 +399,7 @@ def thread_local_hosts(read)
hosts = Thread.current[thread_hosts_key]
thread_index_key = read ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
current_host = Thread.current[thread_index_key].to_i # `to_i` to ensure first call is 0
if current_host != 0 && hosts[current_host][:last_call].to_i < Time.now.to_i - 60
if current_host != 0 && hosts[0][:last_call].to_i > Time.now.to_i - 60
# the current_host is not the first one and we've been using it for less than a minute; continue doing so
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, the < is wrong, but the current_host is correct.

Scenario:

  • do a call
  • it targets the host 0 and works
  • do a call
  • it targets the host 0 and fails
  • we retry with host 1 and store current_host = 1, and works
  • do a call
  • we're on host 1, and it has been less than a minute that we've been using the host 1: aka hosts[current_host][:last_call].to_i > Time.now.to_i - 60
  • it targets host 1, and works
  • wait 1 min
  • do a call
  • we're on host 1, and it has been MORE than a minute that we've been using the host 1
  • return the regular hosts array, targets host 0 and works

first = hosts[current_host]
[first] + hosts.reject { |h| h[:index] == 0 || h == first } + hosts.select { |h| h[:index] == 0 }
Expand Down
4 changes: 2 additions & 2 deletions spec/client_spec.rb
Expand Up @@ -979,7 +979,7 @@ def test_browse(expected, *args)
end

context 'DNS timeout' do
before(:all) do
before(:each) do
app_id = ENV['ALGOLIA_APPLICATION_ID']
Thread.current["algolia_hosts_#{app_id}"] = nil
Thread.current["algolia_search_hosts_#{app_id}"] = nil
Expand Down Expand Up @@ -1008,7 +1008,7 @@ def test_browse(expected, *args)
expect(start_time.to_i + 5).to be <= Time.now.to_i + 1
start_time = Time.now
@client.list_indexes # re-use the 2nd one
expect(start_time.to_i).to be <= Time.now.to_i + 1
expect(start_time.to_i).to be >= Time.now.to_i - 1
end
end

Expand Down