<commit_before>require "timeout"
require "retriable/config"
require "retriable/exponential_backoff"
require "retriable/version"

module Retriable
  extend self

  attr_reader :config

  def self.configure
    yield(config)
  end

  def config
    @config ||= Config.new
  end

  def retry(
    max_tries:         config.max_tries,
    base_interval:     config.base_interval,
    max_interval:      config.max_interval,
    rand_factor:       config.rand_factor,
    multiplier:        config.multiplier,
    max_elapsed_time:  config.max_elapsed_time,
    intervals:         config.intervals,
    timeout:           config.timeout,
    on:                config.on,
    on_retry:          config.on_retry
    )

    start_time = Time.now
    elapsed_time = -> { Time.now - start_time }

    max_tries = intervals.size if intervals
    intervals ||= ExponentialBackoff.new(
      max_tries: max_tries,
      base_interval: base_interval,
      multiplier: multiplier,
      max_interval: max_interval,
      rand_factor: rand_factor
    ).intervals

    intervals.each.with_index(1) do |interval, attempt|
      begin
        if timeout
          Timeout::timeout(timeout) { return yield(attempt) }
        else
          return yield(attempt)
        end
      rescue *[*on] => exception
        on_retry.call(exception, attempt, Time.now - start_time, interval) if on_retry
        raise if attempt >= max_tries || (elapsed_time.call + interval) > max_elapsed_time
        sleep interval if config.sleep_disabled != true
      end
    end
  end
end<commit_msg>use elased_time.call in on_retry call<commit_after>require "timeout"
require "retriable/config"
require "retriable/exponential_backoff"
require "retriable/version"

module Retriable
  extend self

  attr_reader :config

  def self.configure
    yield(config)
  end

  def config
    @config ||= Config.new
  end

  def retry(
    max_tries:         config.max_tries,
    base_interval:     config.base_interval,
    max_interval:      config.max_interval,
    rand_factor:       config.rand_factor,
    multiplier:        config.multiplier,
    max_elapsed_time:  config.max_elapsed_time,
    intervals:         config.intervals,
    timeout:           config.timeout,
    on:                config.on,
    on_retry:          config.on_retry
    )

    start_time = Time.now
    elapsed_time = -> { Time.now - start_time }

    max_tries = intervals.size if intervals
    intervals ||= ExponentialBackoff.new(
      max_tries: max_tries,
      base_interval: base_interval,
      multiplier: multiplier,
      max_interval: max_interval,
      rand_factor: rand_factor
    ).intervals

    intervals.each.with_index(1) do |interval, attempt|
      begin
        if timeout
          Timeout::timeout(timeout) { return yield(attempt) }
        else
          return yield(attempt)
        end
      rescue *[*on] => exception
        on_retry.call(exception, attempt, elapsed_time.call, interval) if on_retry
        raise if attempt >= max_tries || (elapsed_time.call + interval) > max_elapsed_time
        sleep interval if config.sleep_disabled != true
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07888040712468193, 'fraction_numerical': 0.0006361323155216285, 'mean_word_length': 2.5061315496098104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '2921776', 'n_tokens_mistral': 967, 'n_tokens_neox': 962, 'n_words': 216}