rack-idempotent 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ rvm:
2
+ - 1.8.7
3
+ - 1.9.3
4
+ - rbx
5
+ notifications:
6
+ recipients:
7
+ - drnicwilliams@gmail.com
8
+ - isombra@engineyard.com
9
+ - jhansen@engineyard.com
10
+ branches:
11
+ only:
12
+ - master
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack-idempotent.gemspec
4
+ gemspec
5
+
6
+ group(:test) do
7
+ gem 'rake'
8
+ gem 'rack-client', :require => 'rack/client'
9
+ gem 'rspec'
10
+ end
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,10 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new do |t|
6
+ t.rspec_opts = %w[--color]
7
+ t.pattern = 'spec/**/*_spec.rb'
8
+ end
9
+ task :test => :spec
10
+ task :default => :spec
@@ -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,5 @@
1
+ module Rack
2
+ class Idempotent
3
+ VERSION = "0.0.1"
4
+ end
5
+ 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
@@ -0,0 +1,3 @@
1
+ Bundler.require(:test)
2
+
3
+ require File.expand_path("../../lib/rack-idempotent", __FILE__)
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