# -*- ruby -*-

breaker = Milter::Manager::Breaker.new

stress = Object.new
stress.instance_eval do
  @breaker = breaker
end

class << stress
  def threshold_n_connections
    @breaker.threshold_n_connections
  end

  def threshold_n_connections=(n)
    @breaker.threshold_n_connections = n
  end
end

singleton_class = class << self; self; end
singleton_class.send(:define_method, :stress) do
  stress
end

define_applicable_condition("Stress Notify") do |condition|
  condition.description = "Define stress=yes macro when stress situation"

  condition.define_connect_stopper do |context, host, address|
    context["stress"] = "yes" if breaker.stressing?(context)
    false
  end
end

define_applicable_condition("No Stress") do |condition|
  condition.description = "Apply milter only when normal condition"

  condition.define_connect_stopper do |context, host, address|
    breaker.stressing?(context)
  end
end
