Module: CableX::Cable::RateLimit::CheckRateLimit

Included in:
Connection
Defined in:
lib/cable_x/cable/rate_limit/rate_limit.rb

Overview

Process Rate Limit

Instance Method Summary collapse

Instance Method Details

#check_rate_limitObject



13
14
15
# File 'lib/cable_x/cable/rate_limit/rate_limit.rb', line 13

def check_rate_limit
  check_unit('second') || check_unit('minute') || check_unit('hour')
end

#check_unit(unit) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/cable_x/cable/rate_limit/rate_limit.rb', line 17

def check_unit(unit)
  check_value = rate_limit[unit.to_s.to_sym]
  return unless check_value

  key = device_key unit
  redis_set key, 0, 1.send(unit) * 1000 unless redis_exists(key)
  redis_incr key
  block_connection = (redis_get(key) || -1).to_i >= check_value
  return unless block_connection

  block_device unit
end

#do_rate_limitObject



9
10
11
# File 'lib/cable_x/cable/rate_limit/rate_limit.rb', line 9

def do_rate_limit
  check_rate_limit if rate_limit
end