Class: ShopifyUnlimited::Throttle

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_unlimited/throttle.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeThrottle

Returns a new instance of Throttle.



10
11
12
13
14
15
16
# File 'lib/shopify_unlimited/throttle.rb', line 10

def initialize
  @throttle = 0.6
  @throttle_increment = @throttle
  @requests_threshold = 10
  @debug = ! ENV['SHOPIFY_UNLIMITED_DEBUG'].blank?
  @not_found_retries = Integer(ENV['SHOPIFY_UNLIMITED_NOTFOUND_RETRIES']) rescue 1
end

Instance Attribute Details

#requests_thresholdObject

Returns the value of attribute requests_threshold.



9
10
11
# File 'lib/shopify_unlimited/throttle.rb', line 9

def requests_threshold
  @requests_threshold
end

#runningObject

Returns the value of attribute running.



9
10
11
# File 'lib/shopify_unlimited/throttle.rb', line 9

def running
  @running
end

#throttleObject

Returns the value of attribute throttle.



9
10
11
# File 'lib/shopify_unlimited/throttle.rb', line 9

def throttle
  @throttle
end

#throttle_incrementObject

Returns the value of attribute throttle_increment.



9
10
11
# File 'lib/shopify_unlimited/throttle.rb', line 9

def throttle_increment
  @throttle_increment
end

Instance Method Details

#dbg(msg) ⇒ Object



18
19
20
# File 'lib/shopify_unlimited/throttle.rb', line 18

def dbg(msg)
  puts msg if @debug
end

#runObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/shopify_unlimited/throttle.rb', line 22

def run
  if @running
    return yield
  end
  @running = true
  value = nil
  tries ||= 0
  not_found_tries ||= 0
  orig_logger = ActiveResource::Base.logger
  begin
    dbg "--- throttle.run ---"
    left = ShopifyAPI::Base.connection.response.nil? ? @requests_threshold : ShopifyAPI.credit_left
    over = @requests_threshold - left
    dbg "left: #{left} over: #{over}"
    if over > 0
      @throttle += (over * rand/20) + rand/10
      dbg "  sleep #{@throttle + rand/10}"
      sleep @throttle + rand/10
    else
      @throttle = (0.94 + rand/20) * @throttle
    end
    t = Time.now
    dbg "  yield"
    value = yield
  rescue ActiveResource::ClientError => e
    case e.response.code
    when '404'
      dbg "  404"
      sleep 5 + not_found_tries + (rand * rand * 5)
      not_found_tries += 1
      if not_found_tries <= @not_found_retries
        ActiveResource::Base.logger ||= Logger.new(STDOUT)
        ActiveResource::Base.logger.info "Shopify returned not found: #{e.message}. Retrying"
        retry
      else
        ActiveResource::Base.logger = orig_logger
        raise
      end
    when '429'
      dbg "  429"
      ActiveResource::Base.logger.info "Shopify hit api limit" if ActiveResource::Base.logger
      @throttle += rand/5
      tries += 1
      sleep (@throttle * 4 * tries) + rand/10
      if tries < 10
        retry
      else
        raise
      end
    else
      raise
    end
  end
  requests_made = left - ShopifyAPI.credit_left
  if requests_made > 1
    dbg "  #{requests_made} requests_made"
    @throttle += (rand/20) * requests_made
    fuzzy_sleep_time = [0, (t + (requests_made * @throttle) - Time.now)].max + rand/10
    dbg "  sleep #{fuzzy_sleep_time}"
    sleep fuzzy_sleep_time
  else
    @throttle = (0.94 + rand/20) * @throttle
  end
  dbg "=== throttle.done ==="
  value
ensure
  @running = false
end