class Lucid::Shopify::ThrottledStrategy

Maintain API call limit throttling across a single thread.

Constants

MINIMUM_INTERVAL

Public Instance Methods

call(request, &send_request) click to toggle source

@param request [Request]

@yieldreturn [Response]

@return [Response]

# File lib/lucid/shopify/throttled_strategy.rb, line 16
def call(request, &send_request)
  interval(build_interval_key(request))

  send_request.()
end

Private Instance Methods

build_interval_key(request) click to toggle source

@param request [Request]

@return [String]

# File lib/lucid/shopify/throttled_strategy.rb, line 39
        def build_interval_key(request)
  '%s[%s].timestamp' % [self.class, request.credentials.myshopify_domain]
end
interval(interval_key) click to toggle source

If time since the last request < {MINIMUM_INTERVAL}, then sleep for the difference.

@param interval_key [String]

# File lib/lucid/shopify/throttled_strategy.rb, line 26
        def interval(interval_key)
  if Thread.current[interval_key]
    (timestamp - Thread.current[interval_key]).tap do |n|
      sleep(Rational(MINIMUM_INTERVAL - n, 1000)) if n < MINIMUM_INTERVAL
    end
  end

  Thread.current[interval_key] = timestamp
end
timestamp() click to toggle source

Time in milliseconds since the UNIX epoch.

@return [Integer]

# File lib/lucid/shopify/throttled_strategy.rb, line 46
        def timestamp
  (Time.now.to_f * 1000).to_i
end