hot_tub 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +1 -4
- data/README.md +7 -6
- data/hot_tub.gemspec +1 -1
- data/lib/hot_tub/session.rb +9 -4
- data/lib/hot_tub/version.rb +1 -1
- data/spec/helpers/server.rb +28 -0
- data/spec/pool_spec.rb +3 -3
- data/spec/session_spec.rb +33 -13
- data/spec/spec_helper.rb +2 -0
- metadata +8 -8
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -13,7 +13,7 @@ There are a couple Ruby connection pooling libraries out there but HotTub differ
|
|
13
13
|
* Set to expand pool under load that is eventually reaped back down to set size (never_block), can be disabled
|
14
14
|
* Attempts to close clients/connections at_exit
|
15
15
|
|
16
|
-
### HotTub::
|
16
|
+
### HotTub::Session
|
17
17
|
* Thread safe
|
18
18
|
* The same api as HotTub::Pool
|
19
19
|
* Can be used with HotTub::Pool or any client library
|
@@ -61,7 +61,7 @@ Close and clean can be defined at initialization with lambdas, if they are not d
|
|
61
61
|
pool = HotTub::Pool.new({:size => 10, :close => lambda {|clnt| clnt.close}}) { MyHttpLib.new }
|
62
62
|
pool.run { |clnt| clnt.get(@@url,query).body }
|
63
63
|
|
64
|
-
## HotTub::
|
64
|
+
## HotTub::Session Usage
|
65
65
|
HotTub::Sessions are a synchronized hash of clients/pools and are implemented similar HotTub::Pool.
|
66
66
|
For example, Excon is thread safe but you set a single url at the client level so sessions
|
67
67
|
are handy if you need to access multiple urls but would prefer a single object.
|
@@ -69,7 +69,7 @@ are handy if you need to access multiple urls but would prefer a single object.
|
|
69
69
|
require 'hot_tub'
|
70
70
|
require 'excon'
|
71
71
|
# Our client block must accept the url argument
|
72
|
-
sessions = HotTub::
|
72
|
+
sessions = HotTub::Session.new {|url| Excon.new(url) }
|
73
73
|
|
74
74
|
sessions.run("http://somewebservice.com") do |clnt|
|
75
75
|
puts clnt.get(:query => {:some => 'stuff'}).response_header.status
|
@@ -81,16 +81,17 @@ are handy if you need to access multiple urls but would prefer a single object.
|
|
81
81
|
puts clnt.get.response_header.status
|
82
82
|
end
|
83
83
|
|
84
|
-
### HotTub::
|
84
|
+
### HotTub::Session with HotTub::Pool
|
85
85
|
Suppose you have a client that is not thread safe, you can use HotTub::Pool with HotTub::Sessions to get what you need.
|
86
86
|
|
87
87
|
require 'hot_tub'
|
88
88
|
require "em-synchrony"
|
89
89
|
require "em-synchrony/em-http"
|
90
|
-
# Our client block must accept the url argument
|
91
90
|
|
91
|
+
# We ust tell HotTub::Session to use HotTub::Pool, pass any pool options in our
|
92
|
+
# options has, and our client block must accept the url argument
|
92
93
|
EM.synchrony do {
|
93
|
-
sessions = HotTub::
|
94
|
+
sessions = HotTub::Session.new(:with_pool => true, :size => 12) {|url| EM::HttpRequest.new(url, :inactivity_timeout => 0) }
|
94
95
|
|
95
96
|
sessions.run("http://somewebservice.com") do |clnt|
|
96
97
|
puts clnt.get(:query => results).response_header.status
|
data/hot_tub.gemspec
CHANGED
@@ -16,7 +16,7 @@ Gem::Specification.new do |s|
|
|
16
16
|
|
17
17
|
s.add_development_dependency "rspec"
|
18
18
|
s.add_development_dependency "sinatra"
|
19
|
-
s.add_development_dependency "puma"
|
19
|
+
s.add_development_dependency "puma", "~> 2.0.0.b7"
|
20
20
|
s.add_development_dependency "excon"
|
21
21
|
|
22
22
|
s.files = `git ls-files`.split("\n")
|
data/lib/hot_tub/session.rb
CHANGED
@@ -19,8 +19,8 @@ module HotTub
|
|
19
19
|
# end
|
20
20
|
#
|
21
21
|
# Example with Pool:
|
22
|
-
#
|
23
|
-
# sessions = HotTub::
|
22
|
+
# You can initialize a HotTub::Pool with each client by passing :with_pool as true and any pool options
|
23
|
+
# sessions = HotTub::Session.new(:with_pool => true, :size => 12) { EM::HttpRequest.new("http://somewebservice.com") }
|
24
24
|
#
|
25
25
|
# sessions.run("http://wwww.yahoo.com") do |conn|
|
26
26
|
# p conn.head.response_header.status
|
@@ -30,6 +30,7 @@ module HotTub
|
|
30
30
|
# p conn.head.response_header.status
|
31
31
|
# end
|
32
32
|
#
|
33
|
+
#
|
33
34
|
def initialize(options={},&client_block)
|
34
35
|
raise ArgumentError, "HotTub::Sessions requre a block on initialization that accepts a single argument" unless block_given?
|
35
36
|
at_exit { close_all } # close connections at exit
|
@@ -45,7 +46,11 @@ module HotTub
|
|
45
46
|
key = to_key(url)
|
46
47
|
return @sessions[key] if @sessions[key]
|
47
48
|
@mutex.synchronize do
|
48
|
-
|
49
|
+
if @options[:with_pool]
|
50
|
+
@sessions[key] = HotTub::Pool.new(@options) { @client_block.call(url) }
|
51
|
+
else
|
52
|
+
@sessions[key] = @client_block.call(url) if @sessions[key].nil?
|
53
|
+
end
|
49
54
|
@sessions[key]
|
50
55
|
end
|
51
56
|
end
|
@@ -84,7 +89,7 @@ module HotTub
|
|
84
89
|
else
|
85
90
|
raise ArgumentError, "you must pass a string or a URI object"
|
86
91
|
end
|
87
|
-
"#{uri.scheme}
|
92
|
+
"#{uri.scheme}://#{uri.host}:#{uri.port}"
|
88
93
|
end
|
89
94
|
end
|
90
95
|
end
|
data/lib/hot_tub/version.rb
CHANGED
data/spec/helpers/server.rb
CHANGED
@@ -35,4 +35,32 @@ module HotTub
|
|
35
35
|
'http://127.0.0.1:9595' << path
|
36
36
|
end
|
37
37
|
end
|
38
|
+
|
39
|
+
class Server2 < Sinatra::Base
|
40
|
+
|
41
|
+
def self.run
|
42
|
+
@events = Puma::Events.new STDOUT, STDERR
|
43
|
+
@server = Puma::Server.new HotTub::Server.new, @events
|
44
|
+
@server.min_threads = 0
|
45
|
+
@server.max_threads = 20
|
46
|
+
@server.add_tcp_listener '127.0.0.1', 9393
|
47
|
+
@server.run
|
48
|
+
end
|
49
|
+
|
50
|
+
set :server, 'puma'
|
51
|
+
set :port, 9393
|
52
|
+
|
53
|
+
|
54
|
+
get '/quick' do
|
55
|
+
(Random.new.rand(0..999999).to_s)
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.teardown
|
59
|
+
@server.stop(true) if @server
|
60
|
+
end
|
61
|
+
|
62
|
+
def self.url
|
63
|
+
'http://127.0.0.1:9393/quick'
|
64
|
+
end
|
65
|
+
end
|
38
66
|
end
|
data/spec/pool_spec.rb
CHANGED
@@ -144,7 +144,7 @@ describe HotTub::Pool do
|
|
144
144
|
pool.run{|connection| connection.get }
|
145
145
|
end
|
146
146
|
end
|
147
|
-
sleep(
|
147
|
+
sleep(1)
|
148
148
|
threads.each do |t|
|
149
149
|
t.join
|
150
150
|
end
|
@@ -155,12 +155,12 @@ describe HotTub::Pool do
|
|
155
155
|
it "should not add connections to pool beyond specified size" do
|
156
156
|
pool = HotTub::Pool.new({:size => 1, :never_block => false, :blocking_timeout => 10}) { MocClient.new }
|
157
157
|
threads = []
|
158
|
-
|
158
|
+
2.times.each do
|
159
159
|
threads << Thread.new do
|
160
160
|
pool.run{|connection| connection.get }
|
161
161
|
end
|
162
162
|
end
|
163
|
-
sleep(
|
163
|
+
sleep(1)
|
164
164
|
threads.each do |t|
|
165
165
|
t.join
|
166
166
|
end
|
data/spec/session_spec.rb
CHANGED
@@ -23,13 +23,13 @@ describe HotTub::Session do
|
|
23
23
|
describe '#to_url' do
|
24
24
|
context "passed URL string" do
|
25
25
|
it "should return key with URI scheme-domain" do
|
26
|
-
@sessions.send(:to_key,@url).should eql("#{@uri.scheme}
|
26
|
+
@sessions.send(:to_key,@url).should eql("#{@uri.scheme}://#{@uri.host}:#{@uri.port}")
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
context "passed URI" do
|
31
31
|
it "should return key with URI scheme-domain" do
|
32
|
-
@sessions.send(:to_key,@uri).should eql("#{@uri.scheme}
|
32
|
+
@sessions.send(:to_key,@uri).should eql("#{@uri.scheme}://#{@uri.host}:#{@uri.port}")
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
@@ -68,35 +68,55 @@ describe HotTub::Session do
|
|
68
68
|
it "should set key with URI scheme-domain" do
|
69
69
|
@sessions.sessions(@url)
|
70
70
|
sessions = @sessions.instance_variable_get(:@sessions)
|
71
|
-
sessions["#{@uri.scheme}
|
71
|
+
sessions["#{@uri.scheme}://#{@uri.host}:#{@uri.port}"].should be_a(MocClient)
|
72
72
|
end
|
73
73
|
end
|
74
74
|
context "passed URI" do
|
75
75
|
it "should set key with URI scheme-domain" do
|
76
76
|
@sessions.sessions(@uri)
|
77
77
|
sessions = @sessions.instance_variable_get(:@sessions)
|
78
|
-
sessions["#{@uri.scheme}
|
78
|
+
sessions["#{@uri.scheme}://#{@uri.host}:#{@uri.port}"].should be_a(MocClient)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "with_pool" do
|
83
|
+
it "should initialize a new HotTub::Pool" do
|
84
|
+
session_with_pool = HotTub::Session.new({:with_pool => true}) { |url| MocClient.new(url) }
|
85
|
+
pool = session_with_pool.sessions(@url)
|
86
|
+
pool.should be_a(HotTub::Pool)
|
79
87
|
end
|
80
88
|
end
|
81
89
|
end
|
82
90
|
|
83
91
|
describe '#run' do
|
84
92
|
it "should work" do
|
85
|
-
|
86
|
-
|
93
|
+
url = HotTub::Server.url
|
94
|
+
sessions = HotTub::Session.new { |url| Excon.new(url) }
|
87
95
|
result = nil
|
88
|
-
|
89
|
-
result = conn.get
|
96
|
+
sessions.run(url) do |conn|
|
97
|
+
result = conn.get.status
|
98
|
+
end
|
99
|
+
result.should eql(200)
|
100
|
+
end
|
101
|
+
|
102
|
+
context "with_pool" do
|
103
|
+
it "should pass run to pool" do
|
104
|
+
url = HotTub::Server.url
|
105
|
+
session_with_pool = HotTub::Session.new({:with_pool => true}) { |url| Excon.new(url) }
|
106
|
+
result = nil
|
107
|
+
session_with_pool.run(url) do |conn|
|
108
|
+
result = conn.get.status
|
109
|
+
end
|
110
|
+
result.should eql(200)
|
90
111
|
end
|
91
|
-
result.should_not be_nil
|
92
112
|
end
|
93
113
|
end
|
94
114
|
|
95
115
|
context 'threads' do
|
96
116
|
it "should work" do
|
97
117
|
url = HotTub::Server.url
|
98
|
-
url2 =
|
99
|
-
session = HotTub::Session.new { |url| Excon.new(url)}
|
118
|
+
url2 = HotTub::Server2.url
|
119
|
+
session = HotTub::Session.new(:with_pool => true) { |url| Excon.new(url)}
|
100
120
|
failed = false
|
101
121
|
start_time = Time.now
|
102
122
|
stop_time = nil
|
@@ -106,8 +126,8 @@ describe HotTub::Session do
|
|
106
126
|
10.times.each do
|
107
127
|
threads << Thread.new do
|
108
128
|
# MocClient is not thread safe so lets initialize a new instance for each
|
109
|
-
session.run(url) { |clnt| Thread.current[:result] =
|
110
|
-
session.run(url2) { |clnt| Thread.current[:result] =
|
129
|
+
session.run(url) { |clnt| Thread.current[:result] = clnt.get.status }
|
130
|
+
session.run(url2) { |clnt| Thread.current[:result] = clnt.get.status }
|
111
131
|
end
|
112
132
|
end
|
113
133
|
threads.each do |t|
|
data/spec/spec_helper.rb
CHANGED
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.
|
4
|
+
version: 0.2.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-04-
|
12
|
+
date: 2013-04-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -48,17 +48,17 @@ dependencies:
|
|
48
48
|
requirement: !ruby/object:Gem::Requirement
|
49
49
|
none: false
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ~>
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 2.0.0.b7
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
none: false
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
61
|
+
version: 2.0.0.b7
|
62
62
|
- !ruby/object:Gem::Dependency
|
63
63
|
name: excon
|
64
64
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
116
116
|
version: '0'
|
117
117
|
segments:
|
118
118
|
- 0
|
119
|
-
hash: -
|
119
|
+
hash: -4288862352751305264
|
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: -
|
128
|
+
hash: -4288862352751305264
|
129
129
|
requirements: []
|
130
130
|
rubyforge_project: hot_tub
|
131
131
|
rubygems_version: 1.8.25
|