reattempt 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +98 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/reattempt.rb +144 -0
- data/lib/reattempt/version.rb +5 -0
- data/reattempt.gemspec +43 -0
- metadata +126 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 066604102e5490b27868c7a4cfd1d3b83a2f87187d5de6cec16a44278e93cb3d
|
4
|
+
data.tar.gz: 3ca36c4e76b52f157e0c87f48f1e3a084c139e23957ef340c3c7b181537124aa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0315c05322d5e7967c0a45f6d008169f68457221e643ed6824d1710249c1beb34ff69963aafd048f95504332750eec76c70dcbe3f7e1122e5bde1cf326619d14
|
7
|
+
data.tar.gz: 99725584d39fe74e62ce398697ccec2784a390f17b65aae877c760659ac6196a6c524bc1309e848bd5ecafa91ab9ec54151f8061398a83d05f8b1f890f8f5ccc
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Thomas Hurst
|
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,98 @@
|
|
1
|
+
# Reattempt
|
2
|
+
|
3
|
+
Simple `Enumerable` APIs offering retries with exponential backoff and jitter.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'reattempt'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install reattempt
|
20
|
+
|
21
|
+
## Synopsis
|
22
|
+
|
23
|
+
Simplest use with the defaults - 5 attempts, 0.02 to 1 second delay, 0.2 jitter
|
24
|
+
(delay is randomised ±10%), catching `StandardError`:
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
begin
|
28
|
+
Reattempt::Retry.new.each do
|
29
|
+
poke_remote_api
|
30
|
+
end
|
31
|
+
rescue Reattempt::RetriesExceeded => e
|
32
|
+
handle_repeated_failure(e.cause)
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
Instances are thread-safe, and it's suggested that you separate their creation
|
37
|
+
from usage: inject them as dependencies, configure them in class attributes,
|
38
|
+
store them in constants, etc.
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Reattempt consists of two main classes:
|
43
|
+
|
44
|
+
### Backoff
|
45
|
+
|
46
|
+
`Backoff` implements a simple jittered exponential backoff calculator as an
|
47
|
+
`Enumerable`:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# Start delay 0.075-0.125 seconds, increasing to 0.75-1.25 seconds
|
51
|
+
bo = Reattempt::Backoff.new(min_delay: 0.1, max_delay: 1.0, jitter: 0.5)
|
52
|
+
|
53
|
+
bo.take(4).map { |x| x.round(4) } # => [0.1138, 0.2029, 0.4227, 0.646]
|
54
|
+
bo.take(2).each { |delay| sleep delay }
|
55
|
+
bo.delay_for_attempt(4) # => 1.0403524624141058
|
56
|
+
bo[4] # => 0.8328055668923606
|
57
|
+
|
58
|
+
bo.each do |delay|
|
59
|
+
printf("Sleeping for about %.2f seconds\n", delay)
|
60
|
+
sleep delay
|
61
|
+
end
|
62
|
+
```
|
63
|
+
|
64
|
+
Note `Backoff` is strictly a *calculator*, it does *not* implement sleep itself.
|
65
|
+
|
66
|
+
The iterator is unbounded and you're expected to `take` however many you need,
|
67
|
+
or manually exit the loop.
|
68
|
+
|
69
|
+
### Retry
|
70
|
+
|
71
|
+
`Retry` implements a retrying iterator, catching the given `Exception` types and
|
72
|
+
sleeping as per a configured `Backoff` instance.
|
73
|
+
|
74
|
+
```ruby
|
75
|
+
bo = Reattempt::Backoff.new(min_delay: 0.1, max_delay: 1.0, jitter: 0.5)
|
76
|
+
try = Reattempt::Retry.new(tries: 5, rescue: TempError, backoff: bo)
|
77
|
+
begin
|
78
|
+
try.each do |attempt|
|
79
|
+
raise TempError, "Failed in attempt #{attempt}"
|
80
|
+
end
|
81
|
+
rescue Reattempt::RetriesExceeded => e
|
82
|
+
p e.cause # => #<TempError: "Failed in attempt 5">
|
83
|
+
end
|
84
|
+
```
|
85
|
+
|
86
|
+
## Development
|
87
|
+
|
88
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
89
|
+
|
90
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/Freaky/ruby-reattempt.
|
95
|
+
|
96
|
+
## License
|
97
|
+
|
98
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "reattempt"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/reattempt.rb
ADDED
@@ -0,0 +1,144 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'reattempt/version'
|
4
|
+
|
5
|
+
require 'dry-initializer'
|
6
|
+
require 'dry-types'
|
7
|
+
|
8
|
+
module Reattempt
|
9
|
+
# Exception raised by +Retry.each+, with the error from the loop available
|
10
|
+
# in +RetriesExceeded#cause+.
|
11
|
+
RetriesExceeded = Class.new(StandardError)
|
12
|
+
|
13
|
+
# Calculate exponential backoff times between +min_delay+ and +max_delay+,
|
14
|
+
# with +jitter+ of between 0 and 1 and +factor+ of, by default, 2.
|
15
|
+
#
|
16
|
+
# Minimum delay is min_delay * jitter / 2, maximum is max_delay * jitter / 2.
|
17
|
+
#
|
18
|
+
# Instances are +Enumerable+.
|
19
|
+
#
|
20
|
+
# Example:
|
21
|
+
#
|
22
|
+
# ```ruby
|
23
|
+
# # Start delay 0.075-0.125 seconds, doubling to a limit of 0.75-1.25
|
24
|
+
# bo = Reattempt::Backoff.new(min_delay: 0.1, max_delay: 1.0, jitter: 0.5)
|
25
|
+
# bo.take(4).map { |x| x.round(4) } # => [0.1151, 0.1853, 0.4972, 0.9316]
|
26
|
+
# ```
|
27
|
+
class Backoff
|
28
|
+
include Enumerable
|
29
|
+
extend Dry::Initializer[undefined: false]
|
30
|
+
|
31
|
+
option :min_delay,
|
32
|
+
default: -> { 0.02 },
|
33
|
+
type: Dry::Types['coercible.float'].constrained(gteq: 0)
|
34
|
+
|
35
|
+
option :max_delay,
|
36
|
+
default: -> { 1.0 },
|
37
|
+
type: Dry::Types['coercible.float'].constrained(gteq: 0)
|
38
|
+
|
39
|
+
option :jitter,
|
40
|
+
default: -> { 0.2 },
|
41
|
+
type: Dry::Types['coercible.float'].constrained(lt: 1, gteq: 0)
|
42
|
+
|
43
|
+
option :factor,
|
44
|
+
default: -> { 2 },
|
45
|
+
type: Dry::Types['coercible.float'].constrained(gt: 0)
|
46
|
+
|
47
|
+
# Iterate over calls to +delay_for_attempt+ with a counter. If no block
|
48
|
+
# given, return an +Enumerator+.
|
49
|
+
def each
|
50
|
+
return enum_for(:each) unless block_given?
|
51
|
+
|
52
|
+
0.upto(Float::INFINITY) do |try|
|
53
|
+
yield delay_for_attempt(try)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# Calculate a randomised delay for attempt number +try+, starting from 0.
|
58
|
+
#
|
59
|
+
# Aliased to +[]+
|
60
|
+
def delay_for_attempt(try)
|
61
|
+
delay = (min_delay * (factor ** try)).clamp(min_delay, max_delay)
|
62
|
+
delay * Random.rand(jitter_range)
|
63
|
+
end
|
64
|
+
|
65
|
+
alias [] delay_for_attempt
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def jitter_range
|
70
|
+
@jitter_range ||= Range.new(1 - (jitter / 2), 1 + jitter / 2)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# Retry the loop iterator if configured caught exceptions are raised and retry
|
75
|
+
# count is not exceeded, sleeping as per a given backoff configuration.
|
76
|
+
#
|
77
|
+
# Example:
|
78
|
+
# ```ruby
|
79
|
+
# bo = Reattempt::Backoff.new(min_delay: 0.1, max_delay: 1.0, jitter: 0.5)
|
80
|
+
# try = Reattempt::Retry.new(tries: 5, rescue: TempError, backoff: bo)
|
81
|
+
# begin
|
82
|
+
# try.each do |attempt|
|
83
|
+
# raise TempError, "Failed in attempt #{attempt}"
|
84
|
+
# end
|
85
|
+
# rescue Reattempt::RetriesExceeded => e
|
86
|
+
# p e.cause # => #<TempError: "Failed in attempt 5">
|
87
|
+
# end
|
88
|
+
# ```
|
89
|
+
class Retry
|
90
|
+
include Enumerable
|
91
|
+
extend Dry::Initializer[undefined: false]
|
92
|
+
|
93
|
+
option :tries,
|
94
|
+
default: -> { 5 },
|
95
|
+
type: Dry::Types['strict.integer'].constrained(gteq: 0)
|
96
|
+
|
97
|
+
option :rescue,
|
98
|
+
default: -> { StandardError },
|
99
|
+
type: Dry::Types['coercible.array'].constrained(min_size: 1)
|
100
|
+
.of(Dry::Types::Any.constrained(attr: :===))
|
101
|
+
|
102
|
+
option :backoff,
|
103
|
+
default: -> { Backoff.new },
|
104
|
+
type: Dry::Types::Definition.new(Backoff).constrained(type: Backoff)
|
105
|
+
|
106
|
+
option :sleep_proc,
|
107
|
+
default: -> { Kernel.method(:sleep) },
|
108
|
+
type: Dry::Types::Any.constrained(attr: :call)
|
109
|
+
|
110
|
+
option :rescue_proc,
|
111
|
+
default: -> { ->(_ex) {} },
|
112
|
+
type: Dry::Types::Any.constrained(attr: :call)
|
113
|
+
|
114
|
+
# Yield the block with the current attempt number, starting from 1, for up
|
115
|
+
# to +tries+ times. Setting +tries+ to zero will result in an instant
|
116
|
+
# +RetriesExceeded+, which may be useful for testing.
|
117
|
+
#
|
118
|
+
# If any of the configured +rescue+ exceptions are raised (as matched by
|
119
|
+
# +===+), call +rescue_proc+ with the exception, call +sleep_proc+ with the
|
120
|
+
# delay as configured by +backoff+, and try again up to +retries+ times.
|
121
|
+
#
|
122
|
+
# +rescue_proc+ defaults to a no-op.
|
123
|
+
#
|
124
|
+
# +sleep_proc+ defaults to +Kernel#sleep+.
|
125
|
+
#
|
126
|
+
# Raise +RetriesExceeded+ when the count is exceeded, setting +cause+ to
|
127
|
+
# the previous exception.
|
128
|
+
def each
|
129
|
+
return enum_for(:each) unless block_given?
|
130
|
+
|
131
|
+
ex = nil
|
132
|
+
|
133
|
+
backoff.lazy.take(tries).each_with_index do |delay, try|
|
134
|
+
return yield(try + 1)
|
135
|
+
rescue Exception => ex
|
136
|
+
raise unless self.rescue.any? { |r| r === ex }
|
137
|
+
rescue_proc.call ex
|
138
|
+
sleep_proc.call delay
|
139
|
+
end
|
140
|
+
|
141
|
+
raise RetriesExceeded, cause: ex
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
data/reattempt.gemspec
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "reattempt/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "reattempt"
|
8
|
+
spec.version = Reattempt::VERSION
|
9
|
+
spec.authors = ["Thomas Hurst"]
|
10
|
+
spec.email = ["tom@hur.st"]
|
11
|
+
|
12
|
+
spec.summary = %q{Enumerable retries with exponential backoff}
|
13
|
+
spec.description = %q{Enumerable APIs for retries and exponential backoff with jitter}
|
14
|
+
spec.homepage = "https://github.com/Freaky/ruby-reattempt/"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.required_ruby_version = ">= 2.5"
|
18
|
+
|
19
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
20
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
21
|
+
if spec.respond_to?(:metadata)
|
22
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
23
|
+
else
|
24
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
25
|
+
"public gem pushes."
|
26
|
+
end
|
27
|
+
|
28
|
+
# Specify which files should be added to the gem when it is released.
|
29
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
30
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
31
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
32
|
+
end
|
33
|
+
spec.bindir = "exe"
|
34
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
35
|
+
spec.require_paths = ["lib"]
|
36
|
+
|
37
|
+
spec.add_dependency "dry-initializer"
|
38
|
+
spec.add_dependency "dry-types"
|
39
|
+
|
40
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
41
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
42
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reattempt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Thomas Hurst
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dry-initializer
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-types
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
description: Enumerable APIs for retries and exponential backoff with jitter
|
84
|
+
email:
|
85
|
+
- tom@hur.st
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE.txt
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- bin/console
|
97
|
+
- bin/setup
|
98
|
+
- lib/reattempt.rb
|
99
|
+
- lib/reattempt/version.rb
|
100
|
+
- reattempt.gemspec
|
101
|
+
homepage: https://github.com/Freaky/ruby-reattempt/
|
102
|
+
licenses:
|
103
|
+
- MIT
|
104
|
+
metadata:
|
105
|
+
allowed_push_host: https://rubygems.org
|
106
|
+
post_install_message:
|
107
|
+
rdoc_options: []
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: '2.5'
|
115
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
version: '0'
|
120
|
+
requirements: []
|
121
|
+
rubyforge_project:
|
122
|
+
rubygems_version: 2.7.6
|
123
|
+
signing_key:
|
124
|
+
specification_version: 4
|
125
|
+
summary: Enumerable retries with exponential backoff
|
126
|
+
test_files: []
|