algoliasearch 1.12.2 → 1.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6499852364a9df2e8315a4fd8dd39f990aeddd24
4
- data.tar.gz: f7adb2fa84f3c2ba9b777687776a6cb62c4606d1
3
+ metadata.gz: 4050df50fa8668d5c9c873c8783a195bd575dd6f
4
+ data.tar.gz: 214b9bc3a0afc498470fe6d21fab6a3893ea138d
5
5
  SHA512:
6
- metadata.gz: 2ea07f4064258fc0e80ba6d5f3aac45e0b53f7ab16f601c294d8919864639be0c49c5c270aad2311c04efd4772846cc2f669217baf3ef732a4002da4cdfb495e
7
- data.tar.gz: 1198f1d56a57cbfd8d00d2bc5644e9c762c6d55b403fae3b693fe85e2b7942f1eeb2b42ee7103a86c418512b5d03cd5c1f5559e75e5871a4768927f844138b0c
6
+ metadata.gz: d92b856c6c07c952637e11c4108c7057f844bc8a0eab04e53a3aa9a0200124036220e8808db600892d1e63062ea8147210cdd41df33abf8eb3ad07c98d550688
7
+ data.tar.gz: 534342154bf113bc6f65385c875907f0f90c48c464b73dbfb004372c3d3da57a67d5f3dd66f1c4c03eb97b065b85d371df8c3758cbaf1ff66cc7459a7559527c
data/ChangeLog CHANGED
@@ -1,5 +1,8 @@
1
1
  CHANGELOG
2
2
 
3
+ 2016-12-06
4
+ * Allow for multiple clients on different app ids on the same thread
5
+
3
6
  2016-12-05
4
7
  * Fix client scoped methods
5
8
 
@@ -41,6 +41,11 @@ module Algolia
41
41
  }
42
42
  end
43
43
 
44
+ def destroy
45
+ Thread.current["algolia_search_hosts_#{application_id}"] = nil
46
+ Thread.current["algolia_hosts_#{application_id}"] = nil
47
+ end
48
+
44
49
  #
45
50
  # Initialize a new index
46
51
  #
@@ -338,6 +343,10 @@ module Algolia
338
343
  send_timeout += 10 if i == 2
339
344
  receive_timeout += 10 if i == 2
340
345
 
346
+ thread_index_key = type != :write ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
347
+ Thread.current[thread_index_key] = host[:index]
348
+ host[:last_call] = Time.now.to_i
349
+
341
350
  host[:session].connect_timeout = connect_timeout
342
351
  host[:session].send_timeout = send_timeout
343
352
  host[:session].receive_timeout = receive_timeout
@@ -374,16 +383,29 @@ module Algolia
374
383
 
375
384
  # This method returns a thread-local array of sessions
376
385
  def thread_local_hosts(read)
377
- Thread.current[read ? :algolia_search_hosts : :algolia_hosts] ||= (read ? search_hosts : hosts).map do |host|
386
+ thread_hosts_key = read ? "algolia_search_hosts_#{application_id}" : "algolia_hosts_#{application_id}"
387
+ Thread.current[thread_hosts_key] ||= (read ? search_hosts : hosts).each_with_index.map do |host, i|
378
388
  client = HTTPClient.new
379
389
  client.ssl_config.ssl_version = @ssl_version if @ssl && @ssl_version
380
390
  client.transparent_gzip_decompression = true
381
391
  client.ssl_config.add_trust_ca File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'resources', 'ca-bundle.crt'))
382
392
  {
393
+ :index => i,
383
394
  :base_url => "http#{@ssl ? 's' : ''}://#{host}",
384
- :session => client
395
+ :session => client,
396
+ :last_call => nil
385
397
  }
386
398
  end
399
+ hosts = Thread.current[thread_hosts_key]
400
+ thread_index_key = read ? "algolia_search_host_index_#{application_id}" : "algolia_host_index_#{application_id}"
401
+ current_host = Thread.current[thread_index_key].to_i # `to_i` to ensure first call is 0
402
+ if current_host != 0 && hosts[current_host][:last_call].to_i < Time.now.to_i - 60
403
+ # the current_host is not the first one and we've been using it for less than a minute; continue doing so
404
+ first = hosts[current_host]
405
+ [first] + hosts.reject { |h| h[:index] == 0 || h == first } + hosts.select { |h| h[:index] == 0 }
406
+ else
407
+ hosts
408
+ end
387
409
  end
388
410
 
389
411
  def perform_request(session, url, method, data)
@@ -649,7 +671,8 @@ module Algolia
649
671
 
650
672
  # Used mostly for testing. Lets you delete the api key global vars.
651
673
  def Algolia.destroy
652
- @@client = Thread.current[:algolia_hosts] = Thread.current[:algolia_search_hosts] = nil
674
+ @@client.destroy unless @@client.nil?
675
+ @@client = nil
653
676
  self
654
677
  end
655
678
 
@@ -1,3 +1,3 @@
1
1
  module Algolia
2
- VERSION = "1.12.2"
2
+ VERSION = "1.12.3"
3
3
  end
@@ -997,6 +997,15 @@ describe 'Client' do
997
997
  @client.list_indexes # fallback on the second host after 5 sec (connection timeout)
998
998
  expect(start_time.to_i + 5).to be <= Time.now.to_i + 1
999
999
  end
1000
+
1001
+ it "should re-use the working (2nd) host after the 1st one failed" do
1002
+ start_time = Time.now
1003
+ @client.list_indexes # fallback on the second host after 5 sec (connection timeout)
1004
+ expect(start_time.to_i + 5).to be <= Time.now.to_i + 1
1005
+ start_time = Time.now
1006
+ @client.list_indexes # re-use the 2nd one
1007
+ expect(start_time.to_i).to be <= Time.now.to_i + 1
1008
+ end
1000
1009
  end
1001
1010
  end
1002
1011
 
@@ -7,7 +7,10 @@ describe 'With a mocked client' do
7
7
 
8
8
  before(:each) do
9
9
  WebMock.enable!
10
- Thread.current[:algolia_hosts] = Thread.current[:algolia_search_hosts] = nil # reset session objects
10
+ # reset session objects
11
+ app_id = Algolia.client.application_id
12
+ Thread.current["algolia_hosts_#{app_id}"] = nil
13
+ Thread.current["algolia_search_hosts_#{app_id}"] = nil
11
14
  end
12
15
 
13
16
  it "should add a simple object" do
@@ -6,7 +6,10 @@ describe 'With a rate limited client' do
6
6
 
7
7
  before(:each) do
8
8
  WebMock.enable!
9
- Thread.current[:algolia_hosts] = Thread.current[:algolia_search_hosts] = nil
9
+ # reset session objects
10
+ app_id = Algolia.client.application_id
11
+ Thread.current["algolia_hosts_#{app_id}"] = nil
12
+ Thread.current["algolia_search_hosts_#{app_id}"] = nil
10
13
  end
11
14
 
12
15
  it "should pass the right headers" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: algoliasearch
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.12.2
4
+ version: 1.12.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Algolia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-05 00:00:00.000000000 Z
11
+ date: 2016-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient