kontent-delivery-sdk-ruby 2.0.10 → 2.0.11

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e6fdd249e9cf25e87571caf44ef970862708e8dcef43c720507a99a43e1dc5e7
4
- data.tar.gz: 6c9ec2a7b64b69d1bf544da3611647fd31d990e523a33f1bc5d03f9854cec04b
3
+ metadata.gz: 10a1940690541523126a2bd9f3767e54a68de48c14ca7acfb311f392e0cb3707
4
+ data.tar.gz: 80b485b19bef5ddc40e8964133b96dc0c1d3543f2c477c704973502c86495832
5
5
  SHA512:
6
- metadata.gz: e628cb4abafc731ad06a5f9ea1ca68d11943635c8ca0942cc7b2a414a88653b2901636b2a87b1e6c38ac017059dbb7faeff895cf6293ae0b0bed402e1209368d
7
- data.tar.gz: c2602cd4065a6bfa3604c01d69e3224a193495bdfcfdece618745d71bf6eae4b6484993e4c55ee84e13932da5622fd5ebffce3056eca1a8782db18036f605a48
6
+ metadata.gz: e11259c855f2dc4caf3146a704b0330acf6314d68cff4259a1acb03c60bee8359b567f68d10c033e2fef3bd36a51da864f8f5501485b032e05feba6b37f0a57b
7
+ data.tar.gz: 0a4ab01bb8be8744d83faf31ed25e35fa986b1fedaf531e7c85978138a3e58563ead1e5d19ba42e494af9d79ba73bedc87e3f1c0ea80088582a3b06790f6dbeb
data/README.md CHANGED
@@ -97,15 +97,16 @@ You can then request the secure published content in your project. Be sure to no
97
97
 
98
98
  ### Retry policy
99
99
 
100
- By default, the SDK uses a retry policy, asking for requested content again in case of an error. The default policy retries the HTTP requests if the following status codes are returned:
100
+ By default, the SDK uses a retry policy, asking for requested content again in case of an error. The default policy retries the HTTP request if the following status codes are returned:
101
101
 
102
102
  * 408 - `RequestTimeout`
103
+ * 429 - `TooManyRequests`
103
104
  * 500 - `InternalServerError`
104
105
  * 502 - `BadGateway`
105
106
  * 503 - `ServiceUnavailable`
106
107
  * 504 - `GatewayTimeout`
107
108
 
108
- The default policy retries requests 5 times, totaling 6 overall attempts to retrieve content before returning a `ResponseBase` object containing the error. The consecutive attempts are delayed exponentially: 200 milliseconds, 400 milliseconds, 800 milliseconds, etc.
109
+ The SDK will perform a total of 6 attempts at a maximum of 30 seconds to retrieve content before returning a `ResponseBase` object containing the error. The consecutive attempts are delayed with [exponential backoff and jitter](https://aws.amazon.com/blogs/architecture/exponential-backoff-and-jitter/).
109
110
 
110
111
  To disable the retry policy, you can use the `with_retry_policy` argument:
111
112
 
@@ -7,8 +7,10 @@ module Kentico
7
7
  class RequestManager
8
8
  class << self
9
9
  MAX_ATTEMPTS = 6
10
- INITIAL_DELAY = 0.2
11
- RETRY_WHEN_CODE = [408, 500, 502, 503, 504].freeze
10
+ MAX_DELAY_SECONDS = 30
11
+ INITIAL_DELAY = 1
12
+ RETRY_WHEN_CODE = [408, 429, 500, 502, 503, 504].freeze
13
+ CODES_WITH_POSSIBLE_RETRY_HEADER = [429, 503].freeze
12
14
 
13
15
  def start(query, headers)
14
16
  @query = query
@@ -16,6 +18,7 @@ module Kentico
16
18
  @times_run = 1
17
19
  @delay = INITIAL_DELAY
18
20
  @url = @query.provide_url
21
+ @total_delay = 0
19
22
  continue
20
23
  end
21
24
 
@@ -23,19 +26,28 @@ module Kentico
23
26
 
24
27
  def should_retry(potential_response)
25
28
  return potential_response if @times_run == MAX_ATTEMPTS ||
26
- !RETRY_WHEN_CODE.include?(potential_response.http_code) ||
27
- !@query.with_retry_policy
29
+ !RETRY_WHEN_CODE.include?(potential_response.http_code) ||
30
+ !@query.with_retry_policy ||
31
+ @total_delay >= MAX_DELAY_SECONDS
28
32
 
29
- @times_run += 1
30
- @delay *= 2
33
+ next_delay
31
34
  sleep(@delay)
35
+ @total_delay += @delay
32
36
  continue
33
37
  end
34
38
 
39
+ # Generates a random delay based on times_run, then increases times_run
40
+ def next_delay
41
+ min = 0.8 * INITIAL_DELAY
42
+ max = (1.2 * INITIAL_DELAY) * (2**@times_run)
43
+ @delay = rand(min..max)
44
+ @times_run += 1
45
+ end
46
+
35
47
  def continue
36
48
  if ENV['TEST'] == '1'
37
49
  resp = Kentico::Kontent::Delivery::Tests::FakeResponder.get_response @query, @url, @headers
38
- return resp if resp.is_a? Kentico::Kontent::Delivery::Responses::ResponseBase
50
+ return should_retry(resp) if resp.is_a? Kentico::Kontent::Delivery::Responses::ResponseBase
39
51
 
40
52
  make_response resp # resp is pure JSON
41
53
  else
@@ -0,0 +1,5 @@
1
+ {
2
+ "request_id": "80000004-0002-fd00-b63f-84710c7967bb",
3
+ "error_code": 10000,
4
+ "message": "API rate limit exceeded. Please retry your request later."
5
+ }
@@ -32,6 +32,8 @@ module Kentico
32
32
  url[BASE_URL.length...url.length]
33
33
  end
34
34
 
35
+ return respond_429 if @query.code_name == '429'
36
+
35
37
  qs = url.contains('?') ? url.split('?')[1] : nil
36
38
  return respond_filtering qs unless qs.nil? # e.g. /items/about_us?skip=0&limit=5
37
39
 
@@ -65,6 +67,11 @@ module Kentico
65
67
  ##Kentico::Kontent::Delivery::Responses::ResponseBase.new 200, '', '', path.read if path.exist?
66
68
  end
67
69
 
70
+ def respond_429
71
+ path = Pathname.new(File.dirname(__FILE__) + '/429.json')
72
+ Kentico::Kontent::Delivery::Responses::ResponseBase.new 429, '', '', path.read if path.exist?
73
+ end
74
+
68
75
  def respond_401
69
76
  path = Pathname.new(File.dirname(__FILE__) + '/401.json')
70
77
  Kentico::Kontent::Delivery::Responses::ResponseBase.new 401, '', '', path.read if path.exist?
@@ -1,7 +1,7 @@
1
1
  module Kentico
2
2
  module Kontent
3
3
  module Delivery
4
- VERSION = '2.0.10'.freeze
4
+ VERSION = '2.0.11'.freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kontent-delivery-sdk-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.10
4
+ version: 2.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Dugre
@@ -155,6 +155,7 @@ files:
155
155
  - lib/delivery/responses/delivery_type_response.rb
156
156
  - lib/delivery/responses/response_base.rb
157
157
  - lib/delivery/tests/401.json
158
+ - lib/delivery/tests/429.json
158
159
  - lib/delivery/tests/fake_responder.rb
159
160
  - lib/delivery/tests/filtering/items_gt.json
160
161
  - lib/delivery/tests/filtering/items_with_count.json