Class: Lightspeed::RequestThrottler

Inherits:
Object
  • Object
show all
Defined in:
lib/lightspeed/request_throttler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequestThrottler

Returns a new instance of RequestThrottler.



5
6
7
8
9
# File 'lib/lightspeed/request_throttler.rb', line 5

def initialize
  @units_per_second = 0.5
  @bucket_max = Float::INFINITY
  @bucket_level = 0
end

Instance Attribute Details

#bucket_levelObject

Returns the value of attribute bucket_level.



3
4
5
# File 'lib/lightspeed/request_throttler.rb', line 3

def bucket_level
  @bucket_level
end

#bucket_maxObject

Returns the value of attribute bucket_max.



3
4
5
# File 'lib/lightspeed/request_throttler.rb', line 3

def bucket_max
  @bucket_max
end

#units_per_secondObject

Returns the value of attribute units_per_second.



3
4
5
# File 'lib/lightspeed/request_throttler.rb', line 3

def units_per_second
  @units_per_second
end

Instance Method Details

#perform_request(request) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/lightspeed/request_throttler.rb', line 11

def perform_request request
  u = units request
  sleep(u / @units_per_second) if @bucket_level + u > @bucket_max
  response = request.perform
  extract_rate_limits request
  response
end