Module: ShopifyCLI::Theme::BackoffHelper

Included in:
Syncer, Syncer::Uploader::Bulk
Defined in:
lib/shopify_cli/theme/backoff_helper.rb

Instance Method Summary collapse

Instance Method Details

#backingoff?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/shopify_cli/theme/backoff_helper.rb', line 32

def backingoff?
  backoff_mutex.locked?
end

#backoff!Object



27
28
29
30
# File 'lib/shopify_cli/theme/backoff_helper.rb', line 27

def backoff!
  ctx.debug("Near API call limit, waiting #{@backoff_delay} seconds")
  backoff_mutex.synchronize { wait(@backoff_delay) }
end

#backoff_if_near_limit!(response) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'lib/shopify_cli/theme/backoff_helper.rb', line 12

def backoff_if_near_limit!(response)
  # Check if the API told us we're near the rate limit
  return if backingoff? || !response

  limit = response.fetch("x-shopify-shop-api-call-limit", "0/999")
  used, total = limit.split("/").map(&:to_i)

  backoff! if used > total - @margin
end

#backoff_mutexObject



36
37
38
# File 'lib/shopify_cli/theme/backoff_helper.rb', line 36

def backoff_mutex
  @backoff_mutex || raise("Backoff helper must be initialized")
end

#initialize_backoff_helper!(margin: 2, backoff_delay: 2) ⇒ Object



6
7
8
9
10
# File 'lib/shopify_cli/theme/backoff_helper.rb', line 6

def initialize_backoff_helper!(margin: 2, backoff_delay: 2)
  @margin = margin
  @backoff_delay = backoff_delay
  @backoff_mutex = Mutex.new
end

#wait_for_backoff!Object



22
23
24
25
# File 'lib/shopify_cli/theme/backoff_helper.rb', line 22

def wait_for_backoff!
  # Sleeping in the mutex in another thread. Wait for unlock
  backoff_mutex.synchronize {} if backingoff?
end