hot_tub 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +1 -1
- data/lib/hot_tub/session.rb +15 -13
- data/lib/hot_tub/version.rb +1 -1
- data/spec/session_spec.rb +12 -5
- metadata +13 -14
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::
|
13
|
+
@@pool = HotTub::Session.new({:size => 2 :client => HotTub::EmSynchronyClient.new('https://google.com'), :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
|
data/lib/hot_tub/session.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
1
|
module HotTub
|
2
2
|
class Session
|
3
|
-
AVAILABLE_CLIENTS = ["NetHttpClient","EmSynchronyClient"]
|
4
3
|
|
5
4
|
# OPTIONS
|
6
5
|
# * :size - number of connections for each pool
|
@@ -12,22 +11,25 @@ module HotTub
|
|
12
11
|
# instead of raising this number
|
13
12
|
# :never_block - if set to true, a connection will always be returned, but
|
14
13
|
# these extra connections are not added to the pool when the request is completed
|
15
|
-
def initialize(options={})
|
14
|
+
def initialize(options={})
|
16
15
|
@options = {
|
17
16
|
:size => 5,
|
18
17
|
:never_block => false,
|
19
|
-
:blocking_timeout => 0.5
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
18
|
+
:blocking_timeout => 0.5
|
19
|
+
}.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
|
26
28
|
end
|
27
29
|
|
28
30
|
def pool
|
29
31
|
@mutex.synchronize do
|
30
|
-
|
32
|
+
add_client if add_client?
|
31
33
|
@pool
|
32
34
|
end
|
33
35
|
end
|
@@ -102,12 +104,12 @@ module HotTub
|
|
102
104
|
end
|
103
105
|
|
104
106
|
private
|
105
|
-
|
106
|
-
def
|
107
|
+
|
108
|
+
def add_client?
|
107
109
|
(@pool.empty? && (@pool_data[:current_size] < @options[:size]))
|
108
110
|
end
|
109
111
|
|
110
|
-
def
|
112
|
+
def add_client
|
111
113
|
HotTub.logger.info "Adding HotTub client: #{@client.class.name} to pool"
|
112
114
|
@pool_data[:current_size] += 1
|
113
115
|
@pool << new_client
|
data/lib/hot_tub/version.rb
CHANGED
data/spec/session_spec.rb
CHANGED
@@ -9,12 +9,19 @@ 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
|
before(:each) do
|
13
19
|
@url = "http://www.testurl123.com/"
|
14
20
|
@tub = HotTub::Session.new(:client => MocClient.new(@url))
|
15
21
|
end
|
16
22
|
|
17
23
|
context 'default configuration' do
|
24
|
+
|
18
25
|
it "should have @pool_size of 5" do
|
19
26
|
@tub.instance_variable_get(:@options)[:size].should eql(5)
|
20
27
|
end
|
@@ -24,24 +31,24 @@ describe HotTub::Session do
|
|
24
31
|
end
|
25
32
|
end
|
26
33
|
|
27
|
-
describe '#
|
34
|
+
describe '#add_client?' do
|
28
35
|
it "should be true if @pool_data[:length] is less than desired pool size and
|
29
36
|
the pool is empty?"do
|
30
37
|
@tub.instance_variable_set(:@pool_data,{:current_size => 1})
|
31
|
-
@tub.send(:
|
38
|
+
@tub.send(:add_client?).should be_true
|
32
39
|
end
|
33
40
|
|
34
41
|
it "should be false pool has reached pool_size" do
|
35
42
|
@tub.instance_variable_set(:@pool_data,{:current_size => 5})
|
36
43
|
@tub.instance_variable_set(:@pool,
|
37
44
|
["connection","connection","connection","connection","connection"])
|
38
|
-
@tub.send(:
|
45
|
+
@tub.send(:add_client?).should be_false
|
39
46
|
end
|
40
47
|
end
|
41
48
|
|
42
|
-
describe '#
|
49
|
+
describe '#add_client' do
|
43
50
|
it "should add connections for supplied url"do
|
44
|
-
@tub.send(:
|
51
|
+
@tub.send(:add_client)
|
45
52
|
@tub.instance_variable_get(:@pool).should_not be_nil
|
46
53
|
end
|
47
54
|
end
|
metadata
CHANGED
@@ -1,27 +1,27 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hot_tub
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Joshua Mckinney
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-18 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
|
16
|
+
requirement: &70155079255920 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
17
18
|
requirements:
|
18
19
|
- - ! '>='
|
19
20
|
- !ruby/object:Gem::Version
|
20
21
|
version: '0'
|
21
|
-
none: false
|
22
|
-
requirement: *2056
|
23
|
-
prerelease: false
|
24
22
|
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70155079255920
|
25
25
|
description: A very simple ruby pool gem
|
26
26
|
email:
|
27
27
|
- joshmckin@gmail.com
|
@@ -51,26 +51,26 @@ files:
|
|
51
51
|
- spec/test_helper_methods.rb
|
52
52
|
homepage: https://github.com/JoshMcKin/hot_tub
|
53
53
|
licenses: []
|
54
|
-
post_install_message:
|
54
|
+
post_install_message:
|
55
55
|
rdoc_options: []
|
56
56
|
require_paths:
|
57
57
|
- lib
|
58
58
|
required_ruby_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
59
60
|
requirements:
|
60
61
|
- - ! '>='
|
61
62
|
- !ruby/object:Gem::Version
|
62
63
|
version: '0'
|
63
|
-
none: false
|
64
64
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
65
66
|
requirements:
|
66
67
|
- - ! '>='
|
67
68
|
- !ruby/object:Gem::Version
|
68
69
|
version: '0'
|
69
|
-
none: false
|
70
70
|
requirements: []
|
71
71
|
rubyforge_project: hot_tub
|
72
|
-
rubygems_version: 1.8.
|
73
|
-
signing_key:
|
72
|
+
rubygems_version: 1.8.6
|
73
|
+
signing_key:
|
74
74
|
specification_version: 3
|
75
75
|
summary: A very simple ruby pool gem
|
76
76
|
test_files:
|
@@ -80,4 +80,3 @@ test_files:
|
|
80
80
|
- spec/session_spec.rb
|
81
81
|
- spec/spec_helper.rb
|
82
82
|
- spec/test_helper_methods.rb
|
83
|
-
...
|