<commit_before>module Resqorn
  class Backoff
    def initialize(options = {})
      @time = options.fetch(:time) { Time }
    end

    # Public: Tell backoff that the thing we might want to back off from just started.
    def started
      check!
      @backoff_duration = @backing_off ? [@backoff_duration * 2, 64.0].min : 1.0
      @last_started_at = @time.now
      @backing_off = false
    end

    # Public: Check if we should wait before starting again.
    def wait?
      ! ok?
    end

    # Public: Check if we are ok to start (i.e. we don't need to back off).
    def ok?
      check!
      how_long?.nil?
    end

    # Public: How much longer until `ok?` will be true?
    def how_long?
      check!
      if @last_started_at && @backoff_duration
        answer = @backoff_duration - (@time.now - @last_started_at)
        if answer > 0.0
          answer
        else
          nil
        end
      else
        nil
      end
    end

    private

    # Private: Check the current state.
    def check!
      if @last_started_at && @backoff_duration && !@backing_off
        @backing_off = (@time.now - @last_started_at) < @backoff_duration
      end
    end
  end
end
<commit_msg>Refactor.<commit_after>module Resqorn
  class Backoff
    def initialize(options = {})
      @time = options.fetch(:time) { Time }
    end

    # Public: Tell backoff that the thing we might want to back off from just started.
    def started
      check!
      @backoff_duration = @backing_off ? [@backoff_duration * 2, 64.0].min : 1.0
      @last_started_at = @time.now
      @backing_off = false
    end

    # Public: Check if we should wait before starting again.
    def wait?
      ! ok?
    end

    # Public: Check if we are ok to start (i.e. we don't need to back off).
    def ok?
      check!
      how_long?.nil?
    end

    # Public: How much longer until `ok?` will be true?
    def how_long?
      check!
      next_start_at > @time.now ? next_start_at - @time.now : nil
    end

    private

    # Private: Check the current state.
    def check!
      @backing_off ||= next_start_at > @time.now
    end

    # Private: Get the time when we can start again.
    def next_start_at
      (@last_started_at && @backoff_duration) ? (@last_started_at + @backoff_duration) : @time.now
    end
  end
end

{'fraction_non_alphanumeric': 0.08953287197231834, 'fraction_numerical': 0.006055363321799308, 'mean_word_length': 2.1383989145183175, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '1422085', 'n_tokens_mistral': 770, 'n_tokens_neox': 737, 'n_words': 243}