Class: Rapidity::Composer
- Inherits:
-
Object
- Object
- Rapidity::Composer
- Defined in:
- lib/rapidity/composer.rb
Instance Attribute Summary collapse
-
#limiters ⇒ Object
readonly
Returns the value of attribute limiters.
-
#limits ⇒ Object
readonly
Returns the value of attribute limits.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Instance Method Summary collapse
-
#initialize(pool, name:, limits: [], namespace: 'rapidity') ⇒ Composer
constructor
limits example “‘ruby` limits = [ { threshold: 2, interval: 1 }, # 2 events per second { threshold: 9, interval: 5 }, # 9 events per 5 seconds { threshold: 20, interval: 20 }, # 20 events per 20 seconds { threshold: 42, interval: 60 }, # 42 events per 60 seconds ] “`.
- #obtain(count = 5, with_time: false) ⇒ Object
- #remains ⇒ Object
Constructor Details
#initialize(pool, name:, limits: [], namespace: 'rapidity') ⇒ Composer
limits example “‘ruby` limits = [
{ threshold: 2, interval: 1 }, # 2 events per second
{ threshold: 9, interval: 5 }, # 9 events per 5 seconds
{ threshold: 20, interval: 20 }, # 20 events per 20 seconds
{ threshold: 42, interval: 60 }, # 42 events per 60 seconds
] “‘
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rapidity/composer.rb', line 26 def initialize(pool, name:, limits: [], namespace: 'rapidity') @limits = limits @name = name @namespace = namespace @limiters = @limits.map.each_with_index do |l, i| limit = OpenStruct.new(l) ::Rapidity::Limiter.new(pool, name: "#{i}_#{name}_#{limit.threshold}/#{limit.interval}", interval: limit.interval, threshold: limit.threshold, namespace: namespace) end end |
Instance Attribute Details
#limiters ⇒ Object (readonly)
Returns the value of attribute limiters.
9 10 11 |
# File 'lib/rapidity/composer.rb', line 9 def limiters @limiters end |
#limits ⇒ Object (readonly)
Returns the value of attribute limits.
9 10 11 |
# File 'lib/rapidity/composer.rb', line 9 def limits @limits end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/rapidity/composer.rb', line 9 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
9 10 11 |
# File 'lib/rapidity/composer.rb', line 9 def namespace @namespace end |
Instance Method Details
#obtain(count = 5, with_time: false) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rapidity/composer.rb', line 44 def obtain(count = 5, with_time: false) time = nil @limiters.each do |limiter| count, time = limiter.obtain(count, with_time: with_time) break if count == 0 end if with_time [count, time] else count end end |
#remains ⇒ Object
38 39 40 41 42 |
# File 'lib/rapidity/composer.rb', line 38 def remains @limiters.each_with_object({}) do |limiter, result| result[limiter.name] = limiter.remains end end |