Class: Prop::IntervalStrategy
- Inherits:
-
Object
- Object
- Prop::IntervalStrategy
- Defined in:
- lib/prop/interval_strategy.rb
Class Method Summary collapse
- .at_threshold?(counter, options) ⇒ Boolean
-
.build(options) ⇒ Object
Builds the expiring cache key.
- .counter(cache_key, options) ⇒ Object
- .increment(cache_key, options, counter) ⇒ Object
- .reset(cache_key) ⇒ Object
- .threshold_reached(options) ⇒ Object
- .validate_options!(options) ⇒ Object
Class Method Details
.at_threshold?(counter, options) ⇒ Boolean
21 22 23 |
# File 'lib/prop/interval_strategy.rb', line 21 def at_threshold?(counter, ) counter >= .fetch(:threshold) end |
.build(options) ⇒ Object
Builds the expiring cache key
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/prop/interval_strategy.rb', line 26 def build() key = .fetch(:key) handle = .fetch(:handle) interval = .fetch(:interval) window = (Time.now.to_i / interval) cache_key = Prop::Key.normalize([ handle, key, window ]) "prop/#{Digest::MD5.hexdigest(cache_key)}" end |
.counter(cache_key, options) ⇒ Object
8 9 10 |
# File 'lib/prop/interval_strategy.rb', line 8 def counter(cache_key, ) Prop::Limiter.reader.call(cache_key).to_i end |
.increment(cache_key, options, counter) ⇒ Object
12 13 14 15 |
# File 'lib/prop/interval_strategy.rb', line 12 def increment(cache_key, , counter) increment = .fetch(:increment, 1) Prop::Limiter.writer.call(cache_key, counter + increment) end |
.reset(cache_key) ⇒ Object
17 18 19 |
# File 'lib/prop/interval_strategy.rb', line 17 def reset(cache_key) Prop::Limiter.writer.call(cache_key, 0) end |
.threshold_reached(options) ⇒ Object
37 38 39 40 41 |
# File 'lib/prop/interval_strategy.rb', line 37 def threshold_reached() threshold = .fetch(:threshold) "#{options[:handle]} threshold of #{threshold} tries per #{options[:interval]}s exceeded for key '#{options[:key].inspect}', hash #{options[:cache_key]}" end |
.validate_options!(options) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/prop/interval_strategy.rb', line 43 def () validate_positive_integer([:threshold], :threshold) validate_positive_integer([:interval], :interval) if .key?(:increment) raise ArgumentError.new(":increment must be zero or a positive Integer") if ![:increment].is_a?(Fixnum) || [:increment] < 0 end end |