http_retriable 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +79 -0
- data/Rakefile +2 -0
- data/http_retriable.gemspec +17 -0
- data/lib/http_retriable.rb +57 -0
- data/lib/http_retriable/version.rb +3 -0
- data/test/retry_test.rb +17 -0
- metadata +57 -0
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
http_retriable
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-1.9.3-p448
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 Tyler Montgomery
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# HttpRetriable
|
|
2
|
+
|
|
3
|
+
HttpRetriable is designed to catch routine http errors and retry the provided
|
|
4
|
+
block that makes network calls.
|
|
5
|
+
|
|
6
|
+
By default, requests are tried 5 times with an exponential backoff to prevent
|
|
7
|
+
thrashing a service that may need a few seconds to recover.
|
|
8
|
+
|
|
9
|
+
You can optionally provide a constant amount of time to sleep between retries
|
|
10
|
+
and provide the number of times to retry the request if 5 retries doesn't suit
|
|
11
|
+
your neeeds.
|
|
12
|
+
|
|
13
|
+
Unlike other libraries that provide a similar function this does not polute
|
|
14
|
+
Kernal or Object with additional methods. You can include or extend HttpRetriable
|
|
15
|
+
for use within your own class or module, or you can call the module directly,
|
|
16
|
+
examples are below.
|
|
17
|
+
|
|
18
|
+
##Examples:
|
|
19
|
+
|
|
20
|
+
###Calling directly:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
def get_funny_cats(id)
|
|
24
|
+
HttpRetriable.call do
|
|
25
|
+
LulzService.get("/cats/lulz/#{id}")
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
###Mixing it in:
|
|
31
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
class FunnyCats
|
|
34
|
+
|
|
35
|
+
include HttpRetriable
|
|
36
|
+
|
|
37
|
+
def get_funny_cats(id)
|
|
38
|
+
retry_http do
|
|
39
|
+
LulzService.get("/cats/lulz/#{id}")
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Options
|
|
46
|
+
You can provide several options:
|
|
47
|
+
* retries - Integer: The number of times to retry the request
|
|
48
|
+
* sleep - Integer: The number of seconds to sleep
|
|
49
|
+
* exceptions - Array: List of exceptions classes
|
|
50
|
+
|
|
51
|
+
```ruby
|
|
52
|
+
def get_funny_cats(id)
|
|
53
|
+
exceptions = [LulzService::Error, Timeout::Error]
|
|
54
|
+
HttpRetriable.call(:retries => 3, :sleep => 10, :exceptions => exceptions) do
|
|
55
|
+
LulzService.get("/cats/lulz/#{id}")
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Installation
|
|
61
|
+
|
|
62
|
+
Add this line to your application's Gemfile:
|
|
63
|
+
|
|
64
|
+
gem 'http_retriable'
|
|
65
|
+
|
|
66
|
+
And then execute:
|
|
67
|
+
|
|
68
|
+
$ bundle
|
|
69
|
+
|
|
70
|
+
Or install it yourself as:
|
|
71
|
+
|
|
72
|
+
$ gem install http_retriable
|
|
73
|
+
|
|
74
|
+
## Contributing
|
|
75
|
+
|
|
76
|
+
1. Fork it.
|
|
77
|
+
2. Branch it.
|
|
78
|
+
3. Code it.
|
|
79
|
+
4. Pull Request it.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
require File.expand_path('../lib/http_retriable/version', __FILE__)
|
|
3
|
+
|
|
4
|
+
Gem::Specification.new do |gem|
|
|
5
|
+
gem.authors = ["Tyler Montgomery"]
|
|
6
|
+
gem.email = ["tyler.a.montgomery@gmail.com"]
|
|
7
|
+
gem.description = %q{Catches common http exceptions and retries your block}
|
|
8
|
+
gem.summary = %q{}
|
|
9
|
+
gem.homepage = ""
|
|
10
|
+
|
|
11
|
+
gem.files = `git ls-files`.split($\)
|
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
14
|
+
gem.name = "http_retriable"
|
|
15
|
+
gem.require_paths = ["lib"]
|
|
16
|
+
gem.version = HttpRetriable::VERSION
|
|
17
|
+
end
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require "http_retriable/version"
|
|
2
|
+
module HttpRetriable
|
|
3
|
+
|
|
4
|
+
extend self
|
|
5
|
+
|
|
6
|
+
def retry_http(*args, &block)
|
|
7
|
+
options = args.extract_options!
|
|
8
|
+
default_exceptions = [
|
|
9
|
+
EOFError,
|
|
10
|
+
Errno::ECONNREFUSED,
|
|
11
|
+
Errno::ECONNRESET,
|
|
12
|
+
Errno::EHOSTUNREACH,
|
|
13
|
+
Errno::EINVAL,
|
|
14
|
+
Errno::EPIPE,
|
|
15
|
+
Errno::ETIMEDOUT]
|
|
16
|
+
if defined?(RestClient)
|
|
17
|
+
default_exceptions += [RestClient::RequestTimeout, RestClient::ServerBrokeConnection]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
retries = options.fetch(:retries, 5)
|
|
21
|
+
should_sleep = options.fetch(:sleep, false)
|
|
22
|
+
backoff = !should_sleep # if sleep is provided by the user, don't backoff
|
|
23
|
+
exceptions = options.fetch(:exceptions, default_exceptions)
|
|
24
|
+
|
|
25
|
+
retried = 0
|
|
26
|
+
seconds_to_sleep = should_sleep ? should_sleep : 2
|
|
27
|
+
quick_retries = 2
|
|
28
|
+
begin
|
|
29
|
+
yield
|
|
30
|
+
rescue *exceptions => e
|
|
31
|
+
retried += 1
|
|
32
|
+
if backoff
|
|
33
|
+
if retried < quick_retries
|
|
34
|
+
retry
|
|
35
|
+
elsif retried < retries
|
|
36
|
+
seconds_to_sleep = seconds_to_sleep ** 2
|
|
37
|
+
sleep seconds_to_sleep
|
|
38
|
+
retry
|
|
39
|
+
else
|
|
40
|
+
raise e
|
|
41
|
+
end
|
|
42
|
+
else
|
|
43
|
+
if retried < retries
|
|
44
|
+
sleep seconds_to_sleep
|
|
45
|
+
retry
|
|
46
|
+
else
|
|
47
|
+
raise e
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def self.call(*args, &block)
|
|
54
|
+
self.retry_http(*args, &block)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
data/test/retry_test.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'bundler/setup'
|
|
2
|
+
require 'http_retriable'
|
|
3
|
+
require 'minitest/autorun'
|
|
4
|
+
require 'minitest/spec'
|
|
5
|
+
require 'fakeweb'
|
|
6
|
+
|
|
7
|
+
class RetryTest < MiniTest::Spec
|
|
8
|
+
|
|
9
|
+
it "should back off" do
|
|
10
|
+
#verify how much time it takes to execute
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it "should sleep constantly" do
|
|
14
|
+
# verify time it takes to execute"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: http_retriable
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
prerelease:
|
|
6
|
+
platform: ruby
|
|
7
|
+
authors:
|
|
8
|
+
- Tyler Montgomery
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2013-10-25 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: Catches common http exceptions and retries your block
|
|
15
|
+
email:
|
|
16
|
+
- tyler.a.montgomery@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- .gitignore
|
|
22
|
+
- .ruby-gemset
|
|
23
|
+
- .ruby-version
|
|
24
|
+
- Gemfile
|
|
25
|
+
- LICENSE
|
|
26
|
+
- README.md
|
|
27
|
+
- Rakefile
|
|
28
|
+
- http_retriable.gemspec
|
|
29
|
+
- lib/http_retriable.rb
|
|
30
|
+
- lib/http_retriable/version.rb
|
|
31
|
+
- test/retry_test.rb
|
|
32
|
+
homepage: ''
|
|
33
|
+
licenses: []
|
|
34
|
+
post_install_message:
|
|
35
|
+
rdoc_options: []
|
|
36
|
+
require_paths:
|
|
37
|
+
- lib
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
none: false
|
|
40
|
+
requirements:
|
|
41
|
+
- - ! '>='
|
|
42
|
+
- !ruby/object:Gem::Version
|
|
43
|
+
version: '0'
|
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
45
|
+
none: false
|
|
46
|
+
requirements:
|
|
47
|
+
- - ! '>='
|
|
48
|
+
- !ruby/object:Gem::Version
|
|
49
|
+
version: '0'
|
|
50
|
+
requirements: []
|
|
51
|
+
rubyforge_project:
|
|
52
|
+
rubygems_version: 1.8.25
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 3
|
|
55
|
+
summary: ''
|
|
56
|
+
test_files:
|
|
57
|
+
- test/retry_test.rb
|