Class: CB2::RollingWindow
- Inherits:
-
Object
- Object
- CB2::RollingWindow
- Defined in:
- lib/strategies/rolling_window.rb
Instance Attribute Summary collapse
-
#duration ⇒ Object
Returns the value of attribute duration.
-
#redis ⇒ Object
Returns the value of attribute redis.
-
#reenable_after ⇒ Object
Returns the value of attribute reenable_after.
-
#service ⇒ Object
Returns the value of attribute service.
-
#threshold ⇒ Object
Returns the value of attribute threshold.
Instance Method Summary collapse
-
#initialize(options) ⇒ RollingWindow
constructor
A new instance of RollingWindow.
- #open! ⇒ Object
- #open? ⇒ Boolean
- #process ⇒ Object
Constructor Details
#initialize(options) ⇒ RollingWindow
Returns a new instance of RollingWindow.
4 5 6 7 8 9 10 |
# File 'lib/strategies/rolling_window.rb', line 4 def initialize() @service = .fetch(:service) @duration = .fetch(:duration) @threshold = .fetch(:threshold) @reenable_after = .fetch(:reenable_after) @redis = Redis.current end |
Instance Attribute Details
#duration ⇒ Object
Returns the value of attribute duration.
2 3 4 |
# File 'lib/strategies/rolling_window.rb', line 2 def duration @duration end |
#redis ⇒ Object
Returns the value of attribute redis.
2 3 4 |
# File 'lib/strategies/rolling_window.rb', line 2 def redis @redis end |
#reenable_after ⇒ Object
Returns the value of attribute reenable_after.
2 3 4 |
# File 'lib/strategies/rolling_window.rb', line 2 def reenable_after @reenable_after end |
#service ⇒ Object
Returns the value of attribute service.
2 3 4 |
# File 'lib/strategies/rolling_window.rb', line 2 def service @service end |
#threshold ⇒ Object
Returns the value of attribute threshold.
2 3 4 |
# File 'lib/strategies/rolling_window.rb', line 2 def threshold @threshold end |
Instance Method Details
#open! ⇒ Object
16 17 18 |
# File 'lib/strategies/rolling_window.rb', line 16 def open! redis.setex(cache_key, reenable_after, 1) end |
#open? ⇒ Boolean
12 13 14 |
# File 'lib/strategies/rolling_window.rb', line 12 def open? redis.exists(cache_key) end |
#process ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/strategies/rolling_window.rb', line 20 def process key = "circuit-breaker-count-#{service}" t = Time.now.to_i pipeline = redis.pipelined do # keep the sorted set clean redis.zremrangebyscore(key, "-inf", t - duration) # add as a random uuid because sorted sets won't take duplicate items: redis.zadd(key, t, SecureRandom.uuid) # just count how many errors are left in the set redis.zcard(key) end if pipeline.last >= threshold open! end end |