hot_tub 0.0.2 → 0.0.3

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/README.md CHANGED
@@ -10,7 +10,7 @@ HotTub is available through [Rubygems](https://rubygems.org/gems/hot_tub) and ca
10
10
 
11
11
  ## Setup
12
12
  class MyClass
13
- @@pool = HotTub::Session.new({:size => 2 :client => HotTub::EmSynchronyClient.new('https://google.com'), :never_block => true})
13
+ @@pool = HotTub::Session.new(HotTub::ExconClient.new('https://google.com'),{:size => 2, :never_block => true})
14
14
 
15
15
  def self.fetch_results(query)
16
16
  @@pool.get(:query => query) # keepalive has be defaulted to true in the client
@@ -23,14 +23,18 @@ module HotTub
23
23
 
24
24
  # Override this method to perform the necessary action for ensure a client
25
25
  # is clean for use.
26
- def sanitize_hot_tub_client
26
+ def clean
27
27
  @client
28
28
  end
29
29
 
30
- def close_hot_tub_client
30
+ def close
31
31
  @client
32
32
  end
33
33
 
34
+ def dup
35
+ self.class.new(@url,@options)
36
+ end
37
+
34
38
  class << self
35
39
  def mutex
36
40
  Mutex.new
@@ -9,7 +9,7 @@ module HotTub
9
9
  @client = EM::HttpRequest.new(url,options)
10
10
  end
11
11
 
12
- def sanitize_hot_tub_client
12
+ def clean
13
13
  if @client.conn && @client.conn.error?
14
14
  HotTub.logger.info "Sanitizing connection : #{EventMachine::report_connection_error_status(@client.conn.instance_variable_get(:@signature))}"
15
15
  @client.conn.close_connection
@@ -18,7 +18,7 @@ module HotTub
18
18
  @client
19
19
  end
20
20
 
21
- def close_hot_tub_client
21
+ def close
22
22
  @client.conn.close_connection if @client.conn
23
23
  end
24
24
 
@@ -31,10 +31,6 @@ module HotTub
31
31
  end
32
32
  end
33
33
 
34
- def dup
35
- self.class.new(@url,@options)
36
- end
37
-
38
34
  class << self
39
35
  # Use a fiber safe mutex
40
36
  def mutex
@@ -12,16 +12,13 @@ module HotTub
12
12
  end
13
13
 
14
14
  # pretty sure Excon handles this internally
15
- def sanitize_hot_tub_client
15
+ def clean
16
16
  @client
17
17
  end
18
18
 
19
- def close_hot_tub_client
19
+ def close
20
20
  @client.socket.close
21
21
  end
22
-
23
- def dup
24
- self.class.new(@url,@options)
25
- end
22
+
26
23
  end
27
24
  end
@@ -13,14 +13,14 @@ module HotTub
13
13
  end
14
14
 
15
15
  # pretty sure HttpClient handles this internally
16
- def sanitize_hot_tub_client
16
+ def clean
17
17
  @client
18
18
  end
19
19
 
20
- def close_hot_tub_client
20
+ def close
21
21
  @client.shutdown
22
22
  end
23
-
23
+ # HttpClient has different initialization attributes so we need a custom dup
24
24
  def dup
25
25
  self.class.new(@options)
26
26
  end
@@ -3,28 +3,23 @@ module HotTub
3
3
 
4
4
  # OPTIONS
5
5
  # * :size - number of connections for each pool
6
- # * :inactivity_timeout - number of seconds to wait before disconnecting,
7
- # setting to 0 means the connection will not be closed
6
+ # * :inactivity_timeout - number of seconds to wait before disconnecting, setting to 0 means the connection will not be closed
8
7
  # * :pool_timeout - the amount of seconds to block waiting for an availble connection,
9
8
  # because this is blocking it should be an extremely short amount of
10
9
  # time default to 0.5 seconds, if you need more consider enlarging your pool
11
10
  # instead of raising this number
12
11
  # :never_block - if set to true, a connection will always be returned, but
13
12
  # these extra connections are not added to the pool when the request is completed
14
- def initialize(options={})
13
+ def initialize(client,options={})
15
14
  @options = {
16
15
  :size => 5,
17
16
  :never_block => false,
18
17
  :blocking_timeout => 0.5
19
18
  }.merge(options || {})
20
- if @options[:client]
21
- @pool = []
22
- @pool_data = {:current_size => 0}
23
- @client = @options[:client]
24
- @mutex = (@client.respond_to?(:mutex) ? @client.mutex : Mutex.new)
25
- else
26
- raise ArgumentError, "The option :client is required for HotTub::Session.new"
27
- end
19
+ @pool = []
20
+ @pool_data = {:current_size => 0}
21
+ @client = client
22
+ @mutex = (@client.respond_to?(:mutex) ? @client.mutex : Mutex.new)
28
23
  end
29
24
 
30
25
  def pool
@@ -44,7 +39,6 @@ module HotTub
44
39
  # block until we get an available connection or Timeout::Error
45
40
  while client.nil?
46
41
  raise_alarm if alarm <= Time.now
47
-
48
42
  client = pool.shift
49
43
  if client.nil? && (@options[:never_block])
50
44
  HotTub.logger.info "Adding never_block client for #{@client.class.name}, will not be returned to pool."
@@ -52,14 +46,14 @@ module HotTub
52
46
  client.mark_temporary
53
47
  end
54
48
  end
55
- client.sanitize_hot_tub_client
49
+ client.clean
56
50
  client
57
51
  end
58
52
 
59
53
  # return a client to the pool
60
54
  def return_client(client)
61
55
  if client.temporary?
62
- client.close_hot_tub_client # Too hot in the hot tub...
56
+ client.close # Too hot in the hot tub...
63
57
  else
64
58
  @pool << client
65
59
  end
@@ -70,11 +64,11 @@ module HotTub
70
64
  # is used for mulitple requests. For HTTP requests make sure you request has
71
65
  # keep-alive properly set for your client
72
66
  # EX:
73
- # @pool = HotTub.new("https://some_web_site.com")
67
+ # @pool = HotTub.new(HotTub::ExconClient.new("https://some_web_site.com"))
74
68
  # results = []
75
69
  # @pool.run do |client|
76
- # results.push (client.get(:query => {:foo => "bar"}, :keepalive => true))
77
- # results.push (client.get(:query => {:bar => "foo"}, :keepalive => true)) # reuse client
70
+ # results.push (client.get(:query => {:foo => "bar"}))
71
+ # results.push (client.get(:query => {:bar => "foo"})) # reuse client
78
72
  # end
79
73
  #
80
74
  def run(&block)
@@ -92,9 +86,9 @@ module HotTub
92
86
  # Let pool instance respond to client methods. For HTTP request make sure you
93
87
  # requests has keep-alive properly set for your client
94
88
  # EX:
95
- # @pool = HotTub.new("https://some_web_site.com")
96
- # r1 = @pool.get(:query => {:foo => "bar"}, :keepalive => true)
97
- # r2 = @pool.get(:query => {:bar => "foo"}, :keepalive => true) # uses a different client
89
+ # @pool = HotTub.new(HotTub::ExconClient.new("https://some_web_site.com"))
90
+ # r1 = @pool.get(:query => {:foo => "bar"})
91
+ # r2 = @pool.get(:query => {:bar => "foo"}) # uses a different client
98
92
  #
99
93
  def method_missing(method, *args, &blk)
100
94
  client = fetch
@@ -1,3 +1,3 @@
1
1
  module HotTub
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
data/spec/session_spec.rb CHANGED
@@ -9,15 +9,10 @@ require 'spec_helper'
9
9
  end
10
10
 
11
11
  describe HotTub::Session do
12
-
13
- describe '#initialize' do
14
- it "should raise ArgumentError if :client option is nil" do
15
- lambda { HotTub::Session.new}.should raise_error(ArgumentError)
16
- end
17
- end
12
+
18
13
  before(:each) do
19
14
  @url = "http://www.testurl123.com/"
20
- @tub = HotTub::Session.new(:client => MocClient.new(@url))
15
+ @tub = HotTub::Session.new(MocClient.new(@url))
21
16
  end
22
17
 
23
18
  context 'default configuration' do
@@ -11,8 +11,7 @@ module TestHelperMethods
11
11
  normal = Time.now - start
12
12
 
13
13
  # we want a whole new pool to make sure we don't cheat
14
- connection_pool = HotTub::Session.new(
15
- :client => client)
14
+ connection_pool = HotTub::Session.new(client)
16
15
 
17
16
  start = Time.now
18
17
  50.times.each do
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.2
4
+ version: 0.0.3
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-06-18 00:00:00.000000000Z
12
+ date: 2012-07-02 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70155079255920 !ruby/object:Gem::Requirement
16
+ requirement: &70179585887100 !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: *70155079255920
24
+ version_requirements: *70179585887100
25
25
  description: A very simple ruby pool gem
26
26
  email:
27
27
  - joshmckin@gmail.com