Class: Rookout::Augs::LimitsManager

Inherits:
Object
  • Object
show all
Defined in:
lib/rookout/augs/limits_manager.rb

Instance Method Summary collapse

Constructor Details

#initialize(limiters) ⇒ LimitsManager

Returns a new instance of LimitsManager.



4
5
6
# File 'lib/rookout/augs/limits_manager.rb', line 4

def initialize limiters
  @limiters = limiters
end

Instance Method Details

#with_limit(skip_limiters, start_time = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rookout/augs/limits_manager.rb', line 8

def with_limit skip_limiters, start_time = nil
  start_time ||= Time.now
  can_execute = true
  after_execute = []

  @limiters.each do |limiter|
    limiter_passed = limiter.before_run start_time
    if limiter_passed || skip_limiters
      after_execute.append -> { limiter.after_run start_time }
    else
      can_execute = false
    end
  end

  if can_execute
    yield
  end
ensure
  unless after_execute.nil?
    after_execute.each(&:call)
  end
end