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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f245ec552fdece7dd116f7e0969cd88aebb956e1
4
- data.tar.gz: f5bc9530fb1ff42c167bb05f0ab72b6265f16a63
3
+ metadata.gz: af5ab22d571fb050b860aa00c6272f9d3e1ca564
4
+ data.tar.gz: 7aab55538f57d844feca8b389285d705707de8d3
5
5
  SHA512:
6
- metadata.gz: 03dbfae650c78a6adbc7835d2c41f26a1162b84ff9a15bb06985353383f58814eb7fd60804396a83d028d92cee19a7f605fb24db00d10251fec1b1202335d650
7
- data.tar.gz: 1749e5b189cb802fde7c795d5f79b7c06fb9c5853b65241ae066b3eef1667bdd02574d58309778ee3b9dfcff80c8f0d1dca1f610eb5a16e12ce8ba66a8118df8
6
+ metadata.gz: 62623f8ff72d0f9fa386e37aab4a023401be235f91478ed9e0e78f4d329787d05bf72baaf8670e5bba6a9a6203f7f2c52e3659bfcb97fe8e002fa6bd4dfae580
7
+ data.tar.gz: 8ca0674fa6a19f4bb228f083d968ee2067503dc513e4658753af06427c43a8da2b6ba095c6f71808daa596b07298b8d01cd94fb080be1ed5bfb160b92861e7f6
data/.rspec CHANGED
@@ -1 +1,4 @@
1
- --color
1
+ --color
2
+ --warnings
3
+ --format documentation
4
+ --profile
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.add(URL, { :size => 12 }) do
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.add('yahoo', { :size => 5 }) { Excon.new("https://yahoo.com") }
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.add("redis", :size => 5, :max_size => 30) { Redis.new }
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|
@@ -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.block_yield { "foo" }
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.block_call { "foo" }
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.160000 0.000000 0.160000 ( 0.166351)
59
- # block call 0.690000 0.000000 0.690000 ( 0.684485)
60
- # block to yield 0.160000 0.000000 0.160000 ( 0.166744)
61
- # block to call 0.680000 0.000000 0.680000 ( 0.677085)
62
- # ----------------------------------------- total: 1.690000sec
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.160000 0.000000 0.160000 ( 0.165289)
66
- # block call 0.690000 0.010000 0.700000 ( 0.699424)
67
- # block to yield 0.180000 0.000000 0.180000 ( 0.172781)
68
- # block to call 0.700000 0.010000 0.710000 ( 0.702157)
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.164459 0.009985 0.174444 ( 0.136798)
73
- # block call 0.396599 0.001442 0.398041 ( 0.374256)
74
- # block to yield 0.095014 0.000532 0.095546 ( 0.068005)
75
- # block to call 0.386417 0.001135 0.387552 ( 0.370419)
76
- # ----------------------------------------- total: 1.055583sec
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.050738 0.000172 0.050910 ( 0.050869)
80
- # block call 0.359893 0.000935 0.360828 ( 0.360429)
81
- # block to yield 0.050676 0.000181 0.050857 ( 0.050787)
82
- # block to call 0.360640 0.001018 0.361658 ( 0.361311)
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.410000 0.010000 0.420000 ( 0.260548)
87
- # block call 0.420000 0.000000 0.420000 ( 0.356582)
88
- # block to yield 0.220000 0.000000 0.220000 ( 0.189081)
89
- # block to call 0.360000 0.010000 0.370000 ( 0.266201)
90
- # ----------------------------------------- total: 1.430000sec
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.170000 0.000000 0.170000 ( 0.173698)
94
- # block call 0.220000 0.000000 0.220000 ( 0.222841)
95
- # block to yield 0.180000 0.000000 0.180000 ( 0.172968)
96
- # block to call 0.220000 0.000000 0.220000 ( 0.224151)
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)
@@ -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.070000 0.040000 0.110000 ( 10.859963)
106
- # threaded size 5 0.080000 0.090000 0.170000 ( 2.288797)
107
- # threaded size 5, max 10 0.060000 0.060000 0.120000 ( 1.197574)
108
- # threaded, size 5, no max 0.030000 0.030000 0.060000 ( 0.222523)
109
- # threaded, HotTub.run 0.090000 0.090000 0.180000 ( 0.659862)
110
- # --------------------------------------------------- total: 0.640000sec
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.030000 0.110000 ( 10.871813)
114
- # threaded size 5 0.080000 0.090000 0.170000 ( 2.228075)
115
- # threaded size 5, max 10 0.060000 0.070000 0.130000 ( 1.204607)
116
- # threaded, size 5, no max 0.030000 0.020000 0.050000 ( 0.220836)
117
- # threaded, HotTub.run 0.090000 0.080000 0.170000 ( 0.670364)
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.152422 0.059901 0.212323 ( 11.104594)
123
- # threaded size 5 0.204700 0.192881 0.397581 ( 2.273955)
124
- # threaded size 5, max 10 0.166915 0.155789 0.322704 ( 1.161038)
125
- # threaded, size 5, no max 0.143544 0.055138 0.198682 ( 0.233619)
126
- # threaded, HotTub.run 0.184800 0.143068 0.327868 ( 0.699002)
127
- # --------------------------------------------------- total: 1.459158sec
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.146872 0.053339 0.200211 ( 11.129070)
131
- # threaded size 5 0.206160 0.199475 0.405635 ( 2.270780)
132
- # threaded size 5, max 10 0.201255 0.163359 0.364614 ( 1.205708)
133
- # threaded, size 5, no max 0.066599 0.067142 0.133741 ( 0.237895)
134
- # threaded, HotTub.run 0.533596 0.093450 0.627046 ( 0.670178)
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.760000 0.050000 0.810000 ( 11.191212)
140
- # threaded size 5 0.790000 0.120000 0.910000 ( 2.262856)
141
- # threaded size 5, max 10 0.600000 0.080000 0.680000 ( 1.160630)
142
- # threaded, size 5, no max 0.360000 0.040000 0.400000 ( 0.228086)
143
- # threaded, HotTub.run 0.860000 0.100000 0.960000 ( 0.673639)
144
- # --------------------------------------------------- total: 3.760000sec
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.520000 0.060000 0.580000 ( 11.079422)
148
- # threaded size 5 0.390000 0.100000 0.490000 ( 2.235107)
149
- # threaded size 5, max 10 0.290000 0.080000 0.370000 ( 1.171805)
150
- # threaded, size 5, no max 0.300000 0.030000 0.330000 ( 0.265258)
151
- # threaded, HotTub.run 0.580000 0.090000 0.670000 ( 0.672435)
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
- # Adds a new Pool to the global sessions
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
- GLOBAL_SESSIONS.add(url, opts, &client_block)
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 => lambda { |clnt| clnt.reset }
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
- if @clean_client.is_a?(Proc)
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
- @close_action = (@close_client || known_client_action(clnt,:close) || false) if @close_action.nil?
39
- if @close_action
34
+ @close_client = (known_client_action(clnt,:close) || false) if @close_client.nil?
35
+ if @close_client
40
36
  begin
