hot_tub 0.5.2 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rspec +4 -1
- data/HISTORY.md +8 -0
- data/README.md +3 -4
- data/benchmarks/block_passing.rb +29 -29
- data/benchmarks/hot_tub.rb +43 -38
- data/lib/hot_tub.rb +6 -2
- data/lib/hot_tub/known_clients.rb +16 -22
- data/lib/hot_tub/pool.rb +13 -18
- data/lib/hot_tub/reaper.rb +2 -2
- data/lib/hot_tub/sessions.rb +2 -1
- data/lib/hot_tub/version.rb +1 -1
- data/spec/hot_tub/pool_spec.rb +0 -1
- data/spec/hot_tub/sessions_spec.rb +15 -15
- metadata +20 -20
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: af5ab22d571fb050b860aa00c6272f9d3e1ca564
|
4
|
+
data.tar.gz: 7aab55538f57d844feca8b389285d705707de8d3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62623f8ff72d0f9fa386e37aab4a023401be235f91478ed9e0e78f4d329787d05bf72baaf8670e5bba6a9a6203f7f2c52e3659bfcb97fe8e002fa6bd4dfae580
|
7
|
+
data.tar.gz: 8ca0674fa6a19f4bb228f083d968ee2067503dc513e4658753af06427c43a8da2b6ba095c6f71808daa596b07298b8d01cd94fb080be1ed5bfb160b92861e7f6
|
data/.rspec
CHANGED
data/HISTORY.md
CHANGED
@@ -3,6 +3,14 @@ HotTub Changelog
|
|
3
3
|
|
4
4
|
Head
|
5
5
|
=======
|
6
|
+
- None yet
|
7
|
+
|
8
|
+
0.5.3
|
9
|
+
=======
|
10
|
+
- For Sessions, rename #add to #get_or_set for clarity, and alias with #add
|
11
|
+
- Fix Ruby warnings
|
12
|
+
- Prevent possible deadlock waiting for reaper to shutdown
|
13
|
+
- No need to reset reaper
|
6
14
|
|
7
15
|
0.5.2
|
8
16
|
=======
|
data/README.md
CHANGED
@@ -70,7 +70,7 @@ A global Sessions object is available from the HotTub module and has several hel
|
|
70
70
|
# Once load dies down our pool will be reaped back down to 12 connections
|
71
71
|
|
72
72
|
URL = "https://google.com"
|
73
|
-
pool = HotTub.
|
73
|
+
pool = HotTub.get_or_set(URL, { :size => 12 }) do
|
74
74
|
uri = URI.parse(URL)
|
75
75
|
http = Net::HTTP.new(uri.host, uri.port)
|
76
76
|
http.use_ssl = false
|
@@ -80,16 +80,15 @@ A global Sessions object is available from the HotTub module and has several hel
|
|
80
80
|
|
81
81
|
# A separate HotTub::Pool of Excon connections.
|
82
82
|
|
83
|
-
HotTub.
|
83
|
+
HotTub.get_or_set('yahoo', { :size => 5 }) { Excon.new("https://yahoo.com") }
|
84
84
|
|
85
85
|
# Lets add Redis too. HotTub.add returns the pool created for that key so we
|
86
86
|
# can store that in an constant for easy access.
|
87
87
|
# We don't want too many connections so we set our :max_size. Under load our pool
|
88
88
|
# can grow to 30 connections. Once load dies down our pool can be reaped back down to 5
|
89
89
|
|
90
|
-
REDIS = HotTub.
|
90
|
+
REDIS = HotTub.get_or_set("redis", :size => 5, :max_size => 30) { Redis.new }
|
91
91
|
|
92
|
-
|
93
92
|
# Now we can call any of our pools using the key we set any where in our code.
|
94
93
|
|
95
94
|
HotTub.run(url) do |clnt|
|
data/benchmarks/block_passing.rb
CHANGED
@@ -43,54 +43,54 @@ Benchmark.bmbm do |x|
|
|
43
43
|
end
|
44
44
|
x.report("block to yield") do
|
45
45
|
n.times do
|
46
|
-
BlockTest.
|
46
|
+
BlockTest.block_to_yield { "foo" }
|
47
47
|
end
|
48
48
|
end
|
49
49
|
x.report("block to call") do
|
50
50
|
n.times do
|
51
|
-
BlockTest.
|
51
|
+
BlockTest.block_to_call { "foo" }
|
52
52
|
end
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
56
56
|
# ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
|
57
57
|
# Rehearsal --------------------------------------------------
|
58
|
-
# block yield 0.
|
59
|
-
# block call 0.
|
60
|
-
# block to yield 0.
|
61
|
-
# block to call 0.
|
62
|
-
# ----------------------------------------- total:
|
58
|
+
# block yield 0.170000 0.000000 0.170000 ( 0.169354)
|
59
|
+
# block call 0.670000 0.010000 0.680000 ( 0.670445)
|
60
|
+
# block to yield 0.650000 0.000000 0.650000 ( 0.645591)
|
61
|
+
# block to call 0.750000 0.000000 0.750000 ( 0.753102)
|
62
|
+
# ----------------------------------------- total: 2.250000sec
|
63
63
|
|
64
64
|
# user system total real
|
65
|
-
# block yield 0.
|
66
|
-
# block call 0.
|
67
|
-
# block to yield 0.
|
68
|
-
# block to call 0.
|
65
|
+
# block yield 0.170000 0.000000 0.170000 ( 0.166196)
|
66
|
+
# block call 0.670000 0.000000 0.670000 ( 0.663034)
|
67
|
+
# block to yield 0.640000 0.000000 0.640000 ( 0.644746)
|
68
|
+
# block to call 0.740000 0.000000 0.740000 ( 0.747084)
|
69
69
|
|
70
70
|
# rubinius 2.5.8 (2.1.0 bef51ae3 2015-07-14 3.5.1 JI) [x86_64-darwin14.4.0]
|
71
71
|
# Rehearsal --------------------------------------------------
|
72
|
-
# block yield 0.
|
73
|
-
# block call 0.
|
74
|
-
# block to yield 0.
|
75
|
-
# block to call 0.
|
76
|
-
# ----------------------------------------- total: 1.
|
72
|
+
# block yield 0.153971 0.007521 0.161492 ( 0.123423)
|
73
|
+
# block call 0.482452 0.001687 0.484139 ( 0.404527)
|
74
|
+
# block to yield 0.421112 0.001725 0.422837 ( 0.317573)
|
75
|
+
# block to call 0.515777 0.001130 0.516907 ( 0.454804)
|
76
|
+
# ----------------------------------------- total: 1.585375sec
|
77
77
|
|
78
78
|
# user system total real
|
79
|
-
# block yield 0.
|
80
|
-
# block call 0.
|
81
|
-
# block to yield 0.
|
82
|
-
# block to call 0.
|
79
|
+
# block yield 0.054358 0.000111 0.054469 ( 0.054250)
|
80
|
+
# block call 0.368271 0.000868 0.369139 ( 0.368516)
|
81
|
+
# block to yield 0.281076 0.000553 0.281629 ( 0.281446)
|
82
|
+
# block to call 0.428372 0.000901 0.429273 ( 0.428916)
|
83
83
|
|
84
84
|
# jruby 9.0.3.0 (2.2.2) 2015-10-21 633c9aa Java HotSpot(TM) 64-Bit Server VM 23.5-b02 on 1.7.0_09-b05 +jit [darwin-x86_64]
|
85
85
|
# Rehearsal --------------------------------------------------
|
86
|
-
# block yield 0.
|
87
|
-
# block call 0.
|
88
|
-
# block to yield 0.
|
89
|
-
# block to call 0.
|
90
|
-
# ----------------------------------------- total: 1.
|
86
|
+
# block yield 0.400000 0.010000 0.410000 ( 0.265343)
|
87
|
+
# block call 0.370000 0.000000 0.370000 ( 0.300598)
|
88
|
+
# block to yield 0.280000 0.010000 0.290000 ( 0.238879)
|
89
|
+
# block to call 0.400000 0.000000 0.400000 ( 0.288711)
|
90
|
+
# ----------------------------------------- total: 1.470000sec
|
91
91
|
|
92
92
|
# user system total real
|
93
|
-
# block yield 0.
|
94
|
-
# block call 0.
|
95
|
-
# block to yield 0.
|
96
|
-
# block to call 0.
|
93
|
+
# block yield 0.190000 0.000000 0.190000 ( 0.185705)
|
94
|
+
# block call 0.230000 0.000000 0.230000 ( 0.227818)
|
95
|
+
# block to yield 0.200000 0.000000 0.200000 ( 0.206645)
|
96
|
+
# block to call 0.250000 0.000000 0.250000 ( 0.244579)
|
data/benchmarks/hot_tub.rb
CHANGED
@@ -9,6 +9,11 @@ class MocClient
|
|
9
9
|
def get
|
10
10
|
sleep(0.01)
|
11
11
|
end
|
12
|
+
|
13
|
+
def clean
|
14
|
+
@clean = true
|
15
|
+
end
|
16
|
+
|
12
17
|
end
|
13
18
|
|
14
19
|
puts `ruby -v`
|
@@ -16,7 +21,7 @@ puts `ruby -v`
|
|
16
21
|
Benchmark.bmbm do |b|
|
17
22
|
|
18
23
|
b.report("single thread") do
|
19
|
-
hot_tub = HotTub.new(:size => 1, :max_size => 1) { MocClient.new }
|
24
|
+
hot_tub = HotTub.new(:size => 1, :max_size => 1, :clean => lambda {|clnt| clnt.clean}) { MocClient.new }
|
20
25
|
1000.times.each do
|
21
26
|
hot_tub.run do |conn|
|
22
27
|
conn.get
|
@@ -25,7 +30,7 @@ Benchmark.bmbm do |b|
|
|
25
30
|
end
|
26
31
|
|
27
32
|
b.report("threaded size 5") do
|
28
|
-
hot_tub = HotTub.new(:size => 5, :max_size => 5) { MocClient.new }
|
33
|
+
hot_tub = HotTub.new(:size => 5, :max_size => 5, :clean => lambda {|clnt| clnt.clean}) { MocClient.new }
|
29
34
|
threads = []
|
30
35
|
50.times.each do
|
31
36
|
threads << Thread.new do
|
@@ -42,7 +47,7 @@ Benchmark.bmbm do |b|
|
|
42
47
|
end
|
43
48
|
|
44
49
|
b.report("threaded size 5, max 10") do
|
45
|
-
hot_tub = HotTub.new(:size => 5, :max_size => 10) { MocClient.new }
|
50
|
+
hot_tub = HotTub.new(:size => 5, :max_size => 10, :clean => lambda {|clnt| clnt.clean}) { MocClient.new }
|
46
51
|
threads = []
|
47
52
|
50.times.each do
|
48
53
|
threads << Thread.new do
|
@@ -59,7 +64,7 @@ Benchmark.bmbm do |b|
|
|
59
64
|
end
|
60
65
|
|
61
66
|
b.report("threaded, size 5, no max") do
|
62
|
-
hot_tub = HotTub.new(:size => 5) { MocClient.new }
|
67
|
+
hot_tub = HotTub.new(:size => 5, :clean => lambda {|clnt| clnt.clean}) { MocClient.new }
|
63
68
|
threads = []
|
64
69
|
50.times.each do
|
65
70
|
threads << Thread.new do
|
@@ -79,7 +84,7 @@ Benchmark.bmbm do |b|
|
|
79
84
|
b.report("threaded, HotTub.run ") do
|
80
85
|
urls = ['http://foo.com','http://bar.com','http://zap.com']
|
81
86
|
urls.each do |url|
|
82
|
-
HotTub.add(url) { MocClient.new }
|
87
|
+
HotTub.add(url, {:clean => lambda {|clnt| clnt.clean}}) { MocClient.new }
|
83
88
|
end
|
84
89
|
threads = []
|
85
90
|
50.times.each do
|
@@ -102,50 +107,50 @@ end
|
|
102
107
|
|
103
108
|
# ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
|
104
109
|
# Rehearsal ------------------------------------------------------------
|
105
|
-
# single thread 0.
|
106
|
-
# threaded size 5 0.
|
107
|
-
# threaded size 5, max 10 0.
|
108
|
-
# threaded, size 5, no max 0.030000 0.
|
109
|
-
# threaded, HotTub.run 0.
|
110
|
-
# --------------------------------------------------- total: 0.
|
110
|
+
# single thread 0.090000 0.050000 0.140000 ( 11.056805)
|
111
|
+
# threaded size 5 0.110000 0.130000 0.240000 ( 2.323854)
|
112
|
+
# threaded size 5, max 10 0.080000 0.100000 0.180000 ( 1.162047)
|
113
|
+
# threaded, size 5, no max 0.030000 0.040000 0.070000 ( 0.229230)
|
114
|
+
# threaded, HotTub.run 0.130000 0.120000 0.250000 ( 0.673286)
|
115
|
+
# --------------------------------------------------- total: 0.880000sec
|
111
116
|
|
112
117
|
# user system total real
|
113
|
-
# single thread 0.080000 0.
|
114
|
-
# threaded size 5 0.
|
115
|
-
# threaded size 5, max 10 0.
|
116
|
-
# threaded, size 5, no max 0.
|
117
|
-
# threaded, HotTub.run 0.
|
118
|
+
# single thread 0.080000 0.040000 0.120000 ( 10.908496)
|
119
|
+
# threaded size 5 0.110000 0.130000 0.240000 ( 2.314875)
|
120
|
+
# threaded size 5, max 10 0.080000 0.100000 0.180000 ( 1.202064)
|
121
|
+
# threaded, size 5, no max 0.040000 0.040000 0.080000 ( 0.224133)
|
122
|
+
# threaded, HotTub.run 0.120000 0.120000 0.240000 ( 0.670526)
|
118
123
|
|
119
124
|
|
120
125
|
# rubinius 2.5.8 (2.1.0 bef51ae3 2015-07-14 3.5.1 JI) [x86_64-darwin14.4.0]
|
121
126
|
# Rehearsal ------------------------------------------------------------
|
122
|
-
# single thread 0.
|
123
|
-
# threaded size 5 0.
|
124
|
-
# threaded size 5, max 10 0.
|
125
|
-
# threaded, size 5, no max 0.
|
126
|
-
# threaded, HotTub.run 0.
|
127
|
-
# --------------------------------------------------- total: 1.
|
127
|
+
# single thread 0.427639 0.053404 0.481043 ( 11.049242)
|
128
|
+
# threaded size 5 0.214155 0.201618 0.415773 ( 2.255010)
|
129
|
+
# threaded size 5, max 10 0.170088 0.158815 0.328903 ( 1.159579)
|
130
|
+
# threaded, size 5, no max 0.132440 0.046240 0.178680 ( 0.240322)
|
131
|
+
# threaded, HotTub.run 0.169417 0.123085 0.292502 ( 0.676492)
|
132
|
+
# --------------------------------------------------- total: 1.696901sec
|
128
133
|
|
129
134
|
# user system total real
|
130
|
-
# single thread 0.
|
131
|
-
# threaded size 5 0.
|
132
|
-
# threaded size 5, max 10 0.
|
133
|
-
# threaded, size 5, no max 0.
|
134
|
-
# threaded, HotTub.run 0.
|
135
|
+
# single thread 0.155789 0.054654 0.210443 ( 11.052989)
|
136
|
+
# threaded size 5 0.253045 0.209961 0.463006 ( 2.276732)
|
137
|
+
# threaded size 5, max 10 0.149159 0.138857 0.288016 ( 1.171204)
|
138
|
+
# threaded, size 5, no max 0.216853 0.033597 0.250450 ( 0.224729)
|
139
|
+
# threaded, HotTub.run 0.626503 0.080910 0.707413 ( 0.664893)
|
135
140
|
|
136
141
|
|
137
142
|
# jruby 9.0.3.0 (2.2.2) 2015-10-21 633c9aa Java HotSpot(TM) 64-Bit Server VM 23.5-b02 on 1.7.0_09-b05 +jit [darwin-x86_64]
|
138
143
|
# Rehearsal ------------------------------------------------------------
|
139
|
-
# single thread 0.
|
140
|
-
# threaded size 5 0.
|
141
|
-
# threaded size 5, max 10 0.
|
142
|
-
# threaded, size 5, no max 0.
|
143
|
-
# threaded, HotTub.run
|
144
|
-
# --------------------------------------------------- total:
|
144
|
+
# single thread 0.890000 0.070000 0.960000 ( 11.343993)
|
145
|
+
# threaded size 5 0.850000 0.130000 0.980000 ( 2.324202)
|
146
|
+
# threaded size 5, max 10 0.710000 0.110000 0.820000 ( 1.178593)
|
147
|
+
# threaded, size 5, no max 0.320000 0.050000 0.370000 ( 0.233405)
|
148
|
+
# threaded, HotTub.run 1.190000 0.100000 1.290000 ( 0.683554)
|
149
|
+
# --------------------------------------------------- total: 4.420000sec
|
145
150
|
|
146
151
|
# user system total real
|
147
|
-
# single thread 0.
|
148
|
-
# threaded size 5 0.
|
149
|
-
# threaded size 5, max 10 0.
|
150
|
-
# threaded, size 5, no max 0.
|
151
|
-
# threaded, HotTub.run 0.
|
152
|
+
# single thread 0.350000 0.060000 0.410000 ( 11.156489)
|
153
|
+
# threaded size 5 0.540000 0.130000 0.670000 ( 2.251505)
|
154
|
+
# threaded size 5, max 10 0.300000 0.100000 0.400000 ( 1.153055)
|
155
|
+
# threaded, size 5, no max 0.270000 0.050000 0.320000 ( 0.230043)
|
156
|
+
# threaded, HotTub.run 0.360000 0.110000 0.470000 ( 0.674920)
|
data/lib/hot_tub.rb
CHANGED
@@ -59,9 +59,13 @@ module HotTub
|
|
59
59
|
GLOBAL_SESSIONS.shutdown!
|
60
60
|
end
|
61
61
|
|
62
|
-
#
|
62
|
+
# Gets or sets a new Pool in the global sessions
|
63
|
+
def self.get_or_set(url,opts={}, &client_block)
|
64
|
+
GLOBAL_SESSIONS.get_or_set(url, opts, &client_block)
|
65
|
+
end
|
66
|
+
|
63
67
|
def self.add(url,opts={}, &client_block)
|
64
|
-
|
68
|
+
self.get_or_set(url,opts, &client_block)
|
65
69
|
end
|
66
70
|
|
67
71
|
def self.run(url ,&run_block)
|
@@ -2,7 +2,7 @@ module HotTub
|
|
2
2
|
module KnownClients
|
3
3
|
KNOWN_CLIENTS = {
|
4
4
|
"Excon::Connection" => {
|
5
|
-
:close =>
|
5
|
+
:close => :reset
|
6
6
|
},
|
7
7
|
"Net::HTTP" => {
|
8
8
|
:close => lambda { |clnt|
|
@@ -20,11 +20,7 @@ module HotTub
|
|
20
20
|
def clean_client(clnt)
|
21
21
|
if @clean_client
|
22
22
|
begin
|
23
|
-
|
24
|
-
preform_client_block(clnt,&@clean_client)
|
25
|
-
else
|
26
|
-
preform_client_method(clnt,@clean_client)
|
27
|
-
end
|
23
|
+
perform_action(clnt,@clean_client)
|
28
24
|
rescue => e
|
29
25
|
HotTub.logger.error "[HotTub] There was an error cleaning one of your #{self.class.name} clients: #{e}" if HotTub.logger
|
30
26
|
end
|
@@ -35,14 +31,10 @@ module HotTub
|
|
35
31
|
# Attempts to close the provided client, checking the options first for a close block
|
36
32
|
# then checking the known clients
|
37
33
|
def close_client(clnt)
|
38
|
-
@
|
39
|
-
if @
|
34
|
+
@close_client = (known_client_action(clnt,:close) || false) if @close_client.nil?
|
35
|
+
if @close_client
|
40
36
|
begin
|
41
|
-
|
42
|
-
preform_client_block(clnt,&@close_action)
|
43
|
-
else
|
44
|
-
preform_client_method(clnt,@close_action)
|
45
|
-
end
|
37
|
+
perform_action(clnt,@close_client)
|
46
38
|
rescue => e
|
47
39
|
HotTub.logger.error "[HotTub] There was an error closing one of your #{self.class.name} clients: #{e}" if HotTub.logger
|
48
40
|
end
|
@@ -55,11 +47,7 @@ module HotTub
|
|
55
47
|
rc = false
|
56
48
|
if @reap_client
|
57
49
|
begin
|
58
|
-
|
59
|
-
rc = preform_client_block(clnt,&@reap_client)
|
60
|
-
else
|
61
|
-
rc = preform_client_method(clnt,@reap_client)
|
62
|
-
end
|
50
|
+
rc = perform_action(clnt,@reap_client)
|
63
51
|
rescue => e
|
64
52
|
HotTub.logger.error "[HotTub] There was an error reaping one of your #{self.class.name} clients: #{e}" if HotTub.logger
|
65
53
|
end
|
@@ -73,12 +61,18 @@ module HotTub
|
|
73
61
|
(KNOWN_CLIENTS[clnt.class.name] && KNOWN_CLIENTS[clnt.class.name][key])
|
74
62
|
end
|
75
63
|
|
76
|
-
def
|
77
|
-
|
64
|
+
def perform_action(clnt,action)
|
65
|
+
if action.is_a?(Proc)
|
66
|
+
yield_action(clnt,&action)
|
67
|
+
else
|
68
|
+
clnt.__send__(action)
|
69
|
+
end
|
78
70
|
end
|
79
71
|
|
80
|
-
|
81
|
-
|
72
|
+
# This is ever so slightly faster in MRI,
|
73
|
+
# more so on Rubinius and Jruby
|
74
|
+
def yield_action(clnt)
|
75
|
+
yield clnt
|
82
76
|
end
|
83
77
|
end
|
84
78
|
end
|
data/lib/hot_tub/pool.rb
CHANGED
@@ -4,7 +4,6 @@ module HotTub
|
|
4
4
|
include HotTub::Reaper::Mixin
|
5
5
|
|
6
6
|
attr_accessor :name
|
7
|
-
attr_reader :current_size, :last_activity
|
8
7
|
|
9
8
|
# Thread-safe lazy connection pool
|
10
9
|
#
|
@@ -92,14 +91,14 @@ module HotTub
|
|
92
91
|
def initialize(opts={},&client_block)
|
93
92
|
raise ArgumentError, 'a block that initializes a new client is required' unless block_given?
|
94
93
|
@name = (opts[:name] || self.class.name)
|
95
|
-
@size = (opts[:size] || 5)
|
96
|
-
@wait_timeout = (opts[:wait_timeout] || 10)
|
97
|
-
@reap_timeout = (opts[:reap_timeout] || 600)
|
98
|
-
@max_size = (opts[:max_size] || 0)
|
94
|
+
@size = (opts[:size] || 5) # in seconds
|
95
|
+
@wait_timeout = (opts[:wait_timeout] || 10) # in seconds
|
96
|
+
@reap_timeout = (opts[:reap_timeout] || 600) # the interval to reap connections in seconds
|
97
|
+
@max_size = (opts[:max_size] || 0) # maximum size of pool when non-blocking, 0 means no limit
|
99
98
|
|
100
|
-
@close_client = opts[:close]
|
101
|
-
@clean_client = opts[:clean]
|
102
|
-
@reap_client = opts[:reap?]
|
99
|
+
@close_client = opts[:close] # => lambda {|clnt| clnt.close} or :close
|
100
|
+
@clean_client = opts[:clean] # => lambda {|clnt| clnt.clean} or :clean
|
101
|
+
@reap_client = opts[:reap?] # => lambda {|clnt| clnt.reap?} or :reap? # should return boolean
|
103
102
|
@client_block = client_block
|
104
103
|
|
105
104
|
@_pool = [] # stores available clients
|
@@ -112,7 +111,7 @@ module HotTub
|
|
112
111
|
|
113
112
|
@shutdown = false
|
114
113
|
@blocking_reap = (opts[:reaper] == false && !opts[:sessions])
|
115
|
-
@reaper =
|
114
|
+
@reaper = ((opts[:sessions] || (opts[:reaper] == false)) ? false : spawn_reaper)
|
116
115
|
|
117
116
|
@never_block = (@max_size == 0)
|
118
117
|
|
@@ -159,7 +158,7 @@ module HotTub
|
|
159
158
|
end
|
160
159
|
alias :close! :drain!
|
161
160
|
|
162
|
-
# Reset the pool
|
161
|
+
# Reset the pool.
|
163
162
|
# or if shutdown allow threads to quickly finish their work
|
164
163
|
# Clients from the previous pool will not return to pool.
|
165
164
|
def reset!
|
@@ -169,10 +168,6 @@ module HotTub
|
|
169
168
|
while clnt = @_pool.pop
|
170
169
|
close_client(clnt)
|
171
170
|
end
|
172
|
-
if @reaper
|
173
|
-
kill_reaper
|
174
|
-
@reaper = spawn_reaper
|
175
|
-
end
|
176
171
|
ensure
|
177
172
|
@_out.clear
|
178
173
|
@_pool.clear
|
@@ -186,12 +181,12 @@ module HotTub
|
|
186
181
|
def shutdown!
|
187
182
|
HotTub.logger.info "[HotTub] Shutting down pool #{@name}!" if HotTub.logger
|
188
183
|
@shutdown = true
|
184
|
+
kill_reaper if @reaper
|
189
185
|
@mutex.synchronize do
|
190
186
|
begin
|
191
187
|
while clnt = @_pool.pop
|
192
188
|
close_client(clnt)
|
193
189
|
end
|
194
|
-
kill_reaper if @reaper
|
195
190
|
ensure
|
196
191
|
@_out.clear
|
197
192
|
@_pool.clear
|
@@ -254,7 +249,7 @@ module HotTub
|
|
254
249
|
begin
|
255
250
|
if !@shutdown && @_out.delete(clnt)
|
256
251
|
@_pool << clnt
|
257
|
-
else
|
252
|
+
else
|
258
253
|
close_client(clnt)
|
259
254
|
HotTub.logger.info "[HotTub] An orphaned client attempted to return to #{@name}." if HotTub.log_trace?
|
260
255
|
end
|
@@ -286,7 +281,7 @@ module HotTub
|
|
286
281
|
end
|
287
282
|
break if clnt
|
288
283
|
end
|
289
|
-
clean_client(clnt) if dirty && clnt
|
284
|
+
clean_client(clnt) if dirty && clnt
|
290
285
|
clnt
|
291
286
|
end
|
292
287
|
|
@@ -306,7 +301,7 @@ module HotTub
|
|
306
301
|
if (@never_block || (_total_current_size < @max_size))
|
307
302
|
nc = yield
|
308
303
|
HotTub.logger.info "[HotTub] Adding client: #{nc.class.name} to #{@name}." if HotTub.log_trace?
|
309
|
-
nc
|
304
|
+
nc
|
310
305
|
end
|
311
306
|
end
|
312
307
|
|
data/lib/hot_tub/reaper.rb
CHANGED
@@ -11,8 +11,8 @@ module HotTub
|
|
11
11
|
th = Thread.new {
|
12
12
|
loop do
|
13
13
|
begin
|
14
|
-
obj.reap!
|
15
14
|
break if obj.shutdown
|
15
|
+
obj.reap!
|
16
16
|
sleep(obj.reap_timeout || 600)
|
17
17
|
rescue Exception => e
|
18
18
|
HotTub.logger.error "[HotTub] Reaper for #{obj.class.name} terminated with exception: #{e.message}" if HotTub.logger
|
@@ -50,7 +50,7 @@ module HotTub
|
|
50
50
|
if @reaper
|
51
51
|
@reaper.kill
|
52
52
|
@reaper.join
|
53
|
-
@reaper = nil
|
53
|
+
@reaper = nil if @shutdown
|
54
54
|
end
|
55
55
|
end
|
56
56
|
|
data/lib/hot_tub/sessions.rb
CHANGED
@@ -57,7 +57,7 @@ module HotTub
|
|
57
57
|
|
58
58
|
# Adds a new HotTub::Pool for the given key unless
|
59
59
|
# one already exists.
|
60
|
-
def
|
60
|
+
def get_or_set(key, pool_options={}, &client_block)
|
61
61
|
raise ArgumentError, 'a block that initializes a new client is required.' unless block_given?
|
62
62
|
pool = nil
|
63
63
|
return pool if pool = @_sessions[key]
|
@@ -69,6 +69,7 @@ module HotTub
|
|
69
69
|
end
|
70
70
|
pool
|
71
71
|
end
|
72
|
+
alias :add :get_or_set
|
72
73
|
|
73
74
|
# Deletes and shutdowns the pool if its found.
|
74
75
|
def delete(key)
|
data/lib/hot_tub/version.rb
CHANGED
data/spec/hot_tub/pool_spec.rb
CHANGED
@@ -15,7 +15,7 @@ describe HotTub::Sessions do
|
|
15
15
|
context 'HotTub::Pool as client' do
|
16
16
|
it "should add a new client for the key" do
|
17
17
|
sessions = HotTub::Sessions.new
|
18
|
-
sessions.
|
18
|
+
sessions.get_or_set(key) { MocClient.new }
|
19
19
|
sns = sessions.instance_variable_get(:@_sessions)
|
20
20
|
expect(sns.size).to eql(1)
|
21
21
|
sns.each_value {|v| expect(v).to be_a( HotTub::Pool)}
|
@@ -31,16 +31,16 @@ describe HotTub::Sessions do
|
|
31
31
|
|
32
32
|
it "should start reaper after add" do
|
33
33
|
expect(sessions.reaper).to be_nil
|
34
|
-
sessions.
|
34
|
+
sessions.get_or_set("https://www.somewebsite.com") { MocClient.new }
|
35
35
|
expect(sessions.reaper).to be_a(Thread)
|
36
36
|
end
|
37
37
|
|
38
38
|
it "should disable pool based reaper" do
|
39
|
-
sessions.
|
40
|
-
sessions.
|
41
|
-
sessions.
|
39
|
+
sessions.get_or_set("https://www.somewebsite.com") { MocClient.new }
|
40
|
+
sessions.get_or_set("https://www.someOtherwebsite.com") { MocClient.new }
|
41
|
+
sessions.get_or_set("https://www.someOtherwebsiteToo.com") { MocClient.new }
|
42
42
|
session = sessions.instance_variable_get(:@_sessions)
|
43
|
-
session.each_value {|v| expect(v.reaper).to
|
43
|
+
session.each_value {|v| expect(v.reaper).to eql(false)}
|
44
44
|
end
|
45
45
|
|
46
46
|
end
|
@@ -49,7 +49,7 @@ describe HotTub::Sessions do
|
|
49
49
|
it "should pass run to pool" do
|
50
50
|
url = HotTub::Server.url
|
51
51
|
sessions = HotTub::Sessions.new
|
52
|
-
sessions.
|
52
|
+
sessions.get_or_set(url) do
|
53
53
|
uri = URI.parse(url)
|
54
54
|
http = Net::HTTP.new(uri.host, uri.port)
|
55
55
|
http.use_ssl = false
|
@@ -68,8 +68,8 @@ describe HotTub::Sessions do
|
|
68
68
|
describe '#clean!' do
|
69
69
|
it "should clean all pools in sessions" do
|
70
70
|
sessions = HotTub::Sessions.new
|
71
|
-
sessions.
|
72
|
-
sessions.
|
71
|
+
sessions.get_or_set('foo') { |url| MocClient.new(url) }
|
72
|
+
sessions.get_or_set('bar') { |url| MocClient.new(url) }
|
73
73
|
sessions.clean!
|
74
74
|
sessions.instance_variable_get(:@_sessions).each_pair do |k,v|
|
75
75
|
v.instance_variable_get(:@_pool).each do |c|
|
@@ -82,8 +82,8 @@ describe HotTub::Sessions do
|
|
82
82
|
describe '#drain!' do
|
83
83
|
it "should drain all pools in sessions" do
|
84
84
|
sessions = HotTub::Sessions.new
|
85
|
-
sessions.
|
86
|
-
sessions.
|
85
|
+
sessions.get_or_set('foo') { |url| MocClient.new(url) }
|
86
|
+
sessions.get_or_set('bar') { |url| MocClient.new(url) }
|
87
87
|
sessions.drain!
|
88
88
|
expect(sessions.instance_variable_get(:@_sessions)).to_not be_empty
|
89
89
|
end
|
@@ -92,8 +92,8 @@ describe HotTub::Sessions do
|
|
92
92
|
describe '#reap!' do
|
93
93
|
it "should clean all pools in sessions" do
|
94
94
|
sessions = HotTub::Sessions.new
|
95
|
-
sessions.
|
96
|
-
sessions.
|
95
|
+
sessions.get_or_set('foo') { |url| MocClient.new(url) }
|
96
|
+
sessions.get_or_set('bar') { |url| MocClient.new(url) }
|
97
97
|
sessions.reap!
|
98
98
|
sessions.instance_variable_get(:@_sessions).each_pair do |k,v|
|
99
99
|
v.instance_variable_get(:@_pool).each do |c|
|
@@ -106,8 +106,8 @@ describe HotTub::Sessions do
|
|
106
106
|
describe '#reset!' do
|
107
107
|
it "should reset all pools in sessions" do
|
108
108
|
sessions = HotTub::Sessions.new
|
109
|
-
sessions.
|
110
|
-
sessions.
|
109
|
+
sessions.get_or_set('foo') { |url| MocClient.new(url) }
|
110
|
+
sessions.get_or_set('bar') { |url| MocClient.new(url) }
|
111
111
|
sessions.reset!
|
112
112
|
sessions.instance_variable_get(:@_sessions).each_pair do |k,v|
|
113
113
|
expect(v.instance_variable_get(:@_pool)).to be_empty
|
metadata
CHANGED
@@ -1,95 +1,95 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hot_tub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joshua Mckinney
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-11-
|
11
|
+
date: 2015-11-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
14
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
15
|
requirements:
|
17
16
|
- - ">="
|
18
17
|
- !ruby/object:Gem::Version
|
19
18
|
version: '0'
|
19
|
+
name: rspec
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
|
-
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
|
29
|
-
requirement: !ruby/object:Gem::Requirement
|
28
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
29
|
requirements:
|
31
30
|
- - ">="
|
32
31
|
- !ruby/object:Gem::Version
|
33
32
|
version: '0'
|
33
|
+
name: rspec-autotest
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
43
|
requirements:
|
45
44
|
- - ">="
|
46
45
|
- !ruby/object:Gem::Version
|
47
46
|
version: '0'
|
47
|
+
name: autotest
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
|
-
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
|
57
|
-
requirement: !ruby/object:Gem::Requirement
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
57
|
requirements:
|
59
58
|
- - ">="
|
60
59
|
- !ruby/object:Gem::Version
|
61
60
|
version: '0'
|
61
|
+
name: sinatra
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
|
-
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
72
71
|
requirements:
|
73
72
|
- - "~>"
|
74
73
|
- !ruby/object:Gem::Version
|
75
74
|
version: '2.0'
|
75
|
+
name: puma
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
|
-
|
78
|
+
requirement: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '2.0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
|
85
|
-
requirement: !ruby/object:Gem::Requirement
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
85
|
requirements:
|
87
86
|
- - ">="
|
88
87
|
- !ruby/object:Gem::Version
|
89
88
|
version: '0'
|
89
|
+
name: excon
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
|
-
|
92
|
+
requirement: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|