class Chantier::FailurePolicies::WithinInterval
Limits the number of failures that may be registered within the given interval
policy = Count.new(10) policy_within_interval = FailWithinTimePeriod.new(policy, 60 * 2) # 2 minutes policy_within_interval.limit_reached? # => false #... and then during 1 minute 5.times { policy_within_interval.failure! } policy_within_interval.limit_reached? # => true
Once the interval is passed, the error count will be reset back to 0.
Public Class Methods
new(policy, interval_in_seconds)
click to toggle source
# File lib/failure_policies.rb, line 108 def initialize(policy, interval_in_seconds) @policy = policy @interval = interval_in_seconds end
Public Instance Methods
arm!()
click to toggle source
# File lib/failure_policies.rb, line 118 def arm! @policy.arm! @interval_started = Time.now.utc.to_f @count = 0 end
failure!()
click to toggle source
# File lib/failure_policies.rb, line 124 def failure! interval_cutoff! @policy.failure! end
limit_reached?()
click to toggle source
# File lib/failure_policies.rb, line 129 def limit_reached? @policy.limit_reached? end
success!()
click to toggle source
# File lib/failure_policies.rb, line 113 def success! interval_cutoff! @policy.success! end
Private Instance Methods
interval_cutoff!()
click to toggle source
# File lib/failure_policies.rb, line 135 def interval_cutoff! t = Time.now.utc.to_f if (t - @interval_started) > @interval @interval_started = t @policy.arm! end end