41
- if @close_action.is_a?(Proc)
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
- if @reap_client.is_a?(Proc)
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 preform_client_block(clnt,action=nil)
77
- yield clnt
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
- def preform_client_method(clnt,action=nil)
81
- clnt.send(action)
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) # in seconds
96
- @wait_timeout = (opts[:wait_timeout] || 10) # in seconds
97
- @reap_timeout = (opts[:reap_timeout] || 600) # the interval to reap connections in seconds
98
- @max_size = (opts[:max_size] || 0) # maximum size of pool when non-blocking, 0 means no limit
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] # => lambda {|clnt| clnt.close} or :close
101
- @clean_client = opts[:clean] # => lambda {|clnt| clnt.clean} or :clean
102
- @reap_client = opts[:reap?] # => lambda {|clnt| clnt.reap?} or :reap? # should return boolean
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 = spawn_reaper unless (opts[:sessions] || (opts[:reaper] == false))
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 and re-spawn reaper.
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
 
@@ -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
 
@@ -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 add(key, pool_options={}, &client_block)
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)
@@ -1,3 +1,3 @@
1
1
  module HotTub
2
- VERSION = "0.5.2"
2
+ VERSION = "0.5.3"
3
3
  end
@@ -148,7 +148,6 @@ describe HotTub::Pool do
148
148
 
149
149
  it "should kill reaper" do
150
150
  pool.shutdown!
151
- sleep(0.01)
152
151
  expect(pool.instance_variable_get(:@reaper)).to be_nil
153
152
  end
154
153
 
@@ -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.add(key) { MocClient.new }
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.add("https://www.somewebsite.com") { MocClient.new }
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.add("https://www.somewebsite.com") { MocClient.new }
40
- sessions.add("https://www.someOtherwebsite.com") { MocClient.new }
41
- sessions.add("https://www.someOtherwebsiteToo.com") { MocClient.new }
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 be_nil}
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.add(url) do
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.add('foo') { |url| MocClient.new(url) }
72
- sessions.add('bar') { |url| MocClient.new(url) }
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.add('foo') { |url| MocClient.new(url) }
86
- sessions.add('bar') { |url| MocClient.new(url) }
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.add('foo') { |url| MocClient.new(url) }
96
- sessions.add('bar') { |url| MocClient.new(url) }
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.add('foo') { |url| MocClient.new(url) }
110
- sessions.add('bar') { |url| MocClient.new(url) }
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.2
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-04 00:00:00.000000000 Z
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rspec
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
- version_requirements: !ruby/object:Gem::Requirement
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
- name: rspec-autotest
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
- version_requirements: !ruby/object:Gem::Requirement
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
- name: autotest
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
- version_requirements: !ruby/object:Gem::Requirement
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
- name: sinatra
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
- version_requirements: !ruby/object:Gem::Requirement
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
- name: puma
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
- version_requirements: !ruby/object:Gem::Requirement
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
- name: excon
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
- version_requirements: !ruby/object:Gem::Requirement
92
+ requirement: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version