faraday_loop 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1d56c0d8c38f2280182be9196717e0c5d7d34d4fb379f0d8131980cf823001b7
4
+ data.tar.gz: 9ac03fb0cf3df5c99af7d8408c2108e6da5d23d637f062fa245fda830f03795e
5
+ SHA512:
6
+ metadata.gz: 9d88120599802fd04ff91582d2428dd7ff419111308b68712d431242474ec892e714b3c4c7013135a054beba8171ba6486adee7bc8e2c97192e9bfea5f1ffea6
7
+ data.tar.gz: 0e89c8fc6a71b85f721b23090b9e53cca189b89230313a6d29badd13b8ac3bbd6344af24dc8dfa5bc0bb083b4689640c9f7d74162a721142fd0c0e0176bc797c
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-04-24
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in faraday_loop.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ faraday_loop (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.18.0)
10
+ rake (13.0.6)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ faraday_loop!
17
+ minitest (~> 5.0)
18
+ rake (~> 13.0)
19
+
20
+ BUNDLED WITH
21
+ 2.3.22
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Sutro Labs Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # Faraday Loop
2
+
3
+ Ever need a nice way to retry a block of code? Maybe you need a maximum number of retries? Exponential backoff? Different handling for different exceptions?
4
+
5
+ [Faraday](https://github.com/lostisland/faraday) has one!
6
+
7
+ But, while Faraday is one nice piece of kit, you might not be in the market for an http client like Faraday and all of its Middelware.
8
+
9
+ So we took the [Farday Retry Middleware](https://github.com/lostisland/faraday-retry), and just simply cloned it into its own utility.
10
+
11
+ ## Installation
12
+
13
+ Install the gem and add to the application's Gemfile by executing:
14
+
15
+ $ bundle add faraday_loop
16
+
17
+ If bundler is not being used to manage dependencies, install the gem by executing:
18
+
19
+ $ gem install faraday_loop
20
+
21
+ ## Usage
22
+
23
+ You'll first need to require it:
24
+
25
+ ```ruby
26
+ require 'faraday_loop'
27
+ ```
28
+
29
+ Then, create a new instance of the retry utility, passing any options as needed:
30
+
31
+ ```ruby
32
+ retry_utility = FaradayLoop::Retry.new(
33
+ max: 3,
34
+ interval: 1,
35
+ max_interval: 10,
36
+ interval_randomness: 0.5,
37
+ backoff_factor: 2,
38
+ exceptions: [MyCustomException],
39
+ retry_if: ->(e) { e.is_a?(MyCustomException) },
40
+ retry_block: ->(retries, e) { puts "Retry #{retries}: #{e.message}" }
41
+ )
42
+ ```
43
+
44
+ - max: The maximum number of retries (default: 2)
45
+ - interval: The initial interval between retries in seconds (default: 0)
46
+ - max_interval: The maximum interval between retries in seconds (default: Float::MAX)
47
+ - interval_randomness: A factor to add randomness to the retry intervals (default: 0)
48
+ - backoff_factor: The factor to multiply the interval by for each retry (default: 1)
49
+ - exceptions: An array of exception classes to retry (default: Faraday::RetriableExceptions)
50
+ - retry_if: A lambda that returns true if the exception should be retried (default: checks if the exception is in the exceptions array)
51
+ - retry_block: An optional lambda that is called before each retry, passing in the current retry count and the exception raised
52
+
53
+ Finally, wrap the operation you want to retry in a block:
54
+
55
+ ```ruby
56
+ retry_utility.call do
57
+ # Your operation that might fail and require retries
58
+ end
59
+ ```
60
+
61
+ You can also shortcut this with:
62
+
63
+ ```ruby
64
+ retry_utility = FaradayLoop::Retry(
65
+ options
66
+ ).retry do
67
+ # Your operation that might fail and require retries
68
+ end
69
+ ```
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
74
+
75
+ Feedback
76
+ --------
77
+ [Source code available on Github](https://github.com/sutrolabs/faraday_loop). Feedback and pull requests are greatly appreciated. Let us know if we can improve this.
78
+
79
+ From
80
+ -----------
81
+ :wave: The folks at lotisland really made this: https://github.com/lostisland/faraday-retry. We at [Census](http://getcensus.com) just hoisted it up.
82
+
83
+ Interested in other things we do? **[Come work with us](https://www.getcensus.com/careers)**.
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FaradayLoop
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "faraday_loop/version"
4
+
5
+ module FaradayLoop
6
+ class Error < StandardError; end
7
+
8
+ # Clones the Faraday request retry middleware behavior.
9
+ # (https://github.com/lostisland/faraday/blob/v1.3.1/lib/faraday/request/retry.rb)
10
+ #
11
+ # Allows us to apply this familiar retry/backoff machinery outside of Faraday.
12
+ class Retry
13
+ def self.retry(
14
+ max: nil,
15
+ interval: nil,
16
+ max_interval: nil,
17
+ interval_randomness: nil,
18
+ backoff_factor: nil,
19
+ exceptions: nil,
20
+ retry_if: nil,
21
+ retry_block: nil,
22
+ &block
23
+ )
24
+ retry_handler =
25
+ Retry.new(
26
+ max: max,
27
+ interval: interval,
28
+ max_interval: max_interval,
29
+ interval_randomness: interval_randomness,
30
+ backoff_factor: backoff_factor,
31
+ exceptions: exceptions,
32
+ retry_if: retry_if,
33
+ retry_block: retry_block
34
+ )
35
+ retry_handler.call(&block)
36
+ end
37
+
38
+ DEFAULT_EXCEPTIONS = [StandardError].freeze
39
+ DEFAULT_CHECK = ->(_exception) { true }
40
+
41
+ def initialize(
42
+ max: nil,
43
+ interval: nil,
44
+ max_interval: nil,
45
+ interval_randomness: nil,
46
+ backoff_factor: nil,
47
+ exceptions: nil,
48
+ retry_if: nil,
49
+ retry_block: nil
50
+ )
51
+ @max = max || 2
52
+ @interval = interval || 0
53
+ @max_interval = max_interval || Float::MAX
54
+ @interval_randomness = interval_randomness || 0
55
+ @backoff_factor = backoff_factor || 1
56
+ @exceptions = Array(exceptions || DEFAULT_EXCEPTIONS)
57
+ @retry_if = retry_if || DEFAULT_CHECK
58
+ @retry_block = retry_block || proc { |_, _| }
59
+ end
60
+
61
+ def call(&block)
62
+ retries = @max
63
+ begin
64
+ block.call
65
+ rescue => e
66
+ # Yes, we really want to catch all error types here; the caller is in
67
+ # full control of which errors to retry.
68
+ raise unless matched_error?(e)
69
+
70
+ if retries.positive? && retry_request?(e)
71
+ retries -= 1
72
+ @retry_block.call(retries, e)
73
+ if (sleep_amount = calculate_retry_interval(retries + 1))
74
+ Kernel.sleep sleep_amount
75
+ retry
76
+ end
77
+ end
78
+
79
+ raise
80
+ end
81
+ end
82
+
83
+ private
84
+
85
+ def matched_error?(error)
86
+ @exceptions.any? do |ex|
87
+ if ex.is_a? Module
88
+ error.is_a? ex
89
+ else
90
+ error.class.to_s == ex.to_s
91
+ end
92
+ end
93
+ end
94
+
95
+ def retry_request?(exception)
96
+ @retry_if.call(exception)
97
+ end
98
+
99
+ def calculate_retry_interval(retries)
100
+ retry_index = @max - retries
101
+ current_interval = @interval * (@backoff_factor**retry_index)
102
+ current_interval = [current_interval, @max_interval].min
103
+ random_interval = rand * @interval_randomness.to_f * @interval
104
+ current_interval + random_interval
105
+ end
106
+ end
107
+ end
metadata ADDED
@@ -0,0 +1,55 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: faraday_loop
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - n8
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-04-27 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Faraday Loop provides an easy-to-use mechanism for retrying failed operations
14
+ in any Ruby application, independent of the full Faraday library.
15
+ email:
16
+ - nate@getcensus.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - CHANGELOG.md
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - lib/faraday_loop.rb
28
+ - lib/faraday_loop/version.rb
29
+ homepage: https://github.com/sutrolabs/faraday_loop
30
+ licenses:
31
+ - MIT
32
+ metadata:
33
+ homepage_uri: https://github.com/sutrolabs/faraday_loop
34
+ source_code_uri: https://github.com/sutrolabs/faraday_loop
35
+ changelog_uri: https://github.com/sutrolabs/faraday_loop/CHANGELOG.md
36
+ post_install_message:
37
+ rdoc_options: []
38
+ require_paths:
39
+ - lib
40
+ required_ruby_version: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - ">="
43
+ - !ruby/object:Gem::Version
44
+ version: 2.6.0
45
+ required_rubygems_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ requirements: []
51
+ rubygems_version: 3.1.6
52
+ signing_key:
53
+ specification_version: 4
54
+ summary: A lightweight, generic retry utility extracted from the Faraday Ruby project
55
+ test_files: []