Class: Chantier::FailurePolicies::WithinInterval

Inherits:
None
  • Object
show all
Defined in:
lib/failure_policies.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(policy, interval_in_seconds) ⇒ WithinInterval

Returns a new instance of WithinInterval.



108
109
110
111
# File 'lib/failure_policies.rb', line 108

def initialize(policy, interval_in_seconds)
  @policy = policy
  @interval = interval_in_seconds
end

Instance Method Details

#arm!Object



118
119
120
121
122
# File 'lib/failure_policies.rb', line 118

def arm!
  @policy.arm!
  @interval_started = Time.now.utc.to_f
  @count = 0
end

#failure!Object



124
125
126
127
# File 'lib/failure_policies.rb', line 124

def failure!
  interval_cutoff!
  @policy.failure!
end

#limit_reached?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/failure_policies.rb', line 129

def limit_reached?
  @policy.limit_reached?
end

#success!Object



113
114
115
116
# File 'lib/failure_policies.rb', line 113

def success!
  interval_cutoff!
  @policy.success!
end