faraday_middleware-request-retry 0.1.0 → 0.2.0

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
  SHA1:
3
- metadata.gz: 7aa9342c9636f4ecd594b79f8bbf40efe64c5092
4
- data.tar.gz: ae7a148dcce1ecbcb12e5801f66f2242f997b4bf
3
+ metadata.gz: 0ca0587453305189b8a204f3d68cb995c9570854
4
+ data.tar.gz: 9865555586b7328f99d5980f7cecbe6b6f1b90a3
5
5
  SHA512:
6
- metadata.gz: 9febff9858f8c17495793773ebc4d75c1469c959f47d1813dedee59f75ae60a77d266848aca3938492d5736b457cee14ad8a3a1270c5cbba05fc149888923f74
7
- data.tar.gz: 1d7cd9529a89f91b0ac492bc367da09e85db67a83322d4347afcf53c28885bce7c8dcd6a13bd275c3426649f0d6023c0755c1f147e3ed54fb0704c5fc0475d30
6
+ metadata.gz: 1af7393a7fdef619daba5a1c088e33786cf45922a214bdd3423e1f3edf4f585c62264bf12020e081c49896e5412ed2d4ac315e63baf207dc6ed5ce24abc24dee
7
+ data.tar.gz: 37a0981c817b5f2d4a01d77889d21acd4b52bb24498a9f2b0cb46e240b442a79491911898846deb5fa6181488e314ecf567ded441a0bd5d9d3dedfb6b0bc608f
@@ -1,5 +1,7 @@
1
1
  CHANGELOG
2
2
  ---------
3
+ - **2016-12-28**: 0.2.0
4
+ - Refactor to support external use via `RetryUtil`
3
5
  - **2016-12-28**: 0.1.0
4
6
  - Add custom retry time support
5
7
  - Add custom status code support
data/README.md CHANGED
@@ -61,6 +61,20 @@ Options include:
61
61
  | `:retry_after` | Default retry seconds | optional | `10` seconds |
62
62
  | `:error_codes` | Array of error codes, e.g. `[429, 503, 504]` | optional | `[429, 503]` |
63
63
 
64
+ ### External Use
65
+
66
+ The core retry functionality can be used separately from Faraday when necessary. For example, when requests cannot be replayed, e.g. when `Faraday::UploadIO` is used.
67
+
68
+ ```ruby
69
+ res = faraday_client.get '/my_url'
70
+
71
+ retry_util = FaradayMiddleware::Request::RetryUtil.new
72
+
73
+ # The following will return `true` for retry, `false` for don't,
74
+ # while sleeping when necessary
75
+ want_retry = retry_util.retry_status res.status, res.headers['Retry-After']
76
+ ```
77
+
64
78
  ## Change Log
65
79
 
66
80
  See [CHANGELOG.md](CHANGELOG.md)
@@ -4,34 +4,20 @@ module FaradayMiddleware
4
4
  module Request
5
5
  # Faraday middleware to handle HTTP Status 429 (rate limiting) / 503 (maintenance)
6
6
  class Retry < Faraday::Middleware
7
- DEFAULT_RETRY_AFTER = 10
8
- ERROR_CODES = [429, 503]
9
-
10
- attr_reader :error_codes
11
- attr_reader :retry_after
7
+ attr_reader :retry_util
12
8
 
13
- def initialize(app, options = {})
9
+ def initialize(app, opts = {})
14
10
  super app
15
- @logger = options[:logger]
16
- @error_codes = options[:error_codes] || ERROR_CODES
17
- @retry_after = options[:retry_after] || DEFAULT_RETRY_AFTER
11
+ @retry_util = FaradayMiddleware::Request::RetryUtil.new opts
18
12
  end
19
13
 
20
14
  def call(env)
21
15
  original_env = env.dup
22
16
  response = @app.call env
23
17
 
24
- if @error_codes.include? response.env[:status]
25
- seconds_left = (response.env[:response_headers][:retry_after] || @retry_after).to_i
26
- @logger.warn "You have been rate limited. Retrying in #{seconds_left} seconds..." if @logger
18
+ retry_status = @retry_util.retry_status response.env[:status], response.env[:response_headers][:retry_after]
27
19
 
28
- seconds_left.times do |i|
29
- sleep 1
30
- time_left = seconds_left - i
31
- @logger.warn "#{time_left}..." if time_left > 0 && time_left % 5 == 0 && @logger
32
- end
33
-
34
- @logger.warn 'Retrying...' if @logger
20
+ if retry_status
35
21
  @app.call original_env
36
22
  else
37
23
  response
@@ -40,3 +26,37 @@ module FaradayMiddleware
40
26
  end
41
27
  end
42
28
  end
29
+
30
+ module FaradayMiddleware
31
+ module Request
32
+ # Standalone HTTP request retry utility class
33
+ # Can be used outside of FaradayMiddleware, e.g.
34
+ # when Faraday::UploadIO is used
35
+ class RetryUtil
36
+ DEFAULT_ERROR_CODES = [429, 503].freeze
37
+ DEFAULT_RETRY_AFTER = 10
38
+
39
+ attr_accessor :error_codes
40
+ attr_accessor :retry_after
41
+
42
+ def initialize(opts = {})
43
+ @error_codes = opts[:error_codes] || DEFAULT_ERROR_CODES
44
+ @retry_after = opts[:retry_after] || DEFAULT_RETRY_AFTER
45
+ @logger = opts[:logger]
46
+ end
47
+
48
+ def retry_status(status, this_retry_after = nil)
49
+ return false unless @error_codes.include? status
50
+ seconds_left = (this_retry_after || @retry_after).to_i
51
+ @logger.warn "You have been rate limited. Retrying in #{seconds_left} seconds..." if @logger
52
+ seconds_left.times do |i|
53
+ sleep 1
54
+ time_left = seconds_left - i
55
+ @logger.warn "#{time_left}..." if time_left > 0 && time_left % 5 == 0 && @logger
56
+ end
57
+ @logger.warn 'Retrying...' if @logger
58
+ true
59
+ end
60
+ end
61
+ end
62
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faraday_middleware-request-retry
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Wang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-26 00:00:00.000000000 Z
11
+ date: 2016-12-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday