def thread_local_hosts(read)
      thread_hosts_key = read ? "algolia_search_hosts_#{application_id}" : "algolia_hosts_#{application_id}"
      Thread.current[thread_hosts_key] ||= (read ? search_hosts : hosts).each_with_index.map do |host, i|
        client = HTTPClient.new
        client.ssl_config.ssl_version = @ssl_version if @ssl && @ssl_version
        client.transparent_gzip_decompression = @gzip
        client.ssl_config.add_trust_ca File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'resources', 'ca-bundle.crt'))
        {
          :index => i,
          :base_url => "http#{@ssl ? 's' : ''}://#{host}",
          :session => client,
          :last_call => nil
        }
      end
      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
      # we want to always target host 0 first
      # if the current host is not 0, then we want to use it first only if (we never used it OR we're using it since less than 1 minute)
      if current_host != 0 && (hosts[current_host][:last_call].nil? || hosts[current_host][:last_call] > Time.now.to_i - 60)
        # first host will be `current_host`
        first = hosts[current_host]
        [first] + hosts.reject { |h| h[:index] == 0 || h == first } + hosts.select { |h| h[:index] == 0 }
      else
        # first host will be `0`
        hosts
      end
    end
def thread_local_hosts(read)
      thread_hosts_key = read ? "algolia_search_hosts_#{application_id}" : "algolia_hosts_#{application_id}"
      Thread.current[thread_hosts_key] ||= (read ? search_hosts : hosts).each_with_index.map do |host, i|
        client = HTTPClient.new
        client.ssl_config.ssl_version = @ssl_version if @ssl && @ssl_version
        client.transparent_gzip_decompression = @gzip
        client.ssl_config.add_trust_ca File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'resources', 'ca-bundle.crt'))
        {
          :index => i,
          :base_url => "http#{@ssl ? 's' : ''}://#{host}",
          :session => client,
          :last_call => nil
        }
      end
      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
      # we want to always target host 0 first
      # if the current host is not 0, then we want to use it first only if (we never used it OR we're using it since less than 1 minute)
      if current_host != 0 && (hosts[current_host][:last_call].nil? || hosts[current_host][:last_call] > Time.now.to_i - 60)
        # first host will be `current_host`
        first = hosts[current_host]
        [first] + hosts.reject { |h| h[:index] == 0 || h == first } + hosts.select { |h| h[:index] == 0 }
      else
        # first host will be `0`
        hosts
      end
    end
This method returns a thread-local array of sessions