<commit_before>module ExceptionalMiddleware::Middleware::CircuitBreaker
  def self.included(includer)
    includer.extend(ClassMethods)
  end

  module ClassMethods
    def halt?(remote_exception)
      raise NotImplementedError, "#{__method__} must be implemented by including class!"
    end

    def wrap(successor)
      lambda do |remote_exception|
        successor.call(remote_exception) unless halt?(remote_exception)
        nil
      end
    end
  end
end<commit_msg>Add CircuitBreakerMiddleware with tests.<commit_after># Adds circuit breaker like behavior to the middleware stack allowing for
# additional exception handling to be short-circuited. In situations where a lot
# of exceptions are occuring this type of behavior can be useful to prevent
# futile retries.
module ExceptionalMiddleware::Middleware::CircuitBreaker
  def self.included(includer)
    includer.extend(ClassMethods)
  end

  # Behaviors that the including Module or Class should be extended with.
  module ClassMethods
    # Indicates if the circuit breaker has been tripped and whether execution
    # should be halted or whether the successor should be invoked instead. Must
    # be implemeneted by including Class/Module.
    #
    # @param remote_exception [RemotelyExceptional::RemoteException] The remote
    #   exception that needs handling.
    # @raise [NotImplementedError] Raised if the incluing Class or Module does
    #   not implement its own halt? method.
    # @return [Boolean] A Boolean value indicating whether or not the successor
    #   should be invoked. When true, the successor method will not be invoked.
    #   When false, the successor will be invoked.
    def halt?(remote_exception)
      raise NotImplementedError, "#{__method__} must be implemented by including class!"
    end

    # Wraps the given successor method in a Proc that adds circuit breaking
    # logic such that the successor is invoked when the {#halt?} method returns
    # false and is not invoked when the {#halt?} method returns true.
    #
    # @param successor [#call] The successor Object that should be invoked
    #   unless told to halt.
    # @return [Proc] A new Proc with circuit breaking behavior.
    def wrap(successor)
      lambda do |remote_exception|
        successor.call(remote_exception) unless halt?(remote_exception)
        nil
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.056540084388185655, 'fraction_numerical': 0.0, 'mean_word_length': 3.61284046692607, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2497309', 'n_tokens_mistral': 623, 'n_tokens_neox': 608, 'n_words': 258}