rack-idempotent 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.travis.yml +12 -0
- data/Gemfile +10 -0
- data/LICENSE +22 -0
- data/README.md +62 -0
- data/Rakefile +10 -0
- data/lib/rack-idempotent.rb +52 -0
- data/lib/rack-idempotent/version.rb +5 -0
- data/rack-idempotent.gemspec +17 -0
- data/spec/rack-idempotent_spec.rb +74 -0
- data/spec/spec_helper.rb +3 -0
- metadata +58 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ines Sombra
|
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,62 @@
|
|
1
|
+
# Rack::Idempotent
|
2
|
+
|
3
|
+
This rack middleware intends to handle retry logic for rack-client.
|
4
|
+
|
5
|
+
Rack::Idempotent rescues and retries low-level errors and 'safe to retry' http response codes.
|
6
|
+
|
7
|
+
Default retry limit is currently set to 5.
|
8
|
+
|
9
|
+
Handled low-level Net::HTTP exceptions include:
|
10
|
+
|
11
|
+
* `Errno::ETIMEDOUT`
|
12
|
+
* `Errno::ECONNREFUSED`
|
13
|
+
* `Errno::EHOSTUNREACH`
|
14
|
+
|
15
|
+
Response status codes that are retried:
|
16
|
+
|
17
|
+
* 408: Request Timeout
|
18
|
+
* 502: Bad Gateway
|
19
|
+
* 503: Service Unavailable
|
20
|
+
* 504: Gateway Timeout
|
21
|
+
|
22
|
+
If the retry limit is exceeded, Rack::Idemptotent will raise `Rack::Idempotent::RetryLimitExceeded`.
|
23
|
+
|
24
|
+
The exceptions raised are stored as an array available via `Rack::Idempotent::RetryLimitExceeded#idempotent_exceptions`
|
25
|
+
|
26
|
+
## Installation
|
27
|
+
|
28
|
+
Add this line to your application's Gemfile:
|
29
|
+
|
30
|
+
gem 'rack-idempotent'
|
31
|
+
|
32
|
+
And then execute:
|
33
|
+
|
34
|
+
$ bundle
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
|
38
|
+
$ gem install rack-idempotent
|
39
|
+
|
40
|
+
## Usage
|
41
|
+
|
42
|
+
Add Rack::Idempotent as a Rack::Client middleware as close to the handler as possible:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
client = Rack::Client.new do
|
46
|
+
use EY::ApiHMAC::ApiAuth::Client, *ServiceClient.hmac_keys
|
47
|
+
use Rack::Idempotent
|
48
|
+
run Rack::Client::Handler::NetHTTP
|
49
|
+
end
|
50
|
+
```
|
51
|
+
|
52
|
+
## Running Tests
|
53
|
+
|
54
|
+
$ bundle exec rake
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
1. Fork it
|
59
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
60
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
61
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
62
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require "rack-idempotent/version"
|
2
|
+
|
3
|
+
module Rack
|
4
|
+
class Idempotent
|
5
|
+
RETRY_LIMIT = 5
|
6
|
+
IDEMPOTENT_HTTP_CODES = [502, 503, 504, 408]
|
7
|
+
IDEMPOTENT_ERROR_CLASSES = [Errno::ETIMEDOUT, Errno::ECONNREFUSED, Errno::EHOSTUNREACH]
|
8
|
+
|
9
|
+
class RetryLimitExceeded < Exception
|
10
|
+
attr_reader :idempotent_exceptions
|
11
|
+
def initialize(idempotent_exceptions)
|
12
|
+
@idempotent_exceptions = idempotent_exceptions
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class HTTPException < Exception
|
17
|
+
attr_reader :status, :headers, :body
|
18
|
+
def initialize(status, headers, body)
|
19
|
+
@status, @headers, @body = status, headers, body
|
20
|
+
end
|
21
|
+
|
22
|
+
def to_s
|
23
|
+
@status.to_s
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def initialize(app)
|
28
|
+
@app= app
|
29
|
+
end
|
30
|
+
|
31
|
+
def call(env)
|
32
|
+
env['client.retries'] = 0
|
33
|
+
status, headers, body = nil
|
34
|
+
idempotent_exceptions = []
|
35
|
+
begin
|
36
|
+
dup_env = env.dup
|
37
|
+
status, headers, body = @app.call(dup_env)
|
38
|
+
raise HTTPException.new(status, headers, body) if IDEMPOTENT_HTTP_CODES.include?(status)
|
39
|
+
env.merge!(dup_env)
|
40
|
+
[status, headers, body]
|
41
|
+
rescue *IDEMPOTENT_ERROR_CLASSES, HTTPException => ie
|
42
|
+
idempotent_exceptions << ie
|
43
|
+
if env['client.retries'] > RETRY_LIMIT - 1
|
44
|
+
raise(RetryLimitExceeded.new(idempotent_exceptions))
|
45
|
+
else
|
46
|
+
env['client.retries'] += 1
|
47
|
+
retry
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/rack-idempotent/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ines Sombra"]
|
6
|
+
gem.email = ["isombra@engineyard.com"]
|
7
|
+
gem.description = %q{Retry logic for rack-client}
|
8
|
+
gem.summary = %q{Retry logic for rack-client}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
12
|
+
gem.files = `git ls-files`.split("\n")
|
13
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
|
+
gem.name = "rack-idempotent"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Rack::Idempotent::VERSION
|
17
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Rack::Idempotent do
|
4
|
+
class CaptureEnv
|
5
|
+
class << self; attr_accessor :env; end
|
6
|
+
def initialize(app); @app=app; end
|
7
|
+
def call(env)
|
8
|
+
@app.call(env)
|
9
|
+
ensure
|
10
|
+
self.class.env = env
|
11
|
+
end
|
12
|
+
end
|
13
|
+
class RaiseUp
|
14
|
+
class << self; attr_accessor :errors; end
|
15
|
+
def self.call(env)
|
16
|
+
error = self.errors.shift
|
17
|
+
raise error if error.is_a?(Class)
|
18
|
+
status_code = error || 200
|
19
|
+
[status_code, {}, []]
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
before(:each){ CaptureEnv.env = nil }
|
24
|
+
let(:client) do
|
25
|
+
Rack::Client.new do
|
26
|
+
use CaptureEnv
|
27
|
+
use Rack::Idempotent
|
28
|
+
run RaiseUp
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should retry Errno::ETIMEDOUT" do
|
33
|
+
RaiseUp.errors = [Errno::ETIMEDOUT, Errno::ETIMEDOUT]
|
34
|
+
client.get("/doesntmatter")
|
35
|
+
|
36
|
+
env = CaptureEnv.env
|
37
|
+
env['client.retries'].should == 2
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should raise Rack::Idempotent::RetryLimitExceeded when retry limit is reached" do
|
41
|
+
RaiseUp.errors = (Rack::Idempotent::RETRY_LIMIT + 1).times.map{|i| Errno::ETIMEDOUT}
|
42
|
+
|
43
|
+
lambda { client.get("/doesntmatter") }.should raise_exception(Rack::Idempotent::RetryLimitExceeded)
|
44
|
+
|
45
|
+
env = CaptureEnv.env
|
46
|
+
env['client.retries'].should == Rack::Idempotent::RETRY_LIMIT
|
47
|
+
end
|
48
|
+
|
49
|
+
[502, 503, 504, 408].each do |code|
|
50
|
+
it "retries #{code}" do
|
51
|
+
RaiseUp.errors = [code]
|
52
|
+
client.get("/something")
|
53
|
+
env = CaptureEnv.env
|
54
|
+
env['client.retries'].should == 1
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should store exceptions raised" do
|
59
|
+
RaiseUp.errors = [502, Errno::ECONNREFUSED, 408, 504, Errno::EHOSTUNREACH, Errno::ETIMEDOUT]
|
60
|
+
errors = RaiseUp.errors.dup
|
61
|
+
exception = nil
|
62
|
+
|
63
|
+
begin
|
64
|
+
client.get("/doesntmatter")
|
65
|
+
rescue Rack::Idempotent::RetryLimitExceeded => e
|
66
|
+
exception = e
|
67
|
+
end
|
68
|
+
|
69
|
+
exception.should_not be_nil
|
70
|
+
exception.idempotent_exceptions.size.should == 6
|
71
|
+
exception.idempotent_exceptions.map{|ie| ie.is_a?(Rack::Idempotent::HTTPException) ? ie.status : ie.class}.should == errors
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rack-idempotent
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ines Sombra
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-01-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Retry logic for rack-client
|
15
|
+
email:
|
16
|
+
- isombra@engineyard.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .travis.yml
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- lib/rack-idempotent.rb
|
28
|
+
- lib/rack-idempotent/version.rb
|
29
|
+
- rack-idempotent.gemspec
|
30
|
+
- spec/rack-idempotent_spec.rb
|
31
|
+
- spec/spec_helper.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.10
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Retry logic for rack-client
|
56
|
+
test_files:
|
57
|
+
- spec/rack-idempotent_spec.rb
|
58
|
+
- spec/spec_helper.rb
|