Class: HttpThreshold::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/http_threshold/client.rb

Class Method Summary collapse

Class Method Details

.cacheObject



11
12
13
# File 'lib/http_threshold/client.rb', line 11

def cache
  @cache ||= Cache.new
end

.incr_count(domain) ⇒ Object



19
20
21
# File 'lib/http_threshold/client.rb', line 19

def incr_count(domain)
  self.throttles[domain].try(:incr_count)
end

.lock_managerObject



15
16
17
# File 'lib/http_threshold/client.rb', line 15

def lock_manager
  @lock_manager ||= Redlock::Client.new([Redis.new(:url => HttpThreshold.redis_url)])
end

.set_throttle(domain, options = {}) ⇒ Object



23
24
25
# File 'lib/http_threshold/client.rb', line 23

def set_throttle(domain, options = {})
  self.throttles[domain] = HttpThreshold::Throttle.new(domain, options)
end

.sleep_until_allowed(host) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/http_threshold/client.rb', line 27

def sleep_until_allowed(host)
  throttle = self.throttles[host]
  if throttle
    if !throttle.incr_count
      puts "#{host} reach threshold limit"
      sleep 0.25

      while !throttle.incr_count
        sleep 0.25
      end
    end
    yield
  else
    yield
  end
end

.throttlesObject



7
8
9
# File 'lib/http_threshold/client.rb', line 7

def throttles
  @throttles  ||= {}
end