Class: BugCourier::RateLimiter

Inherits:
Object
  • Object
show all
Defined in:
lib/bug_courier/exception_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(max_per_hour:) ⇒ RateLimiter



7
8
9
10
11
# File 'lib/bug_courier/exception_handler.rb', line 7

def initialize(max_per_hour:)
  @max_per_hour = max_per_hour
  @timestamps = []
  @mutex = Mutex.new
end

Instance Method Details

#allow?Boolean



13
14
15
16
17
18
19
20
21
22
# File 'lib/bug_courier/exception_handler.rb', line 13

def allow?
  @mutex.synchronize do
    now = Time.now
    @timestamps.reject! { |t| now - t > 3600 }
    return false if @timestamps.size >= @max_per_hour

    @timestamps << now
    true
  end
end