Class: Rapidity::Composer

Inherits:
Object
  • Object
show all
Defined in:
lib/rapidity/composer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#limitersObject (readonly)

Returns the value of attribute limiters.



9
10
11
# File 'lib/rapidity/composer.rb', line 9

def limiters
  @limiters
end

#limitsObject (readonly)

Returns the value of attribute limits.



9
10
11
# File 'lib/rapidity/composer.rb', line 9

def limits
  @limits
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/rapidity/composer.rb', line 9

def name
  @name
end

#namespaceObject (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

#remainsObject



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