Class: Lucid::Shopify::RedisThrottledStrategy

Inherits:
ThrottledStrategy show all
Defined in:
lib/lucid/shopify/redis_throttled_strategy.rb

Overview

Use Redis to maintain API call limit throttling across threads/processes.

No delay for requests up to half of the call limit.

Constant Summary collapse

LEAK_RATE =
500

Constants inherited from ThrottledStrategy

ThrottledStrategy::MINIMUM_INTERVAL

Instance Method Summary collapse

Constructor Details

#initialize(redis_client: Redis.current) ⇒ RedisThrottledStrategy

Returns a new instance of RedisThrottledStrategy.

Parameters:

  • redis_client (Redis) (defaults to: Redis.current)


15
16
17
# File 'lib/lucid/shopify/redis_throttled_strategy.rb', line 15

def initialize(redis_client: Redis.current)
  @redis_client = redis_client
end

Instance Method Details

#call(request, &send_request) ⇒ Response

Parameters:

Yield Returns:

Returns:



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lucid/shopify/redis_throttled_strategy.rb', line 24

def call(request, &send_request)
  interval_key = build_interval_key(request)

  interval(interval_key)

  send_request.().tap do |res|
    header = res.headers['X-Shopify-Shop-Api-Call-Limit']

    next if header.nil?

    cur, max = header.split('/')

    @redis_client.mapped_hmset(interval_key,
      cur: cur,
      max: max,
      at: timestamp
    )
  end
end