stillwater 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ *.sw*
data/.rvmrc CHANGED
@@ -1 +1,52 @@
1
- rvm 1.8.7@balancer --create
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.8.7" > .rvmrc
9
+ environment_id="ruby-1.8.7-p370@stillwater"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.14.10 (latest)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ if [[ $- == *i* ]] # check for interactive shells
29
+ then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
30
+ else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
31
+ fi
32
+ else
33
+ # If the environment file has not yet been created, use the RVM CLI to select.
34
+ rvm --create use "$environment_id" || {
35
+ echo "Failed to create RVM environment '${environment_id}'."
36
+ return 1
37
+ }
38
+ fi
39
+
40
+ # If you use bundler, this might be useful to you:
41
+ if [[ -s Gemfile ]] && {
42
+ ! builtin command -v bundle >/dev/null ||
43
+ builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
44
+ }
45
+ then
46
+ printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
47
+ gem install bundler
48
+ fi
49
+ if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
50
+ then
51
+ bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
52
+ fi
@@ -8,6 +8,7 @@ module Stillwater
8
8
  @pool = []
9
9
  @reactivate_timeout = 5 * 60 # 5 minutes
10
10
  @retry_count = 3
11
+ @polling = false
11
12
  end
12
13
 
13
14
  def add(&builder)
@@ -15,11 +16,8 @@ module Stillwater
15
16
  end
16
17
 
17
18
  def reactivate_timeout=(seconds)
18
- running = !!@thread
19
- @thread.kill if running
20
- @thread = nil
21
19
  @reactivate_timeout = seconds
22
- start_polling if running
20
+ start_polling
23
21
  end
24
22
 
25
23
  def with_connection(&block)
@@ -32,18 +30,21 @@ module Stillwater
32
30
 
33
31
  def retry_connection_from(exception_class, &block)
34
32
  count = 0
35
- conn = checkout
36
- yield conn
37
- rescue exception_class
38
- deactivate conn
39
- count += 1
40
- raise if count >= @retry_count
41
- retry
42
- ensure
43
- checkin conn
33
+ begin
34
+ conn = checkout
35
+ yield conn
36
+ rescue exception_class
37
+ raise if count >= @retry_count
38
+ deactivate conn
39
+ count += 1
40
+ retry
41
+ ensure
42
+ checkin conn
43
+ end
44
44
  end
45
45
 
46
46
  def checkout
47
+ reactivate_all if poll_timeout_exceeded?
47
48
  connection_info = available.respond_to?(:sample) ? available.sample : available.choice
48
49
  raise ConnectionNotAvailable if connection_info.nil?
49
50
  connection_info[:state] = :in_use
@@ -90,13 +91,20 @@ module Stillwater
90
91
 
91
92
  private
92
93
 
93
- def start_polling
94
- @thread ||= Thread.new do
95
- sleep @reactivate_timeout
96
- reactivate_all
94
+ def poll_timeout_exceeded?
95
+ return false unless @polling
96
+ now = Time.now
97
+ if (now - @last_poll_time) >= @reactivate_timeout
98
+ @last_poll_time = now
99
+ true
97
100
  end
98
101
  end
99
102
 
103
+ def start_polling
104
+ @polling = true
105
+ @last_poll_time = Time.now
106
+ end
107
+
100
108
  def available
101
109
  find_by_state :available
102
110
  end
@@ -1,3 +1,3 @@
1
1
  module Stillwater
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -72,18 +72,22 @@ module Stillwater
72
72
  describe "#retry_connection_from" do
73
73
  before(:each) do
74
74
  subject.add { TestConnection.new("two") }
75
+ subject.add { TestConnection.new("three") }
75
76
  end
76
77
 
77
78
  it "should retry a connection when receiving specific exception type" do
78
79
  client_obj = mock("ClientObject")
79
- client_obj.stubs(:do_something).raises(TestException).then.returns("client result")
80
+ client_obj.stubs(:do_something).
81
+ raises(TestException).then.
82
+ raises(TestException).then.
83
+ returns("client result")
80
84
 
81
85
  result = subject.retry_connection_from(TestException) do |conn|
82
86
  client_obj.do_something
83
87
  end
84
88
 
85
89
  subject.available_count.should == 1
86
- subject.inactive_count.should == 1
90
+ subject.inactive_count.should == 2
87
91
  result.should == "client result"
88
92
  end
89
93
 
@@ -96,7 +100,7 @@ module Stillwater
96
100
  end
97
101
  }.should raise_error(TestException)
98
102
 
99
- subject.available_count.should == 1
103
+ subject.available_count.should == 2
100
104
  subject.inactive_count.should == 1
101
105
  end
102
106
 
@@ -164,6 +168,8 @@ module Stillwater
164
168
 
165
169
  sleep 2
166
170
 
171
+ conn = subject.checkout
172
+ subject.checkin conn
167
173
  subject.available_count.should == 3
168
174
  subject.inactive_count.should == 0
169
175
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stillwater
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Trae Robrock
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2012-07-09 00:00:00 Z
19
+ date: 2012-08-14 00:00:00 Z
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
22
22
  version_requirements: &id001 !ruby/object:Gem::Requirement
@@ -28,9 +28,9 @@ dependencies:
28
28
  segments:
29
29
  - 0
30
30
  version: "0"
31
- name: rake
32
- type: :development
33
31
  prerelease: false
32
+ type: :development
33
+ name: rake
34
34
  requirement: *id001
35
35
  - !ruby/object:Gem::Dependency
36
36
  version_requirements: &id002 !ruby/object:Gem::Requirement
@@ -42,9 +42,9 @@ dependencies:
42
42
  segments:
43
43
  - 0
44
44
  version: "0"
45
- name: rspec
46
- type: :development
47
45
  prerelease: false
46
+ type: :development
47
+ name: rspec
48
48
  requirement: *id002
49
49
  - !ruby/object:Gem::Dependency
50
50
  version_requirements: &id003 !ruby/object:Gem::Requirement
@@ -56,9 +56,9 @@ dependencies:
56
56
  segments:
57
57
  - 0
58
58
  version: "0"
59
- name: mocha
60
- type: :development
61
59
  prerelease: false
60
+ type: :development
61
+ name: mocha
62
62
  requirement: *id003
63
63
  description: A simple connection pool, that allows connections to different servers (or anything else)
64
64
  email:
@@ -112,7 +112,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
112
  requirements: []
113
113
 
114
114
  rubyforge_project:
115
- rubygems_version: 1.8.21
115
+ rubygems_version: 1.8.24
116
116
  signing_key:
117
117
  specification_version: 3
118
118
  summary: A simple connection pool, that allows connections to different servers (or anything else)