stillwater 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/.rvmrc +1 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +50 -0
- data/Rakefile +6 -0
- data/lib/stillwater.rb +5 -0
- data/lib/stillwater/connection_pool.rb +122 -0
- data/lib/stillwater/version.rb +3 -0
- data/spec/connection_pool_spec.rb +173 -0
- data/spec/spec_helper.rb +14 -0
- data/stillwater.gemspec +23 -0
- metadata +137 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.8.7@balancer --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Trae Robrock
|
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,50 @@
|
|
1
|
+
# Stillwater
|
2
|
+
|
3
|
+
A simple connection pool, that allows connections to different servers (or anything else)
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'stillwater'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install stillwater
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
pool = Stillwater::ConnectionPool.new
|
22
|
+
%q{ host1.com host2.com }.each do |host|
|
23
|
+
pool.add { MyConnectionClass.new(host) }
|
24
|
+
end
|
25
|
+
|
26
|
+
# Basic connection handling
|
27
|
+
pool.with_connection do |connection|
|
28
|
+
# Do some stuff with your connection
|
29
|
+
end
|
30
|
+
|
31
|
+
# Retry connections
|
32
|
+
# This will retry your code with a new connection and mark the tried
|
33
|
+
# connection as bad. The bad connection will be put back in the pool
|
34
|
+
# at the default period of 5 minutes.
|
35
|
+
pool.retry_connection_from(ServerConnectionFailed) do |connection|
|
36
|
+
# Do some stuff with your connection
|
37
|
+
end
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create new Pull Request
|
46
|
+
|
47
|
+
## Contributors
|
48
|
+
|
49
|
+
* Trae Robrock ( https://github.com/trobrock )
|
50
|
+
* Julio Santos ( https://github.com/julio )
|
data/Rakefile
ADDED
data/lib/stillwater.rb
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
require 'active_support/core_ext/array'
|
2
|
+
|
3
|
+
module Stillwater
|
4
|
+
class ConnectionNotAvailable < StandardError ; end
|
5
|
+
|
6
|
+
class ConnectionPool
|
7
|
+
attr_accessor :reactivate_timeout, :retry_count
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@pool = []
|
11
|
+
@reactivate_timeout = 5 * 60 # 5 minutes
|
12
|
+
@retry_count = 3
|
13
|
+
end
|
14
|
+
|
15
|
+
def add(&builder)
|
16
|
+
@pool << connection_info_from(builder)
|
17
|
+
end
|
18
|
+
|
19
|
+
def reactivate_timeout=(seconds)
|
20
|
+
running = !!@thread
|
21
|
+
@thread.kill if running
|
22
|
+
@thread = nil
|
23
|
+
@reactivate_timeout = seconds
|
24
|
+
start_polling if running
|
25
|
+
end
|
26
|
+
|
27
|
+
def with_connection(&block)
|
28
|
+
conn = checkout
|
29
|
+
result = yield conn
|
30
|
+
checkin conn
|
31
|
+
|
32
|
+
result
|
33
|
+
end
|
34
|
+
|
35
|
+
def retry_connection_from(exception_class, &block)
|
36
|
+
count = 0
|
37
|
+
conn = checkout
|
38
|
+
yield conn
|
39
|
+
rescue exception_class
|
40
|
+
deactivate conn
|
41
|
+
count += 1
|
42
|
+
raise if count >= @retry_count
|
43
|
+
retry
|
44
|
+
ensure
|
45
|
+
checkin conn
|
46
|
+
end
|
47
|
+
|
48
|
+
def checkout
|
49
|
+
connection_info = available.sample
|
50
|
+
raise ConnectionNotAvailable if connection_info.nil?
|
51
|
+
connection_info[:state] = :in_use
|
52
|
+
|
53
|
+
connection_info[:connection]
|
54
|
+
end
|
55
|
+
|
56
|
+
def checkin(conn)
|
57
|
+
connection_info = in_use.detect { |info| info[:connection] == conn }
|
58
|
+
connection_info[:state] = :available unless connection_info.nil?
|
59
|
+
|
60
|
+
true
|
61
|
+
end
|
62
|
+
|
63
|
+
def deactivate(conn)
|
64
|
+
connection_info = @pool.detect { |info| info[:connection] == conn }
|
65
|
+
connection_info[:connection] = nil
|
66
|
+
connection_info[:state] = :inactive
|
67
|
+
|
68
|
+
start_polling
|
69
|
+
true
|
70
|
+
end
|
71
|
+
|
72
|
+
def reactivate_all
|
73
|
+
inactive.each do |info|
|
74
|
+
info[:connection] = info[:builder].call
|
75
|
+
info[:state] = :available
|
76
|
+
end
|
77
|
+
|
78
|
+
true
|
79
|
+
end
|
80
|
+
|
81
|
+
def available_count
|
82
|
+
available.size
|
83
|
+
end
|
84
|
+
|
85
|
+
def inactive_count
|
86
|
+
inactive.size
|
87
|
+
end
|
88
|
+
|
89
|
+
def in_use_count
|
90
|
+
in_use.size
|
91
|
+
end
|
92
|
+
|
93
|
+
private
|
94
|
+
|
95
|
+
def start_polling
|
96
|
+
@thread ||= Thread.new do
|
97
|
+
sleep @reactivate_timeout
|
98
|
+
reactivate_all
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
def available
|
103
|
+
find_by_state :available
|
104
|
+
end
|
105
|
+
|
106
|
+
def in_use
|
107
|
+
find_by_state :in_use
|
108
|
+
end
|
109
|
+
|
110
|
+
def inactive
|
111
|
+
find_by_state :inactive
|
112
|
+
end
|
113
|
+
|
114
|
+
def find_by_state(state)
|
115
|
+
@pool.select { |info| info[:state] == state }
|
116
|
+
end
|
117
|
+
|
118
|
+
def connection_info_from(builder)
|
119
|
+
{ :builder => builder, :state => :available, :connection => builder.call }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
@@ -0,0 +1,173 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class TestConnection
|
4
|
+
attr_accessor :name
|
5
|
+
def initialize(name)
|
6
|
+
@name = name
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class TestException < StandardError ; end
|
11
|
+
|
12
|
+
module Stillwater
|
13
|
+
describe ConnectionPool do
|
14
|
+
subject { ConnectionPool.new }
|
15
|
+
|
16
|
+
describe "#add" do
|
17
|
+
it "should add an available connection" do
|
18
|
+
subject.add { TestConnection.new("one") }
|
19
|
+
|
20
|
+
subject.available_count.should == 1
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "with a connection" do
|
25
|
+
before(:each) do
|
26
|
+
subject.add { TestConnection.new("one") }
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#checkout" do
|
30
|
+
it "should return a connection" do
|
31
|
+
conn = subject.checkout
|
32
|
+
|
33
|
+
conn.should be_instance_of(TestConnection)
|
34
|
+
subject.available_count.should == 0
|
35
|
+
subject.in_use_count.should == 1
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not be able to check out inactive connections" do
|
39
|
+
conn = subject.checkout
|
40
|
+
subject.deactivate conn
|
41
|
+
|
42
|
+
lambda { subject.checkout }.should raise_error(ConnectionNotAvailable)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "#checkin" do
|
47
|
+
it "should return the connection to the pool" do
|
48
|
+
conn = subject.checkout
|
49
|
+
subject.available_count.should == 0
|
50
|
+
subject.in_use_count.should == 1
|
51
|
+
|
52
|
+
subject.checkin(conn)
|
53
|
+
subject.available_count.should == 1
|
54
|
+
subject.in_use_count.should == 0
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "#with_connection" do
|
59
|
+
it "should run some code with a connection" do
|
60
|
+
result = subject.with_connection do |connection|
|
61
|
+
subject.available_count.should == 0
|
62
|
+
connection.name.should == "one"
|
63
|
+
|
64
|
+
"bob"
|
65
|
+
end
|
66
|
+
|
67
|
+
result.should == "bob"
|
68
|
+
subject.available_count.should == 1
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#retry_connection_from" do
|
73
|
+
before(:each) do
|
74
|
+
subject.add { TestConnection.new("two") }
|
75
|
+
end
|
76
|
+
|
77
|
+
it "should retry a connection when receiving specific exception type" do
|
78
|
+
client_obj = mock("ClientObject")
|
79
|
+
client_obj.stubs(:do_something).raises(TestException).then.returns("client result")
|
80
|
+
|
81
|
+
result = subject.retry_connection_from(TestException) do |conn|
|
82
|
+
client_obj.do_something
|
83
|
+
end
|
84
|
+
|
85
|
+
subject.available_count.should == 1
|
86
|
+
subject.inactive_count.should == 1
|
87
|
+
result.should == "client result"
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should respect the retry limit" do
|
91
|
+
subject.retry_count = 1
|
92
|
+
|
93
|
+
lambda {
|
94
|
+
subject.retry_connection_from(TestException) do |conn|
|
95
|
+
raise TestException
|
96
|
+
end
|
97
|
+
}.should raise_error(TestException)
|
98
|
+
|
99
|
+
subject.available_count.should == 1
|
100
|
+
subject.inactive_count.should == 1
|
101
|
+
end
|
102
|
+
|
103
|
+
it "should fail if all connections get deactivated" do
|
104
|
+
lambda {
|
105
|
+
subject.retry_connection_from(TestException) do |conn|
|
106
|
+
raise TestException
|
107
|
+
end
|
108
|
+
}.should raise_error(ConnectionNotAvailable)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "#deactivate" do
|
113
|
+
before(:each) do
|
114
|
+
subject.add { TestConnection.new("two") }
|
115
|
+
subject.add { TestConnection.new("three") }
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should remove a deactivated connection from the pool" do
|
119
|
+
subject.available_count.should == 3
|
120
|
+
subject.inactive_count.should == 0
|
121
|
+
|
122
|
+
subject.with_connection do |connection|
|
123
|
+
subject.deactivate connection
|
124
|
+
end
|
125
|
+
|
126
|
+
subject.available_count.should == 2
|
127
|
+
subject.inactive_count.should == 1
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "#reactivate" do
|
132
|
+
before(:each) do
|
133
|
+
subject.add { TestConnection.new("two") }
|
134
|
+
subject.add { TestConnection.new("three") }
|
135
|
+
end
|
136
|
+
|
137
|
+
it "should be able to reactivate inactive connections" do
|
138
|
+
subject.available_count.should == 3
|
139
|
+
subject.inactive_count.should == 0
|
140
|
+
|
141
|
+
conn1 = subject.checkout
|
142
|
+
conn2 = subject.checkout
|
143
|
+
|
144
|
+
subject.deactivate conn1
|
145
|
+
subject.deactivate conn2
|
146
|
+
|
147
|
+
subject.available_count.should == 1
|
148
|
+
subject.inactive_count.should == 2
|
149
|
+
|
150
|
+
subject.reactivate_all
|
151
|
+
|
152
|
+
subject.available_count.should == 3
|
153
|
+
subject.inactive_count.should == 0
|
154
|
+
end
|
155
|
+
|
156
|
+
it "should auto reactivate" do
|
157
|
+
subject.reactivate_timeout = 1
|
158
|
+
|
159
|
+
conn1 = subject.checkout
|
160
|
+
conn2 = subject.checkout
|
161
|
+
|
162
|
+
subject.deactivate conn1
|
163
|
+
subject.deactivate conn2
|
164
|
+
|
165
|
+
sleep 2
|
166
|
+
|
167
|
+
subject.available_count.should == 3
|
168
|
+
subject.inactive_count.should == 0
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'stillwater'
|
2
|
+
|
3
|
+
RSpec.configure do |config|
|
4
|
+
# Use color in STDOUT
|
5
|
+
config.color_enabled = true
|
6
|
+
|
7
|
+
# Use color not only in STDOUT but also in pagers and files
|
8
|
+
config.tty = true
|
9
|
+
|
10
|
+
# Use the specified formatter
|
11
|
+
config.formatter = :documentation # :progress, :html, :textmate
|
12
|
+
|
13
|
+
config.mock_framework = :mocha
|
14
|
+
end
|
data/stillwater.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/stillwater/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Trae Robrock", "Julio Santos"]
|
6
|
+
gem.email = ["trobrock@gmail.com"]
|
7
|
+
gem.description = %q{A simple connection pool, that allows connections to different servers (or anything else)}
|
8
|
+
gem.summary = %q{A simple connection pool, that allows connections to different servers (or anything else)}
|
9
|
+
gem.homepage = "https://github.com/trobrock/stillwater"
|
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 = "stillwater"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Stillwater::VERSION
|
17
|
+
|
18
|
+
gem.add_dependency %q<activesupport>, '~> 2.3.0'
|
19
|
+
|
20
|
+
gem.add_development_dependency %q<rake>
|
21
|
+
gem.add_development_dependency %q<rspec>
|
22
|
+
gem.add_development_dependency %q<mocha>
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,137 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: stillwater
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Trae Robrock
|
14
|
+
- Julio Santos
|
15
|
+
autorequire:
|
16
|
+
bindir: bin
|
17
|
+
cert_chain: []
|
18
|
+
|
19
|
+
date: 2012-07-05 00:00:00 Z
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 2
|
30
|
+
- 3
|
31
|
+
- 0
|
32
|
+
version: 2.3.0
|
33
|
+
name: activesupport
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
requirement: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
hash: 3
|
44
|
+
segments:
|
45
|
+
- 0
|
46
|
+
version: "0"
|
47
|
+
name: rake
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
requirement: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
hash: 3
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
version: "0"
|
61
|
+
name: rspec
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
requirement: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
67
|
+
none: false
|
68
|
+
requirements:
|
69
|
+
- - ">="
|
70
|
+
- !ruby/object:Gem::Version
|
71
|
+
hash: 3
|
72
|
+
segments:
|
73
|
+
- 0
|
74
|
+
version: "0"
|
75
|
+
name: mocha
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
requirement: *id004
|
79
|
+
description: A simple connection pool, that allows connections to different servers (or anything else)
|
80
|
+
email:
|
81
|
+
- trobrock@gmail.com
|
82
|
+
executables: []
|
83
|
+
|
84
|
+
extensions: []
|
85
|
+
|
86
|
+
extra_rdoc_files: []
|
87
|
+
|
88
|
+
files:
|
89
|
+
- .gitignore
|
90
|
+
- .rvmrc
|
91
|
+
- .travis.yml
|
92
|
+
- Gemfile
|
93
|
+
- LICENSE
|
94
|
+
- README.md
|
95
|
+
- Rakefile
|
96
|
+
- lib/stillwater.rb
|
97
|
+
- lib/stillwater/connection_pool.rb
|
98
|
+
- lib/stillwater/version.rb
|
99
|
+
- spec/connection_pool_spec.rb
|
100
|
+
- spec/spec_helper.rb
|
101
|
+
- stillwater.gemspec
|
102
|
+
homepage: https://github.com/trobrock/stillwater
|
103
|
+
licenses: []
|
104
|
+
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
hash: 3
|
116
|
+
segments:
|
117
|
+
- 0
|
118
|
+
version: "0"
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
none: false
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
hash: 3
|
125
|
+
segments:
|
126
|
+
- 0
|
127
|
+
version: "0"
|
128
|
+
requirements: []
|
129
|
+
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 1.8.21
|
132
|
+
signing_key:
|
133
|
+
specification_version: 3
|
134
|
+
summary: A simple connection pool, that allows connections to different servers (or anything else)
|
135
|
+
test_files:
|
136
|
+
- spec/connection_pool_spec.rb
|
137
|
+
- spec/spec_helper.rb
|