<commit_before>require "http_retriable/version"

module HttpRetriable

  extend self

  def retry_http(*args, &block)
    options = args.extract_options!
  
    retries = options.fetch(:retries, 5)
    sleep = options.fetch(:sleep, false)
    backoff = !sleep # if sleep is provided by the user, don't backoff
    exceptions = options.fetch(:exceptions, [
      RestClient::RequestTimeout,
      RestClient::ServerBrokeConnection,
      EOFError,
      Errno::ECONNREFUSED,
      Errno::ECONNRESET,
      Errno::EHOSTUNREACH,
      Errno::EINVAL,
      Errno::EPIPE,
      Errno::ETIMEDOUT,
      Net::HTTPBadResponse,
      Net::HTTPHeaderSyntaxError,
      Net::ProtocolError,
      SocketError,
      Timeout::Error])

    retried = 0
    seconds_to_sleep = sleep ? sleep : 2
    quick_retries = 2
    begin
      yield
    rescue *exceptions => e
      retried += 1
      if backoff
        if retried < quick_retries
          retry
        elsif retried < retries
          seconds_to_sleep = seconds_to_sleep ** 2
          sleep seconds_to_sleep
          retry
        else
          raise e
        end
      else
        if retried < retries
          sleep seconds_to_sleep
          retry
        else
          raise e
        end
      end
    end
  end

  def self.call(*args, &block)
    self.retry_http(args, &block)
  end
end
<commit_msg>Fixed self.call and removed non stdlib exceptions<commit_after>require "http_retriable/version"
module HttpRetriable

  extend self

  def retry_http(*args, &block)
    options = args.extract_options!
    default_exceptions = [
      EOFError,
      Errno::ECONNREFUSED,
      Errno::ECONNRESET,
      Errno::EHOSTUNREACH,
      Errno::EINVAL,
      Errno::EPIPE,
      Errno::ETIMEDOUT]
    if defined?(RestClient)
      default_exceptions += [RestClient::RequestTimeout, RestClient::ServerBrokeConnection]
    end

    retries = options.fetch(:retries, 5)
    should_sleep = options.fetch(:sleep, false)
    backoff = !should_sleep # if sleep is provided by the user, don't backoff
    exceptions = options.fetch(:exceptions, default_exceptions)

    retried = 0
    seconds_to_sleep = should_sleep ? should_sleep : 2
    quick_retries = 2
    begin
      yield
    rescue *exceptions => e
      retried += 1
      if backoff
        if retried < quick_retries
          retry
        elsif retried < retries
          seconds_to_sleep = seconds_to_sleep ** 2
          sleep seconds_to_sleep
          retry
        else
          raise e
        end
      else
        if retried < retries
          sleep seconds_to_sleep
          retry
        else
          raise e
        end
      end
    end
  end

  def self.call(*args, &block)
    self.retry_http(*args, &block)
  end
end

{'fraction_non_alphanumeric': 0.07361516034985423, 'fraction_numerical': 0.004373177842565598, 'mean_word_length': 2.1697459584295613, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '6848279', 'n_tokens_mistral': 852, 'n_tokens_neox': 825, 'n_words': 205}