hot_tub 0.2.3 → 0.2.4

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/lib/hot_tub/pool.rb CHANGED
@@ -2,7 +2,7 @@ module HotTub
2
2
  class Pool
3
3
  include HotTub::KnownClients
4
4
  attr_reader :current_size, :fetching_client, :last_activity
5
-
5
+
6
6
  # Thread-safe lazy connection pool
7
7
  #
8
8
  # == Example EM-Http-Request
@@ -11,8 +11,8 @@ module HotTub
11
11
  #
12
12
  # HotTub::Pool defaults never_block to true, which means if we run out of
13
13
  # connections simply create a new client to continue operations.
14
- # The pool will grow and extra connections will be resued until activity dies down.
15
- # If you would like to block and possibly throw an exception rather than temporarily
14
+ # The pool will grow and extra connections will be resued until activity dies down.
15
+ # If you would like to block and possibly throw an exception rather than temporarily
16
16
  # grow the set :size, set :never_block to false; blocking_timeout defaults to 10 seconds.
17
17
  #
18
18
  # == Example without #never_block (will BlockingTimeout exception)
@@ -37,7 +37,7 @@ module HotTub
37
37
  }.merge(options)
38
38
  @pool = []
39
39
  @current_size = 0
40
- @mutex = (HotTub.em_synchrony? ? EM::Synchrony::Thread::Mutex.new : Mutex.new)
40
+ @mutex = (fiber_mutex? ? EM::Synchrony::Thread::Mutex.new : Mutex.new)
41
41
  @last_activity = Time.now
42
42
  @fetching_client = false
43
43
  end
@@ -70,6 +70,14 @@ module HotTub
70
70
 
71
71
  private
72
72
 
73
+ def fiber_mutex?
74
+ begin
75
+ (HotTub.em_synchrony? && @client_block.call.is_a?(EventMachine::HttpConnection))
76
+ rescue
77
+ false
78
+ end
79
+ end
80
+
73
81
  # Returns an instance of the client for this pool.
74
82
  def client
75
83
  clnt = nil
@@ -110,7 +118,7 @@ module HotTub
110
118
  @fetching_client = false
111
119
  clnt
112
120
  end
113
- ensure
121
+ ensure
114
122
  reap_pool if reap_pool?
115
123
  end
116
124
 
@@ -37,7 +37,7 @@ module HotTub
37
37
  @options = options || {}
38
38
  @client_block = client_block
39
39
  @sessions = Hash.new
40
- @mutex = (HotTub.em_synchrony? ? EM::Synchrony::Thread::Mutex.new : Mutex.new)
40
+ @mutex = (fiber_mutex? ? EM::Synchrony::Thread::Mutex.new : Mutex.new)
41
41
  end
42
42
 
43
43
  # Synchronizes initialization of our sessions
@@ -81,6 +81,14 @@ module HotTub
81
81
 
82
82
  private
83
83
 
84
+ def fiber_mutex?
85
+ begin
86
+ (HotTub.em_synchrony? && @client_block.call("http://moc").is_a?(EventMachine::HttpConnection))
87
+ rescue
88
+ false
89
+ end
90
+ end
91
+
84
92
  def to_key(url)
85
93
  if url.is_a?(String)
86
94
  uri = URI(url)
@@ -1,3 +1,3 @@
1
1
  module HotTub
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
data/spec/pool_spec.rb CHANGED
@@ -261,6 +261,32 @@ describe HotTub::Pool do
261
261
  end
262
262
 
263
263
  unless HotTub.jruby?
264
+ describe "fiber_mutex?" do
265
+ context 'EM::HttpRequest as client' do
266
+ before(:each) do
267
+ @pool = HotTub::Pool.new { EM::HttpRequest.new(HotTub::Server.url) }
268
+ end
269
+ context "EM::Synchrony is present" do
270
+ it "should be true" do
271
+ HotTub.stub(:em_synchrony?).and_return(true)
272
+ @pool.send(:fiber_mutex?).should be_true
273
+ end
274
+ end
275
+ context "EM::Synchrony is not present" do
276
+ it "should be false" do
277
+ HotTub.stub(:em_synchrony?).and_return(false)
278
+ @pool.send(:fiber_mutex?).should be_false
279
+ end
280
+ end
281
+ end
282
+ context 'client is not EM::HttpRequest' do
283
+ it "should be false" do
284
+ pool = HotTub::Pool.new {|url| MocClient.new}
285
+ pool.send(:fiber_mutex?).should be_false
286
+ end
287
+ end
288
+ end
289
+
264
290
  context 'EM:HTTPRequest' do
