Class: RateThrottleClient::ExponentialIncreaseProportionalRemainingDecrease

Inherits:
Base
  • Object
show all
Defined in:
lib/rate_throttle_client/clients/exponential_increase_proportional_remaining_decrease.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#log, #min_sleep, #multiplier, #sleep_for

Instance Method Summary collapse

Methods inherited from Base

#jitter

Constructor Details

#initialize(*args, decrease_divisor: nil, remaining_block: nil, **kargs) ⇒ ExponentialIncreaseProportionalRemainingDecrease

Returns a new instance of ExponentialIncreaseProportionalRemainingDecrease.



5
6
7
8
9
10
11
# File 'lib/rate_throttle_client/clients/exponential_increase_proportional_remaining_decrease.rb', line 5

def initialize(*args, decrease_divisor: nil, remaining_block: nil, **kargs)
  super(*args, **kargs)
  @decrease_divisor = (decrease_divisor || RateThrottleClient.max_limit).to_f
  @remaining_block = remaining_block || ->(req) {
    req.headers["RateLimit-Remaining"].to_i
  }
end

Instance Attribute Details

#decrease_divisorObject

Returns the value of attribute decrease_divisor.



3
4
5
# File 'lib/rate_throttle_client/clients/exponential_increase_proportional_remaining_decrease.rb', line 3

def decrease_divisor
  @decrease_divisor
end

#remaining_blockObject

Returns the value of attribute remaining_block.



3
4
5
# File 'lib/rate_throttle_client/clients/exponential_increase_proportional_remaining_decrease.rb', line 3

def remaining_block
  @remaining_block
end

Instance Method Details

#call(&block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rate_throttle_client/clients/exponential_increase_proportional_remaining_decrease.rb', line 13

def call(&block)
  sleep_for = @sleep_for
  sleep(sleep_for + jitter(sleep_for))

  while (req = yield) && req.status == 429
    sleep_for += @min_sleep

    @log.call(Info.new(sleep_for: sleep_for, request: req))
    sleep(sleep_for + jitter(sleep_for))

    sleep_for *= @multiplier
  end

  decrease_value = sleep_for * @remaining_block.call(req)
  decrease_value /= @decrease_divisor

  if sleep_for >= decrease_value
    sleep_for -= decrease_value
  else
    sleep_for = 0
  end

  @sleep_for = sleep_for

  req
end

#sleep_and_log(sleep_for:, request:) ⇒ Object



40
41
# File 'lib/rate_throttle_client/clients/exponential_increase_proportional_remaining_decrease.rb', line 40

def sleep_and_log(sleep_for: , request: )
end