faraday_middleware-request-retry 0.1.0 → 0.2.1

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
- SHA1:
3
- metadata.gz: 7aa9342c9636f4ecd594b79f8bbf40efe64c5092
4
- data.tar.gz: ae7a148dcce1ecbcb12e5801f66f2242f997b4bf
2
+ SHA256:
3
+ metadata.gz: 7b22970596e52bff369b3bfec6cd6a8812edb3024c9b59c206358ec864fcabac
4
+ data.tar.gz: 0fb7fa15f6063559932f0b6bdf0e593a0a2c873077e80c1220e1841c70e5547f
5
5
  SHA512:
6
- metadata.gz: 9febff9858f8c17495793773ebc4d75c1469c959f47d1813dedee59f75ae60a77d266848aca3938492d5736b457cee14ad8a3a1270c5cbba05fc149888923f74
7
- data.tar.gz: 1d7cd9529a89f91b0ac492bc367da09e85db67a83322d4347afcf53c28885bce7c8dcd6a13bd275c3426649f0d6023c0755c1f147e3ed54fb0704c5fc0475d30
6
+ metadata.gz: 8aebdbe48a13730b3913803c09afed4d46aac46557e905f6efa0753cc0b9807b13c0d4750f8c1ad1984a74f23731fef4fa515265d436141cb1148982ea90f530
7
+ data.tar.gz: 320cbd78a690c6ee6594299897cb311635e2a0116d9e58b66ba993767c963f67f7164b118cd0a4e08e353690d171030e080f421b95d6bddeada2abeabd592ce7
data/CHANGELOG.md CHANGED
@@ -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
@@ -4,11 +4,8 @@ FaradayMiddleware::Request::Retry
4
4
  [![Gem Version][gem-version-svg]][gem-version-link]
5
5
  [![Build Status][build-status-svg]][build-status-link]
6
6
  [![Coverage Status][coverage-status-svg]][coverage-status-link]
7
- [![Dependency Status][dependency-status-svg]][dependency-status-link]
8
- [![Codacy Badge][codacy-svg]][codacy-link]
9
7
  [![Code Climate][codeclimate-status-svg]][codeclimate-status-link]
10
8
  [![Scrutinizer Code Quality][scrutinizer-status-svg]][scrutinizer-status-link]
11
- [![Downloads][downloads-svg]][downloads-link]
12
9
  [![Docs][docs-rubydoc-svg]][docs-rubydoc-link]
13
10
  [![License][license-svg]][license-link]
14
11
 
@@ -61,6 +58,20 @@ Options include:
61
58
  | `:retry_after` | Default retry seconds | optional | `10` seconds |
62
59
  | `:error_codes` | Array of error codes, e.g. `[429, 503, 504]` | optional | `[429, 503]` |
63
60
 
61
+ ### External Use
62
+
63
+ 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.
64
+
65
+ ```ruby
66
+ res = faraday_client.get '/my_url'
67
+
68
+ retry_util = FaradayMiddleware::Request::RetryUtil.new
69
+
70
+ # The following will return `true` for retry, `false` for don't,
71
+ # while sleeping when necessary
72
+ want_retry = retry_util.retry_status res.status, res.headers['Retry-After']
73
+ ```
74
+
64
75
  ## Change Log
65
76
 
66
77
  See [CHANGELOG.md](CHANGELOG.md)
@@ -83,7 +94,7 @@ This library is originally based on code extracted from the Zendesk Ruby SDK's `
83
94
 
84
95
  ## Copyright and license
85
96
 
86
- Copyright © 2015 Zendesk, 2016 John Wang
97
+ Copyright © 2015 Zendesk, 2016-2023 John Wang
87
98
 
88
99
  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
89
100
 
@@ -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.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Wang
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-12-26 00:00:00.000000000 Z
11
+ date: 2023-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -16,40 +16,40 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '1.10'
20
20
  - - ">="
21
21
  - !ruby/object:Gem::Version
22
- version: '0'
22
+ version: 1.10.3
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
26
26
  requirements:
27
27
  - - "~>"
28
28
  - !ruby/object:Gem::Version
29
- version: '0'
29
+ version: '1.10'
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
- version: '0'
32
+ version: 1.10.3
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: faraday_middleware
35
35
  requirement: !ruby/object:Gem::Requirement
36
36
  requirements:
37
37
  - - "~>"
38
38
  - !ruby/object:Gem::Version
39
- version: '0'
39
+ version: '1.2'
40
40
  - - ">="
41
41
  - !ruby/object:Gem::Version
42
- version: '0'
42
+ version: 1.2.0
43
43
  type: :runtime
44
44
  prerelease: false
45
45
  version_requirements: !ruby/object:Gem::Requirement
46
46
  requirements:
47
47
  - - "~>"
48
48
  - !ruby/object:Gem::Version
49
- version: '0'
49
+ version: '1.2'
50
50
  - - ">="
51
51
  - !ruby/object:Gem::Version
52
- version: '0'
52
+ version: 1.2.0
53
53
  - !ruby/object:Gem::Dependency
54
54
  name: coveralls
55
55
  requirement: !ruby/object:Gem::Requirement
@@ -70,14 +70,20 @@ dependencies:
70
70
  requirements:
71
71
  - - "~>"
72
72
  - !ruby/object:Gem::Version
73
- version: '11'
73
+ version: '13'
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: 13.0.6
74
77
  type: :development
75
78
  prerelease: false
76
79
  version_requirements: !ruby/object:Gem::Requirement
77
80
  requirements:
78
81
  - - "~>"
79
82
  - !ruby/object:Gem::Version
80
- version: '11'
83
+ version: '13'
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: 13.0.6
81
87
  - !ruby/object:Gem::Dependency
82
88
  name: simplecov
83
89
  requirement: !ruby/object:Gem::Requirement
@@ -119,11 +125,11 @@ files:
119
125
  - lib/faraday_middleware-request-retry.rb
120
126
  - lib/faraday_middleware-request-retry/retry.rb
121
127
  - test/test_setup.rb
122
- homepage: https://github.com/grokify/
128
+ homepage: https://github.com/grokify/faraday_middleware-request-retry
123
129
  licenses:
124
130
  - Apache-2.0
125
131
  metadata: {}
126
- post_install_message:
132
+ post_install_message:
127
133
  rdoc_options: []
128
134
  require_paths:
129
135
  - lib
@@ -138,9 +144,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
138
144
  - !ruby/object:Gem::Version
139
145
  version: '0'
140
146
  requirements: []
141
- rubyforge_project:
142
- rubygems_version: 2.5.2
143
- signing_key:
147
+ rubygems_version: 3.4.2
148
+ signing_key:
144
149
  specification_version: 4
145
150
  summary: Faraday request middleware with retry
146
151
  test_files: []