shopify_unlimited 0.0.16.threadsafe → 0.0.17.threadsafe

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- ZjE1N2RjYWE2NTk0OTZmNzA1NmZmMzE3NTFjYTM4MzE3YjFkN2RiMg==
5
- data.tar.gz: !binary |-
6
- MGE0N2VhMTcxYjIwZTRkMDZlMzA0Nzk0NWI1Yzk2MzFlMTgzY2UxMQ==
7
- !binary "U0hBNTEy":
8
- metadata.gz: !binary |-
9
- OWFkYzVhMmNhODAxYjA4OGFiNDExM2NjOTIyMjQxNTk2MDdiMjg0NWQwNThh
10
- MjUwZjIyZmYzOTMwZTRmNGYxZGNjN2I5MWE5MGZlOGI5Yzk0MGRhM2NkYzMx
11
- NWJhYTY0ZjU0NTQ3OGZhMmY1YTkyNTY3NWYwMmEwNThmNTNjMGE=
12
- data.tar.gz: !binary |-
13
- OTJlMTNmN2YyYWM0OGIyMjg1MDNjYzVhMThhMWNhZGE3YjZiNGExNDUzMjc4
14
- OTc1NWUwZmRkOTY2NTI3ODU1NjVmNTQzNzlmZTc4YTY2MjM1MDY2YTgwMjhi
15
- ZGY5YWYwZjQ0NDM2MGNlZjliZjJkZWRiOTYxZWY5ZWE5YTEzZTI=
2
+ SHA1:
3
+ metadata.gz: aa8f62c2ce2a1f1dcbf25df03bd1210234b7d76b
4
+ data.tar.gz: 326c1b9728cee6f230768c50b0528a325b04f44a
5
+ SHA512:
6
+ metadata.gz: 0ae8d3d2dbed282c326976ab386d7ce31eb28db82ecb94e1a0560b37c470d35187a20b587ffdd73e5f0331d0a9487e2ee3f04ba34bfa6bf4143b3cb79ac30909
7
+ data.tar.gz: b5495e4f699511dce761a08fe72a6d244d2f195504cc508c11bb16ce25c1cb642294dd1219cabe96017c0f5a9bca85c87087fa1990d2f35cd32c4cf6e10eaa4d
@@ -41,17 +41,13 @@ module ActiveResource
41
41
  page +=1
42
42
  last_count = results.count
43
43
  options[:params][:page] = page
44
- ShopifyAPI::Shop.current.throttle.run do
45
- next_result = find_every.bind(self).call(options)
46
- results.concat next_result unless next_result.nil?
47
- end
48
- results.requests_made += 1
49
- end
50
- else
51
- ShopifyAPI::Shop.current.throttle.run do
52
44
  next_result = find_every.bind(self).call(options)
53
45
  results.concat next_result unless next_result.nil?
46
+ results.requests_made += 1
54
47
  end
48
+ else
49
+ next_result = find_every.bind(self).call(options)
50
+ results.concat next_result unless next_result.nil?
55
51
  results.requests_made += 1
56
52
  end
57
53
 
@@ -0,0 +1,19 @@
1
+ module ShopifyUnlimited
2
+ module ThrottledConnection
3
+ def throttle
4
+ @throttle ||= ShopifyUnlimited::Throttle.new
5
+ end
6
+ def with_auth
7
+ throttle.run do
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
13
+
14
+ module ActiveResource
15
+ class Connection
16
+ prepend ShopifyUnlimited::ThrottledConnection
17
+ end
18
+ end
19
+
@@ -4,50 +4,50 @@
4
4
  # of requests until finished, which will typically happen with
5
5
  # any naive, non-stochastic implementation.
6
6
 
7
- # currently this is only true when only one request at a time
8
- # is passed to throttle.run
9
- # When it becomes useful, we could add an argument to throttle.run
10
- # to specify the number of expected requests. However, that would have
11
- # has the downside that the thread might end up waiting a long time
12
- # for a large enough block of requests.
13
-
14
- module ShopifyAPI
15
-
16
- class Shop
17
- def throttle
18
- @throttle ||= Throttle.new
19
- end
20
- end
21
-
7
+ module ShopifyUnlimited
22
8
  class Throttle
23
- attr_accessor :throttle, :throttle_increment, :requests_threshold
9
+ attr_accessor :throttle, :throttle_increment, :requests_threshold, :running
24
10
  def initialize
25
11
  @throttle = 0.6
26
12
  @throttle_increment = @throttle
27
13
  @requests_threshold = 10
14
+ @debug = ! ENV['SHOPIFY_UNLIMITED_DEBUG'].blank?
28
15
  end
29
16
 
30
- def run(&block)
17
+ def dbg(msg)
18
+ puts msg if @debug
19
+ end
20
+
21
+ def run
22
+ if @running
23
+ return yield
24
+ end
25
+ @running = true
31
26
  value = nil
32
27
  retries ||= 0
33
28
  orig_logger = ActiveResource::Base.logger
34
29
  begin
35
- left = ShopifyAPI.credit_left
30
+ dbg "--- throttle.run ---"
31
+ left = ShopifyAPI::Base.connection.response.nil? ? @requests_threshold : ShopifyAPI.credit_left
36
32
  over = @requests_threshold - left
33
+ dbg "left: #{left} over: #{over}"
37
34
  if over > 0
38
35
  @throttle += (over * rand/20) + rand/10
36
+ dbg " sleep #{@throttle + rand/10}"
39
37
  sleep @throttle + rand/10
40
38
  else
41
39
  @throttle = (0.94 + rand/20) * @throttle
42
40
  end
43
41
  t = Time.now
42
+ dbg " yield"
44
43
  value = yield
45
44
  rescue ActiveResource::ClientError => e
46
45
  case e.response.code
47
46
  when '404'
47
+ dbg " 404"
48
48
  sleep 5 + retries + (rand * rand * 5)
49
49
  retries += 1
50
- if retries < 4
50
+ if retries < 2
51
51
  ActiveResource::Base.logger ||= Logger.new(STDOUT)
52
52
  ActiveResource::Base.logger.info "Shopify returned not found: #{e.message}. Retrying"
53
53
  retry
@@ -56,6 +56,7 @@ module ShopifyAPI
56
56
  raise
57
57
  end
58
58
  when '429'
59
+ dbg " 429"
59
60
  ActiveResource::Base.logger.info "Shopify hit api limit" if ActiveResource::Base.logger
60
61
  @throttle += rand/5
61
62
  retries += 1
@@ -71,13 +72,20 @@ module ShopifyAPI
71
72
  end
72
73
  requests_made = left - ShopifyAPI.credit_left
73
74
  if requests_made > 1
75
+ dbg " #{requests_made} requests_made"
74
76
  @throttle += (rand/20) * requests_made
75
- sleep [0, (t + (requests_made * @throttle) - Time.now)].max + rand/10
77
+ fuzzy_sleep_time = [0, (t + (requests_made * @throttle) - Time.now)].max + rand/10
78
+ dbg " sleep #{fuzzy_sleep_time}"
79
+ sleep fuzzy_sleep_time
76
80
  else
77
81
  @throttle = (0.94 + rand/20) * @throttle
82
+ dbg " sleep #{rand/20}"
78
83
  sleep rand/20
79
84
  end
85
+ dbg "=== throttle.done ==="
80
86
  value
87
+ ensure
88
+ @running = false
81
89
  end
82
90
  end
83
91
  end
@@ -1,3 +1,3 @@
1
1
  module ShopifyUnlimited
2
- VERSION = "0.0.16.threadsafe"
2
+ VERSION = "0.0.17.threadsafe"
3
3
  end
@@ -1,3 +1,4 @@
1
1
  require "shopify_unlimited/version"
2
+ require 'shopify_unlimited/active_resource/connection_extensions'
2
3
  require 'shopify_unlimited/active_resource/base_extensions'
3
- require 'shopify_unlimited/shopify_api/throttle'
4
+ require 'shopify_unlimited/throttle'
metadata CHANGED
@@ -1,107 +1,107 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shopify_unlimited
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.16.threadsafe
4
+ version: 0.0.17.threadsafe
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Johnston
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-02 00:00:00.000000000 Z
11
+ date: 2014-05-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeresource
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: activesupport
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: shopify_api
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 3.2.1
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 3.2.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ~>
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: '1.3'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ~>
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.3'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - ! '>='
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - ! '>='
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- description: ! 'allow use of limit: false in api requests'
97
+ description: 'allow use of limit: false in api requests'
98
98
  email:
99
99
  - lastobelus@mac.com
100
100
  executables: []
101
101
  extensions: []
102
102
  extra_rdoc_files: []
103
103
  files:
104
- - .gitignore
104
+ - ".gitignore"
105
105
  - Gemfile
106
106
  - Guardfile
107
107
  - LICENSE.txt
@@ -109,7 +109,8 @@ files:
109
109
  - Rakefile
110
110
  - lib/shopify_unlimited.rb
111
111
  - lib/shopify_unlimited/active_resource/base_extensions.rb
112
- - lib/shopify_unlimited/shopify_api/throttle.rb
112
+ - lib/shopify_unlimited/active_resource/connection_extensions.rb
113
+ - lib/shopify_unlimited/throttle.rb
113
114
  - lib/shopify_unlimited/version.rb
114
115
  - shopify_unlimited.gemspec
115
116
  - spec/spec_helper.rb
@@ -123,19 +124,19 @@ require_paths:
123
124
  - lib
124
125
  required_ruby_version: !ruby/object:Gem::Requirement
125
126
  requirements:
126
- - - ! '>='
127
+ - - ">="
127
128
  - !ruby/object:Gem::Version
128
129
  version: '0'
129
130
  required_rubygems_version: !ruby/object:Gem::Requirement
130
131
  requirements:
131
- - - ! '>'
132
+ - - ">"
132
133
  - !ruby/object:Gem::Version
133
134
  version: 1.3.1
134
135
  requirements: []
135
136
  rubyforge_project:
136
- rubygems_version: 2.0.6
137
+ rubygems_version: 2.2.2
137
138
  signing_key:
138
139
  specification_version: 4
139
- summary: ! 'allow use of limit: false in api requests'
140
+ summary: 'allow use of limit: false in api requests'
140
141
  test_files:
141
142
  - spec/spec_helper.rb