hot_tub 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hot_tub/clients/client.rb +0 -8
- data/lib/hot_tub/session.rb +53 -47
- data/lib/hot_tub/version.rb +1 -1
- data/spec/session_spec.rb +4 -4
- metadata +4 -4
@@ -13,14 +13,6 @@ module HotTub
|
|
13
13
|
@client.send(method,*args,&blk)
|
14
14
|
end
|
15
15
|
|
16
|
-
def temporary?
|
17
|
-
@temporary == true
|
18
|
-
end
|
19
|
-
|
20
|
-
def mark_temporary
|
21
|
-
@temporary = true
|
22
|
-
end
|
23
|
-
|
24
16
|
# Override this method to perform the necessary action for ensure a client
|
25
17
|
# is clean for use.
|
26
18
|
def clean
|
data/lib/hot_tub/session.rb
CHANGED
@@ -2,14 +2,14 @@ module HotTub
|
|
2
2
|
class Session
|
3
3
|
|
4
4
|
# OPTIONS
|
5
|
-
# * :size - number of connections for each pool
|
5
|
+
# * :size - number of clients/connections for each pool
|
6
6
|
# * :inactivity_timeout - number of seconds to wait before disconnecting, setting to 0 means the connection will not be closed
|
7
|
-
# * :pool_timeout - the amount of seconds to block waiting for an
|
7
|
+
# * :pool_timeout - the amount of seconds to block waiting for an available client,
|
8
8
|
# because this is blocking it should be an extremely short amount of
|
9
9
|
# time default to 0.5 seconds, if you need more consider enlarging your pool
|
10
10
|
# instead of raising this number
|
11
|
-
# :never_block - if set to true, a
|
12
|
-
#
|
11
|
+
# :never_block - if set to true, a client will always be returned,
|
12
|
+
# but the pool size will never grow past that :size option, extra clients are closed
|
13
13
|
def initialize(client,options={})
|
14
14
|
@options = {
|
15
15
|
:size => 5,
|
@@ -21,47 +21,22 @@ module HotTub
|
|
21
21
|
@client = client
|
22
22
|
@mutex = (@client.respond_to?(:mutex) ? @client.mutex : Mutex.new)
|
23
23
|
end
|
24
|
-
|
25
|
-
|
24
|
+
|
25
|
+
# The synchronized pool for all our clients.
|
26
|
+
#
|
27
|
+
def pool(client=nil)
|
26
28
|
@mutex.synchronize do
|
27
|
-
|
29
|
+
if client
|
30
|
+
return_client(client)
|
31
|
+
else
|
32
|
+
add_client if add_client?
|
33
|
+
end
|
28
34
|
@pool
|
29
35
|
end
|
30
36
|
end
|
31
|
-
|
32
|
-
# Fetches an avaible client from the pool.
|
33
|
-
# Hot tubs are not always clean... Make sure we have a good connection. The client
|
34
|
-
# should respond to sanitize_hot_tub_connection, which checks to make sure
|
35
|
-
# the connection is still viable, and resets if not
|
36
|
-
def fetch
|
37
|
-
client = nil
|
38
|
-
alarm = (Time.now + @options[:blocking_timeout])
|
39
|
-
# block until we get an available connection or Timeout::Error
|
40
|
-
while client.nil?
|
41
|
-
raise_alarm if alarm <= Time.now
|
42
|
-
client = pool.shift
|
43
|
-
if client.nil? && (@options[:never_block])
|
44
|
-
HotTub.logger.info "Adding never_block client for #{@client.class.name}, will not be returned to pool."
|
45
|
-
client = new_client
|
46
|
-
client.mark_temporary
|
47
|
-
end
|
48
|
-
end
|
49
|
-
client.clean
|
50
|
-
client
|
51
|
-
end
|
52
|
-
|
53
|
-
# return a client to the pool
|
54
|
-
def return_client(client)
|
55
|
-
if client.temporary?
|
56
|
-
client.close # Too hot in the hot tub...
|
57
|
-
else
|
58
|
-
@pool << client
|
59
|
-
end
|
60
|
-
@pool
|
61
|
-
end
|
62
37
|
|
63
|
-
# Run the block on the retrieved
|
64
|
-
# is used for
|
38
|
+
# Run the block on the retrieved client. Good for ensure the same client
|
39
|
+
# is used for multiple requests. For HTTP requests make sure you request has
|
65
40
|
# keep-alive properly set for your client
|
66
41
|
# EX:
|
67
42
|
# @pool = HotTub.new(HotTub::ExconClient.new("https://some_web_site.com"))
|
@@ -79,7 +54,7 @@ module HotTub
|
|
79
54
|
raise ArgumentError, 'Run requires a block.'
|
80
55
|
end
|
81
56
|
ensure
|
82
|
-
|
57
|
+
pool(client) if client
|
83
58
|
end
|
84
59
|
|
85
60
|
|
@@ -94,7 +69,7 @@ module HotTub
|
|
94
69
|
client = fetch
|
95
70
|
client.send(method,*args,&blk)
|
96
71
|
ensure
|
97
|
-
|
72
|
+
pool(client) if client
|
98
73
|
end
|
99
74
|
|
100
75
|
private
|
@@ -102,6 +77,10 @@ module HotTub
|
|
102
77
|
def add_client?
|
103
78
|
(@pool.empty? && (@pool_data[:current_size] < @options[:size]))
|
104
79
|
end
|
80
|
+
|
81
|
+
def new_client
|
82
|
+
@client.dup
|
83
|
+
end
|
105
84
|
|
106
85
|
def add_client
|
107
86
|
HotTub.logger.info "Adding HotTub client: #{@client.class.name} to pool"
|
@@ -109,11 +88,38 @@ module HotTub
|
|
109
88
|
@pool << new_client
|
110
89
|
@pool
|
111
90
|
end
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
91
|
+
|
92
|
+
# return a client to the pool
|
93
|
+
def return_client(client)
|
94
|
+
if @pool.length < @options[:size]
|
95
|
+
@pool << client
|
96
|
+
else
|
97
|
+
HotTub.logger.info "Closed extra client for #{@client.class.name}."
|
98
|
+
client.close # Too hot in the hot tub...
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
# Fetches an available client from the pool.
|
103
|
+
# Hot tubs are not always clean... Make sure we have a good client. The client
|
104
|
+
# should respond to clean, which checks to make sure the client is still
|
105
|
+
# viable, and reset if necessary.
|
106
|
+
def fetch
|
107
|
+
client = nil
|
108
|
+
alarm = (Time.now + @options[:blocking_timeout])
|
109
|
+
# block until we get an available client or raise Timeout::Error
|
110
|
+
while client.nil?
|
111
|
+
raise_alarm if alarm <= Time.now
|
112
|
+
client = pool.shift
|
113
|
+
if client.nil? && (@options[:never_block])
|
114
|
+
HotTub.logger.info "Adding never_block client for #{@client.class.name}."
|
115
|
+
client = new_client
|
116
|
+
client.mark_temporary
|
117
|
+
end
|
118
|
+
end
|
119
|
+
client.clean
|
120
|
+
client
|
121
|
+
end
|
122
|
+
|
117
123
|
def raise_alarm
|
118
124
|
message = "Could not fetch a free client in time. Consider increasing your pool size for #{@client.class.name}."
|
119
125
|
HotTub.logger.error message
|
data/lib/hot_tub/version.rb
CHANGED
data/spec/session_spec.rb
CHANGED
@@ -48,20 +48,20 @@ describe HotTub::Session do
|
|
48
48
|
end
|
49
49
|
end
|
50
50
|
|
51
|
-
describe '#
|
51
|
+
describe '#fetch' do
|
52
52
|
it "should raise Timeout::Error if an available is not found in time"do
|
53
53
|
@tub.stub(:pool).and_return([])
|
54
|
-
lambda { @tub.fetch}.should raise_error(Timeout::Error)
|
54
|
+
lambda { @tub.send(:fetch)}.should raise_error(Timeout::Error)
|
55
55
|
end
|
56
56
|
|
57
57
|
it "should not raise Timeout::Error if an available is not found in time"do
|
58
58
|
@tub.instance_variable_get(:@options)[:never_block] = true
|
59
59
|
@tub.stub(:pool).and_return([])
|
60
|
-
lambda { @tub.fetch}.should_not raise_error(Timeout::Error)
|
60
|
+
lambda { @tub.send(:fetch)}.should_not raise_error(Timeout::Error)
|
61
61
|
end
|
62
62
|
|
63
63
|
it "should return an instance of the driver" do
|
64
|
-
@tub.fetch.should be_instance_of(MocClient)
|
64
|
+
@tub.send(:fetch).should be_instance_of(MocClient)
|
65
65
|
end
|
66
66
|
end
|
67
67
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hot_tub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-09 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &70279127174460 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,7 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70279127174460
|
25
25
|
description: A very simple ruby pool gem
|
26
26
|
email:
|
27
27
|
- joshmckin@gmail.com
|