<commit_before>require 'elasticsearch/api'

module ElasticSearch
  class Client
    include ElasticSearch::API

    attr_accessor :current_server, :fetch_servers, :seed_servers, :servers, :refreshed_at, :refresh_period

    def initialize(args = {})
      args = defaults.merge(args)
      @fetch_servers = args[:fetch_servers] # Proc returning array of server strings
      @refresh_period = args[:refresh_period] # Seconds before refreshing list of servers
      @seed_servers = Array(args[:servers])
    end

    def defaults
      {
        refresh_period: 60,
        servers: ['http://127.0.0.1:9200']
      }
    end

    %w(get post put delete).each do |method|
      class_eval <<-EOC, __FILE__, __LINE__ + 1
        def #{method}(*args, &block)
          begin
            refresh_servers if should_refresh?
            response = connection.#{method}(*args, &block)
          rescue Exception => e
            case e
            when Faraday::Error::ConnectionFailed
              drop_current_server!
              raise ConnectionFailed, $!
            when Faraday::Error::TimeoutError
              drop_current_server!
              raise TimeoutError, $!
            else
              raise e
            end
          end
        end
      EOC
    end

    def refresh_servers
      @seed_servers = fetch_servers.shuffle
      @servers = @seed_servers.clone
      @current_server = @servers.first
      @refreshed_at = Time.now
      @connection = nil
    end

    def fetch_servers
      if @fetch_servers
        @fetch_servers.call
      else
        @seed_servers
      end
    end

    def should_refresh?
      return true unless servers
      return (servers.length == 0) || (Time.now > refreshed_at + refresh_period)
    end<commit_msg>Don't shuffle servers received from fetch_servers<commit_after>require 'elasticsearch/api'

module ElasticSearch
  class Client
    include ElasticSearch::API

    attr_accessor :current_server, :fetch_servers, :seed_servers, :servers, :refreshed_at, :refresh_period

    def initialize(args = {})
      args = defaults.merge(args)
      @fetch_servers = args[:fetch_servers] # Proc returning array of server strings
      @refresh_period = args[:refresh_period] # Seconds before refreshing list of servers
      @seed_servers = Array(args[:servers])
    end

    def defaults
      {
        refresh_period: 60,
        servers: ['http://127.0.0.1:9200']
      }
    end

    %w(get post put delete).each do |method|
      class_eval <<-EOC, __FILE__, __LINE__ + 1
        def #{method}(*args, &block)
          begin
            refresh_servers if should_refresh?
            response = connection.#{method}(*args, &block)
          rescue Exception => e
            case e
            when Faraday::Error::ConnectionFailed
              drop_current_server!
              raise ConnectionFailed, $!
            when Faraday::Error::TimeoutError
              drop_current_server!
              raise TimeoutError, $!
            else
              raise e
            end
          end
        end
      EOC
    end

    def refresh_servers
      @seed_servers = fetch_servers
      @servers = @seed_servers.clone
      @current_server = @servers.first
      @refreshed_at = Time.now
      @connection = nil
    end

    def fetch_servers
      if @fetch_servers
        @fetch_servers.call
      else
        @seed_servers
      end
    end

    def should_refresh?
      return true unless servers
      return (servers.length == 0) || (Time.now > refreshed_at + refresh_period)
    end
{'fraction_non_alphanumeric': 0.08159819921215532, 'fraction_numerical': 0.007878446820483961, 'mean_word_length': 2.1048034934497815, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '330654', 'n_tokens_mistral': 1004, 'n_tokens_neox': 1012, 'n_words': 252}