Class: LucidShopify::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)


18
19
20
# File 'lib/lucid_shopify/redis_throttled_strategy.rb', line 18

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

Instance Method Details

#call(request, &send_request) ⇒ Response

Parameters:

Yield Returns:

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/lucid_shopify/redis_throttled_strategy.rb', line 29

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

  interval(interval_key)

  send_request.().tap do |res|
    cur, max = res.headers['X-Shopify-Shop-Api-Call-Limit'].split('/')

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