<commit_before>require "breaker/version"
require 'timeout'

module Breaker
  CircuitOpenError = Class.new RuntimeError

  Fuse = Struct.new :name, :state, :failure_threshold, :retry_timeout, :timeout, :failure_count, :retry_threshold do
    def initialize(*args)
      super
      self.state = :closed if state.nil?
      self.failure_count = 0 if failure_count.nil?
    end
  end

  class << self
    def circuit(name, options = {})
      fuse = repo.upsert({
        name: name,
        failure_threshold: options.fetch(:failure_threshold, 10),
        retry_timeout: options.fetch(:retry_timeout, 60),
        timeout: options.fetch(:timeout, 5)
      })

      circuit = Circuit.new fuse

      if block_given?
        circuit.run do
          yield
        end
      end

      circuit
    end

    def repo
      @repo
    end

    def repo=(repo)
      @repo = repo
    end
  end

  class Circuit
    attr_accessor :fuse

    def initialize(fuse, options = {})
      @fuse = fuse
    end

    def open(clock = Time.now)
      fuse.failure_count = 1
      fuse.state = :open
      fuse.retry_threshold = clock + retry_timeout
    end

    def close
      fuse.failure_count = 0
      fuse.state = :closed
      fuse.retry_threshold = nil
    end

    def open?
      fuse.state == :open
    end

    def closed?
      fuse.state == :closed
    end

    def retry_timeout
      fuse.retry_timeout
    end

    def timeout
      fuse.timeout
    end

    def run(clock = Time.now)
      if closed? || half_open?(clock)
        begin
          result = Timeout.timeout timeout do
            yield
          end

          if half_open?(clock)
            close
          end<commit_msg>Alias query methods for easier understanding<commit_after>require "breaker/version"
require 'timeout'

module Breaker
  CircuitOpenError = Class.new RuntimeError

  Fuse = Struct.new :name, :state, :failure_threshold, :retry_timeout, :timeout, :failure_count, :retry_threshold do
    def initialize(*args)
      super
      self.state = :closed if state.nil?
      self.failure_count = 0 if failure_count.nil?
    end
  end

  class << self
    def circuit(name, options = {})
      fuse = repo.upsert({
        name: name,
        failure_threshold: options.fetch(:failure_threshold, 10),
        retry_timeout: options.fetch(:retry_timeout, 60),
        timeout: options.fetch(:timeout, 5)
      })

      circuit = Circuit.new fuse

      if block_given?
        circuit.run do
          yield
        end
      end

      circuit
    end

    def repo
      @repo
    end

    def repo=(repo)
      @repo = repo
    end
  end

  class Circuit
    attr_accessor :fuse

    def initialize(fuse, options = {})
      @fuse = fuse
    end

    def open(clock = Time.now)
      fuse.failure_count = 1
      fuse.state = :open
      fuse.retry_threshold = clock + retry_timeout
    end

    def close
      fuse.failure_count = 0
      fuse.state = :closed
      fuse.retry_threshold = nil
    end

    def open?
      fuse.state == :open
    end
    alias down? open?

    def closed?
      fuse.state == :closed
    end
    alias up? closed?

    def retry_timeout
      fuse.retry_timeout
    end

    def timeout
      fuse.timeout
    end

    def run(clock = Time.now)
      if closed? || half_open?(clock)
        begin
          result = Timeout.timeout timeout do
            yield
          end

          if half_open?(clock)
            close
          end
{'fraction_non_alphanumeric': 0.08104196816208394, 'fraction_numerical': 0.004630969609261939, 'mean_word_length': 1.9139966273187183, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '1821241', 'n_tokens_mistral': 1082, 'n_tokens_neox': 1041, 'n_words': 284}