client_proxy 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 James Golick
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = client_proxy
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 James Golick. See LICENSE for details.
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "client_proxy"
8
+ gem.summary = %Q{Basic ruby proxy class to use around service clients that handles retrying requests and failover.}
9
+ gem.description = %Q{Basic ruby proxy class to use around service clients that handles retrying requests and failover.}
10
+ gem.email = "jamesgolick@gmail.com"
11
+ gem.homepage = "http://github.com/jamesgolick/client_proxy"
12
+ gem.authors = ["James Golick"]
13
+ gem.add_development_dependency "rspec", ">= 1.2.9"
14
+ gem.add_development_dependency "bourne", "~> 1.0"
15
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ end
21
+
22
+ require 'spec/rake/spectask'
23
+ Spec::Rake::SpecTask.new(:spec) do |spec|
24
+ spec.libs << 'lib' << 'spec'
25
+ spec.spec_files = FileList['spec/**/*_spec.rb']
26
+ end
27
+
28
+ Spec::Rake::SpecTask.new(:rcov) do |spec|
29
+ spec.libs << 'lib' << 'spec'
30
+ spec.pattern = 'spec/**/*_spec.rb'
31
+ spec.rcov = true
32
+ end
33
+
34
+ task :spec => :check_dependencies
35
+
36
+ task :default => :spec
37
+
38
+ require 'rake/rdoctask'
39
+ Rake::RDocTask.new do |rdoc|
40
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
41
+
42
+ rdoc.rdoc_dir = 'rdoc'
43
+ rdoc.title = "client_proxy #{version}"
44
+ rdoc.rdoc_files.include('README*')
45
+ rdoc.rdoc_files.include('lib/**/*.rb')
46
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,60 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{client_proxy}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["James Golick"]
12
+ s.date = %q{2010-09-08}
13
+ s.description = %q{Basic ruby proxy class to use around service clients that handles retrying requests and failover.}
14
+ s.email = %q{jamesgolick@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "client_proxy.gemspec",
27
+ "lib/client_proxy.rb",
28
+ "spec/client_proxy_spec.rb",
29
+ "spec/retrying_client_proxy_spec.rb",
30
+ "spec/spec.opts",
31
+ "spec/spec_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/jamesgolick/client_proxy}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.7}
37
+ s.summary = %q{Basic ruby proxy class to use around service clients that handles retrying requests and failover.}
38
+ s.test_files = [
39
+ "spec/client_proxy_spec.rb",
40
+ "spec/retrying_client_proxy_spec.rb",
41
+ "spec/spec_helper.rb"
42
+ ]
43
+
44
+ if s.respond_to? :specification_version then
45
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_development_dependency(%q<rspec>, [">= 1.2.9"])
50
+ s.add_development_dependency(%q<bourne>, ["~> 1.0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
53
+ s.add_dependency(%q<bourne>, ["~> 1.0"])
54
+ end
55
+ else
56
+ s.add_dependency(%q<rspec>, [">= 1.2.9"])
57
+ s.add_dependency(%q<bourne>, ["~> 1.0"])
58
+ end
59
+ end
60
+
@@ -0,0 +1,38 @@
1
+ class ClientProxy
2
+ def initialize(client_factory, hosts, recoverable_errors, max_retries, randomize_order = true)
3
+ @client_factory = client_factory
4
+ @hosts = hosts
5
+ @recoverable_errors = recoverable_errors
6
+ @max_retries = max_retries
7
+ @host_pointer = randomize_order ? rand(hosts.length) : -1
8
+ end
9
+
10
+ def method_missing(method, *args, &block)
11
+ attempt(0, method, *args, &block)
12
+ end
13
+
14
+ def attempt(try, method, *args, &block)
15
+ begin
16
+ current_client.__send__(method, *args, &block)
17
+ rescue *@recoverable_errors => e
18
+ @current_client = nil
19
+ raise e if try == (@max_retries - 1)
20
+ attempt(try + 1, method, *args, &block)
21
+ end
22
+ end
23
+
24
+ private
25
+ def current_client
26
+ @current_client ||= next_client
27
+ end
28
+
29
+ def next_client
30
+ @client_factory.create(next_host)
31
+ end
32
+
33
+ def next_host
34
+ @host_pointer += 1
35
+ @host_pointer = 0 if @host_pointer == @hosts.length
36
+ @current_host = @hosts[@host_pointer]
37
+ end
38
+ end
@@ -0,0 +1,44 @@
1
+ require "spec_helper"
2
+
3
+ describe "ClientProxy" do
4
+ before do
5
+ @client_factory = stub("ClientFactory")
6
+ @host1 = stub("Client(host1)", :do_something => true)
7
+ @host2 = stub("Client(host2)", :do_something => true)
8
+ @client_factory.stubs(:create).with("host1:6379").returns(@host1)
9
+ @client_factory.stubs(:create).with("host2:6379").returns(@host2)
10
+ @client_proxy = ClientProxy.new(@client_factory,
11
+ ["host1:6379", "host2:6379"],
12
+ [RuntimeError],
13
+ 3,
14
+ false)
15
+ end
16
+
17
+ describe "when a request succeeds" do
18
+ it "tries the first host" do
19
+ @client_proxy.do_something.should == true
20
+ @host1.should have_received(:do_something)
21
+ end
22
+ end
23
+
24
+ describe "when a request fails with a recoverable exception" do
25
+ it "recovers and attempts the next host" do
26
+ @host1.stubs(:do_something).raises(RuntimeError, "Fuck!")
27
+ @client_proxy.do_something.should == true
28
+ @host1.should have_received(:do_something)
29
+ @host2.should have_received(:do_something)
30
+ end
31
+ end
32
+
33
+ describe "when a request fails more than max_retries times" do
34
+ it "the error bubbles up" do
35
+ @host1.stubs(:do_something).raises(RuntimeError, "Fuck!")
36
+ @host2.stubs(:do_something).raises(RuntimeError, "Fuck!")
37
+ lambda {
38
+ @client_proxy.do_something
39
+ }.should raise_error(RuntimeError)
40
+ @host1.should have_received(:do_something).twice
41
+ @host2.should have_received(:do_something).once
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,11 @@
1
+ require "spec_helper"
2
+
3
+ describe "ClientProxy::RetryingClient" do
4
+ before do
5
+ @client_factory = stub("ClientFactory")
6
+ @host1 = stub("Client::Host1")
7
+ @host2 = stub("Client::Host2")
8
+ @client_factory.stubs(:create).with("host1:5432").returns(@host1)
9
+ @client_factory.stubs(:create).with("host2:5432").returns(@host2)
10
+ end
11
+ end
@@ -0,0 +1 @@
1
+ --color
@@ -0,0 +1,10 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+ require 'client_proxy'
4
+ require 'spec'
5
+ require 'spec/autorun'
6
+ require 'bourne'
7
+
8
+ Spec::Runner.configure do |config|
9
+ config.mock_with :mocha
10
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: client_proxy
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - James Golick
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-08 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rspec
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 13
30
+ segments:
31
+ - 1
32
+ - 2
33
+ - 9
34
+ version: 1.2.9
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bourne
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 1
48
+ - 0
49
+ version: "1.0"
50
+ type: :development
51
+ version_requirements: *id002
52
+ description: Basic ruby proxy class to use around service clients that handles retrying requests and failover.
53
+ email: jamesgolick@gmail.com
54
+ executables: []
55
+
56
+ extensions: []
57
+
58
+ extra_rdoc_files:
59
+ - LICENSE
60
+ - README.rdoc
61
+ files:
62
+ - .document
63
+ - .gitignore
64
+ - LICENSE
65
+ - README.rdoc
66
+ - Rakefile
67
+ - VERSION
68
+ - client_proxy.gemspec
69
+ - lib/client_proxy.rb
70
+ - spec/client_proxy_spec.rb
71
+ - spec/retrying_client_proxy_spec.rb
72
+ - spec/spec.opts
73
+ - spec/spec_helper.rb
74
+ has_rdoc: true
75
+ homepage: http://github.com/jamesgolick/client_proxy
76
+ licenses: []
77
+
78
+ post_install_message:
79
+ rdoc_options:
80
+ - --charset=UTF-8
81
+ require_paths:
82
+ - lib
83
+ required_ruby_version: !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ hash: 3
89
+ segments:
90
+ - 0
91
+ version: "0"
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ hash: 3
98
+ segments:
99
+ - 0
100
+ version: "0"
101
+ requirements: []
102
+
103
+ rubyforge_project:
104
+ rubygems_version: 1.3.7
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: Basic ruby proxy class to use around service clients that handles retrying requests and failover.
108
+ test_files:
109
+ - spec/client_proxy_spec.rb
110
+ - spec/retrying_client_proxy_spec.rb
111
+ - spec/spec_helper.rb