265
291
  before(:each) do
266
292
  @url = HotTub::Server.url
@@ -269,7 +295,7 @@ describe HotTub::Pool do
269
295
  it "should work" do
270
296
  EM.synchrony do
271
297
  status = []
272
- c = HotTub::Pool.new {EM::HttpRequest.new(@url)}
298
+ c = HotTub::Pool.new(:fiber_mutex => true) {EM::HttpRequest.new(@url)}
273
299
  c.run { |conn| status << conn.head(:keepalive => true).response_header.status}
274
300
  c.run { |conn| status << conn.ahead(:keepalive => true).response_header.status}
275
301
  c.run { |conn| status << conn.head(:keepalive => true).response_header.status}
@@ -282,15 +308,15 @@ describe HotTub::Pool do
282
308
  context 'fibers' do
283
309
  it "should work" do
284
310
  EM.synchrony do
285
- pool = HotTub::Pool.new({:size => 5}) {EM::HttpRequest.new(@url)}
311
+ pool = HotTub::Pool.new({:size => 5, :fiber_mutex => true}) {EM::HttpRequest.new(@url)}
286
312
  failed = false
287
313
  fibers = []
288
314
  lambda {
289
315
  10.times.each do
290
316
  fibers << Fiber.new do
291
- pool.run{|connection|
317
+ pool.run{|connection|
292
318
  s = connection.head(:keepalive => true).response_header.status
293
- failed = true unless s == 200}
319
+ failed = true unless s == 200}
294
320
  end
295
321
  end
296
322
  fibers.each do |f|
data/spec/session_spec.rb CHANGED
@@ -145,10 +145,38 @@ describe HotTub::Session do
145
145
  end
146
146
 
147
147
  unless HotTub.jruby?
148
+
149
+ describe "fiber_mutex?" do
150
+
151
+ context 'EM::HttpRequest as client' do
152
+ before(:each) do
153
+ @session = HotTub::Session.new {|url| EM::HttpRequest.new(url)}
154
+ end
155
+ context "EM::Synchrony is present" do
156
+ it "should be true" do
157
+ HotTub.stub(:em_synchrony?).and_return(true)
158
+ @session.send(:fiber_mutex?).should be_true
159
+ end
160
+ end
161
+ context "EM::Synchrony is not present" do
162
+ it "should be false" do
163
+ HotTub.stub(:em_synchrony?).and_return(false)
164
+ @session.send(:fiber_mutex?).should be_false
165
+ end
166
+ end
167
+ end
168
+ context 'client is not EM::HttpRequest' do
169
+ it "should be false" do
170
+ session = HotTub::Session.new {|url| MocClient.new}
171
+ session.send(:fiber_mutex?).should be_false
172
+ end
173
+ end
174
+ end
175
+
148
176
  context 'fibers' do
149
177
  it "should work" do
150
178
  EM.synchrony do
151
- sessions = HotTub::Session.new {|url| HotTub::Pool.new({:size => 5}) {EM::HttpRequest.new(url)}}
179
+ sessions = HotTub::Session.new(:with_pool => true) {|url| EM::HttpRequest.new(url)}
152
180
  failed = false
153
181
  fibers = []
154
182
  lambda {
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.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
116
116
  version: '0'
117
117
  segments:
118
118
  - 0
119
- hash: 1539670545928436962
119
+ hash: -3003548188710174576
120
120
  required_rubygems_version: !ruby/object:Gem::Requirement
121
121
  none: false
122
122
  requirements:
@@ -125,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
125
125
  version: '0'
126
126
  segments:
127
127
  - 0
128
- hash: 1539670545928436962
128
+ hash: -3003548188710174576
129
129
  requirements: []
130
130
  rubyforge_project: hot_tub
131
131
  rubygems_version: 1.8.25