hot_tub 0.5.3 → 1.0.0

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: af5ab22d571fb050b860aa00c6272f9d3e1ca564
4
- data.tar.gz: 7aab55538f57d844feca8b389285d705707de8d3
3
+ metadata.gz: 74ee741ed84ebe1bfdc105cea7cd6ea36d110069
4
+ data.tar.gz: 213604cfc56cffe9fa593f4a4fe30bd139dafdcd
5
5
  SHA512:
6
- metadata.gz: 62623f8ff72d0f9fa386e37aab4a023401be235f91478ed9e0e78f4d329787d05bf72baaf8670e5bba6a9a6203f7f2c52e3659bfcb97fe8e002fa6bd4dfae580
7
- data.tar.gz: 8ca0674fa6a19f4bb228f083d968ee2067503dc513e4658753af06427c43a8da2b6ba095c6f71808daa596b07298b8d01cd94fb080be1ed5bfb160b92861e7f6
6
+ metadata.gz: 847dc6a4bd53ed60cddcd7103f341ec3b2f8e1c2fad9f380fd614ee7291cc850d0eb56fe44d8ad85e8f209f4566547fbd4b13225fadb9c8ba9f6159dbbd2056d
7
+ data.tar.gz: a82fd572a976426711af212b2d5123432883abadea80f75940097b44c1cc7755cefca6659f151c37fb569f5ec1a04fccb51143fc137aa34490b85ea418665e00
data/.travis.yml CHANGED
@@ -2,9 +2,9 @@ sudo: false
2
2
  cache: bundler
3
3
  language: ruby
4
4
  rvm:
5
- - 2.2
6
- - 2.1
7
- - 2.0.0
5
+ - 2.2.4
6
+ - 2.1.8
7
+ - 2.0.0-p648
8
8
  - ruby-head
9
9
  - rbx-2
10
10
  - jruby-head
data/HISTORY.md CHANGED
@@ -3,7 +3,14 @@ HotTub Changelog
3
3
 
4
4
  Head
5
5
  =======
6
- - None yet
6
+ - None yet.
7
+
8
+ 1.0.0
9
+ =======
10
+ - Allow setting a default client for HotTub::Sessions
11
+ - Deprecate HotTub.new
12
+ - Call pool.shutdown! when deleting a session
13
+ - Add Sessions.stage to lazy load pools
7
14
 
8
15
  0.5.3
9
16
  =======
data/README.md CHANGED
@@ -1,28 +1,6 @@
1
1
  # HotTub [![Build Status](https://travis-ci.org/JoshMcKin/hot_tub.png?branch=master)](https://travis-ci.org/JoshMcKin/hot_tub) [![Coverage Status](https://coveralls.io/repos/JoshMcKin/hot_tub/badge.png?branch=master)](https://coveralls.io/r/JoshMcKin/hot_tub)
2
2
 
3
- Flexible, thread-safe, connection pooling for Ruby. Configurable for any client you desire. Built in reaper th built in support for Net::HTTP and [Excon](https://github.com/excon/excon).
4
-
5
- ## Features
6
-
7
- ### HotTub::Pool
8
-
9
- * Thread safe
10
- * Lazy, pool starts off at 0 and grows as necessary.
11
- * Non-Blocking, can be configured to always return a connection if your pool runs out under load. Overflow connections are returned to the pool for reuse. Once load dies, the pool is reaped down to size.
12
- * Support for cleaning dirty resources, no one likes a dirty `HotTub`
13
- * Support for closing resources on shutdown
14
- * Support for process forking
15
-
16
-
17
- ### HotTub::Sessions
18
-
19
- A synchronized hash where keys are mapped to a HotTub::Pools that are managed by a single HotTub::Reaper.
20
-
21
-
22
- ### HotTub::Reaper
23
-
24
- A separate thread thats manages your pool(s). All HotTub::Pools managed by HotTub::Sessions share a single reaper. One-off HotTub::Pools have their own reaper. The reaper periodically checks pool(s) based on the `:reap_timeout` set for the pool or session. Over-flow connections or connections deemed reap-able ready are pulled from the pool and closed.
25
-
3
+ Flexible, thread-safe, connection pooling for Ruby. Configurable for any client you desire with built in support for Net::HTTP and [Excon](https://github.com/excon/excon).
26
4
 
27
5
  ### Requirements
28
6
 
@@ -60,51 +38,66 @@ HotTub::Sessions are used to manage multiple pools with a single object and usin
60
38
  A global Sessions object is available from the HotTub module and has several helper methods.
61
39
 
62
40
  require 'hot_tub'
63
- require 'net/http'
64
- require 'excon'
65
- require 'redis'
66
-
67
- # Add a HotTub::Pool of Net::HTTP connections to our sessions with a size of 12.
68
- # We are using the url as the key but could use anything.
69
- # we are not setting :max_size so our connections will grow to match our currency.
70
- # Once load dies down our pool will be reaped back down to 12 connections
71
-
72
- URL = "https://google.com"
73
- pool = HotTub.get_or_set(URL, { :size => 12 }) do
74
- uri = URI.parse(URL)
41
+
42
+ # Lets configure HotTub global sessions to use NetHTTP as our default client
43
+
44
+ HotTub.default_client = lambda { |url|
45
+ uri = URI.parse(url)
75
46
  http = Net::HTTP.new(uri.host, uri.port)
76
- http.use_ssl = false
77
47
  http.start
78
48
  http
49
+ }
50
+
51
+ # Add a HotTub::Pool for "https://www.google.com" and use it.
52
+ HotTub.run("https://www.google.com") do |clnt|
53
+ puts clnt.get('/').code
54
+ end
55
+
56
+ # Re-uses the previously defined pool
57
+ HotTub.run("https://www.google.com") do |clnt|
58
+ puts clnt.get('/').code
79
59
  end
80
60
 
81
- # A separate HotTub::Pool of Excon connections.
61
+ # Add another HotTub::Pool for "https://www.yahoo.com" and use it
62
+ HotTub.run("https://www.yahoo.com") do |clnt|
63
+ puts clnt.get('/').code
64
+ end
82
65
 
83
- HotTub.get_or_set('yahoo', { :size => 5 }) { Excon.new("https://yahoo.com") }
66
+ # We can add more HotTub::Pools with unique settings.
67
+ # Lets add another HotTub::Pool of Excon clients with a pool size of 12.
68
+ # HotTub.stage sets the options passed to a settings cache, the pool is
69
+ # created the first time we call HotTub.run. We are not setting :max_size
70
+ # so our connections will grow to match our currency. Once load dies down
71
+ # our pool will be reaped back down to 12 connections
72
+
73
+ HotTub.stage('excon_yahoo', { :size => 12} ) do
74
+ Excon.new("https://yahoo.com", :thread_safe_sockets => false )
75
+ end
84
76
 
85
77
  # Lets add Redis too. HotTub.add returns the pool created for that key so we
86
78
  # can store that in an constant for easy access.
87
79
  # We don't want too many connections so we set our :max_size. Under load our pool
88
80
  # can grow to 30 connections. Once load dies down our pool can be reaped back down to 5
89
81
 
90
- REDIS = HotTub.get_or_set("redis", :size => 5, :max_size => 30) { Redis.new }
82
+ REDIS = HotTub.add("redis", :size => 5, :max_size => 30) { Redis.new }
91
83
 
92
- # Now we can call any of our pools using the key we set any where in our code.
93
-
94
- HotTub.run(url) do |clnt|
95
- puts clnt.head('/').code
96
- end
84
+ # Now we can call any of our pools using the key we set.
97
85
 
98
- HotTub.run('yahoo') do |clnt|
99
- puts clnt.get(:path => "/some_stuff", :query => {:foo => 'bar'}).body
86
+ HotTub.run('excon_yahoo') do |clnt|
87
+ puts clnt.get.status
100
88
  end
101
89
 
102
- # Since our REDIS contast was set to HotTub::Pool instance return from HotTub.add
90
+ # Since our REDIS constant was set to HotTub::Pool instance return from HotTub.add
103
91
  # we do not need the key when calling #run
104
92
  REDIS.run do |clnt|
105
93
  clnt.set('hot', 'stuff')
106
94
  end
107
95
 
96
+ # Re-use "https://www.google.com" we created earlier
97
+ HotTub.run("https://www.google.com") do |clnt|
98
+ puts clnt.get('/').code
99
+ end
100
+
108
101
 
109
102
  ## Single Pool
110
103
 
@@ -123,22 +116,21 @@ A global Sessions object is available from the HotTub module and has several hel
123
116
 
124
117
  HotTub has built in support for closing NetHTTP and Excon. If you need more control or have
125
118
  a different library you would like to use, HotTub can be configured to support your needs
126
- using `:close`, `:clean`, and `:reap?` options in a pools settings.
119
+ using `:close`, `:clean`, and `:reap?` options in a pools settings, and each option can accept
120
+ a lambda that accepts the client as an argument or symbol representing a method to call on the client.
127
121
 
128
122
  `:close` is used to define how a connections should be closed at shutdown and upon reaping.
129
- reaped.
130
123
 
131
124
  `:clean` is called on each existing connection as its pulled from the pool.
132
125
 
133
126
  `:reap?` is used to determine if a connection in the pool is ready for reaping.
134
127
 
135
- EX:
136
128
  pool_options = {
137
129
  :size => 5
138
130
  :max_size => 10
139
- :close => lambda { |clnt| clnt.close_it }, # could also use :close_id symbol instead of a lambda
131
+ :close => :close
140
132
  :clean => lambda { |clnt| clnt.reconnect if clnt.dirty? },
141
- :reap? => lambda { |clnt| clnt.stail? }
133
+ :reap? => :stale? # returns truthy if we want to reap
142
134
  }
143
135
 
144
136
  HotTub.add('offBrand', pool_options) { MyCoolClient.new }
@@ -1,5 +1,5 @@
1
1
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
- require 'benchmark'
2
+ require 'benchmark/ips'
3
3
 
4
4
  puts `ruby -v`
5
5
 
@@ -29,68 +29,72 @@ class BlockTest
29
29
  end
30
30
 
31
31
 
32
- n = 1_000_000
33
- Benchmark.bmbm do |x|
32
+ Benchmark.ips do |x|
34
33
  x.report("block yield") do
35
- n.times do
36
- BlockTest.block_yield { "foo" }
37
- end
34
+ BlockTest.block_yield { "foo" }
38
35
  end
39
36
  x.report("block call") do
40
- n.times do
41
- BlockTest.block_call { "foo" }
42
- end
37
+ BlockTest.block_call { "foo" }
43
38
  end
44
39
  x.report("block to yield") do
45
- n.times do
46
- BlockTest.block_to_yield { "foo" }
47
- end
40
+ BlockTest.block_to_yield { "foo" }
48
41
  end
49
42
  x.report("block to call") do
50
- n.times do
51
- BlockTest.block_to_call { "foo" }
52
- end
43
+ BlockTest.block_to_call { "foo" }
53
44
  end
45
+ x.compare!
54
46
  end
55
47
 
56
48
  # ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
57
- # Rehearsal --------------------------------------------------
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
-
64
- # user system total real
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)
49
+ # Calculating -------------------------------------
50
+ # block yield 108.249k i/100ms
51
+ # block call 64.448k i/100ms
52
+ # block to yield 64.881k i/100ms
53
+ # block to call 60.643k i/100ms
54
+ # -------------------------------------------------
55
+ # block yield 4.655M (± 3.2%) i/s - 23.274M
56
+ # block call 1.262M (± 3.2%) i/s - 6.316M
57
+ # block to yield 1.370M (± 3.8%) i/s - 6.877M
58
+ # block to call 1.174M (± 2.1%) i/s - 5.882M
59
+
60
+ # Comparison:
61
+ # block yield: 4655424.9 i/s
62
+ # block to yield: 1369839.7 i/s - 3.40x slower
63
+ # block call: 1261742.0 i/s - 3.69x slower
64
+ # block to call: 1173741.8 i/s - 3.97x slower
69
65
 
70
66
  # rubinius 2.5.8 (2.1.0 bef51ae3 2015-07-14 3.5.1 JI) [x86_64-darwin14.4.0]
71
- # Rehearsal --------------------------------------------------
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
-
78
- # user system total real
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)
67
+ # Calculating -------------------------------------
68
+ # block yield 148.685k i/100ms
69
+ # block call 140.704k i/100ms
70
+ # block to yield 163.061k i/100ms
71
+ # block to call 126.667k i/100ms
72
+ # -------------------------------------------------
73
+ # block yield 4.162M (± 3.0%) i/s - 20.816M
74
+ # block call 1.886M (± 2.8%) i/s - 9.427M
75
+ # block to yield 2.358M (± 2.1%) i/s - 11.903M
76
+ # block to call 1.678M (± 1.8%) i/s - 8.487M
77
+
78
+ # Comparison:
79
+ # block yield: 4162250.5 i/s
80
+ # block to yield: 2357928.4 i/s - 1.77x slower
81
+ # block call: 1885832.8 i/s - 2.21x slower
82
+ # block to call: 1678439.6 i/s - 2.48x slower
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
- # Rehearsal --------------------------------------------------
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
-
92
- # user system total real
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)
85
+ # Calculating -------------------------------------
86
+ # block yield 109.774k i/100ms
87
+ # block call 114.963k i/100ms
88
+ # block to yield 125.225k i/100ms
89
+ # block to call 109.991k i/100ms
90
+ # -------------------------------------------------
91
+ # block yield 4.998M (± 6.1%) i/s - 24.919M
92
+ # block call 3.980M (± 5.4%) i/s - 19.889M
93
+ # block to yield 4.532M (± 7.4%) i/s - 22.541M
94
+ # block to call 3.771M (± 4.7%) i/s - 18.808M
95
+
96
+ # Comparison:
97
+ # block yield: 4997696.4 i/s
98
+ # block to yield: 4531931.2 i/s - 1.10x slower
99
+ # block call: 3979846.8 i/s - 1.26x slower
100
+ # block to call: 3771261.6 i/s - 1.33x slower
@@ -0,0 +1,99 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'benchmark/ips'
3
+ require 'hot_tub'
4
+ require 'connection_pool'
5
+
6
+ class MocClient;end
7
+
8
+ puts `ruby -v`
9
+
10
+ Benchmark.ips do |b|
11
+
12
+ ht_no_max = HotTub::Pool.new(:size => 5, :name => "Match Concurrency") { MocClient.new }
13
+ b.report("HotTub::Pool - *") do
14
+ threads = []
15
+ 20.times do
16
+ threads << Thread.new do
17
+ ht_no_max.run { |conn| sleep(0.01)}
18
+ end
19
+ end
20
+ threads.each do |t|
21
+ t.join
22
+ end
23
+ end
24
+
25
+ ht_sized = HotTub::Pool.new(:size => 5, :max_size => 5, :name => "Limited") { MocClient.new }
26
+ b.report("HotTub::Pool - 5") do
27
+ threads = []
28
+ 20.times do
29
+ threads << Thread.new do
30
+ ht_sized.run { |conn| sleep(0.01)}
31
+ end
32
+ end
33
+ threads.each do |t|
34
+ t.join
35
+ end
36
+ end
37
+
38
+ cp_t = ConnectionPool.new(:size => 5) { MocClient.new }
39
+ b.report("ConnectionPool - 5") do
40
+ threads = []
41
+ 20.times do
42
+ threads << Thread.new do
43
+ cp_t.with { |conn| sleep(0.01)}
44
+ end
45
+ end
46
+ threads.each do |t|
47
+ t.join
48
+ end
49
+ end
50
+
51
+ b.compare!
52
+ end
53
+
54
+ # ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
55
+ # Calculating -------------------------------------
56
+ # HotTub::Pool - * 7.000 i/100ms
57
+ # HotTub::Pool - 5 2.000 i/100ms
58
+ # ConnectionPool - 5 2.000 i/100ms
59
+ # -------------------------------------------------
60
+ # HotTub::Pool - * 80.183 (± 2.5%) i/s - 406.000
61
+ # HotTub::Pool - 5 22.229 (± 0.0%) i/s - 112.000
62
+ # ConnectionPool - 5 21.886 (± 0.0%) i/s - 110.000
63
+
64
+ # Comparison:
65
+ # HotTub::Pool - *: 80.2 i/s
66
+ # HotTub::Pool - 5: 22.2 i/s - 3.61x slower
67
+ # ConnectionPool - 5: 21.9 i/s - 3.66x slower
68
+
69
+
70
+ # 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]
71
+ # Calculating -------------------------------------
72
+ # HotTub::Pool - * 6.000 i/100ms
73
+ # HotTub::Pool - 5 2.000 i/100ms
74
+ # ConnectionPool - 5 2.000 i/100ms
75
+ # -------------------------------------------------
76
+ # HotTub::Pool - * 69.232 (± 8.7%) i/s - 342.000
77
+ # HotTub::Pool - 5 22.064 (± 4.5%) i/s - 112.000
78
+ # ConnectionPool - 5 22.036 (± 0.0%) i/s - 112.000
79
+
80
+ # Comparison:
81
+ # HotTub::Pool - *: 69.2 i/s
82
+ # HotTub::Pool - 5: 22.1 i/s - 3.14x slower
83
+ # ConnectionPool - 5: 22.0 i/s - 3.14x slower
84
+
85
+
86
+ # rubinius 2.5.8 (2.1.0 bef51ae3 2015-07-14 3.5.1 JI) [x86_64-darwin14.4.0]
87
+ # Calculating -------------------------------------
88
+ # HotTub::Pool - * 8.000 i/100ms
89
+ # HotTub::Pool - 5 2.000 i/100ms
90
+ # ConnectionPool - 5 2.000 i/100ms
91
+ # -------------------------------------------------
92
+ # HotTub::Pool - * 84.235 (± 3.6%) i/s - 424.000
93
+ # HotTub::Pool - 5 22.255 (± 0.0%) i/s - 112.000
94
+ # ConnectionPool - 5 22.259 (± 0.0%) i/s - 112.000
95
+
96
+ # Comparison:
97
+ # HotTub::Pool - *: 84.2 i/s
98
+ # ConnectionPool - 5: 22.3 i/s - 3.78x slower
99
+ # HotTub::Pool - 5: 22.3 i/s - 3.78x slower
@@ -0,0 +1,60 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ require 'benchmark/ips'
3
+ require 'hot_tub'
4
+ require 'connection_pool'
5
+
6
+ class MocClient;end
7
+
8
+ puts `ruby -v`
9
+
10
+ Benchmark.ips do |b|
11
+
12
+ ht_1 = HotTub::Pool.new(:size => 1) { MocClient.new }
13
+ b.report("HotTub::Pool") do
14
+ ht_1.run { |conn| }
15
+ end
16
+
17
+ cp_1 = ConnectionPool.new(:size => 1) { MocClient.new }
18
+ b.report("ConnectionPool") do
19
+ cp_1.with { |conn| }
20
+ end
21
+ b.compare!
22
+ end
23
+
24
+ # ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin14]
25
+ # Calculating -------------------------------------
26
+ # HotTub::Pool 26.909k i/100ms
27
+ # ConnectionPool 10.413k i/100ms
28
+ # -------------------------------------------------
29
+ # HotTub::Pool 351.352k (± 1.9%) i/s - 1.776M
30
+ # ConnectionPool 122.342k (± 1.2%) i/s - 614.367k
31
+
32
+ # Comparison:
33
+ # HotTub::Pool: 351352.4 i/s
34
+ # ConnectionPool: 122341.6 i/s - 2.87x slower
35
+
36
+
37
+ # 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]
38
+ # Calculating -------------------------------------
39
+ # HotTub::Pool 16.145k i/100ms
40
+ # ConnectionPool 6.260k i/100ms
41
+ # -------------------------------------------------
42
+ # HotTub::Pool 304.280k (± 3.7%) i/s - 1.534M
43
+ # ConnectionPool 106.788k (± 2.6%) i/s - 538.360k
44
+
45
+ # Comparison:
46
+ # HotTub::Pool: 304279.7 i/s
47
+ # ConnectionPool: 106788.5 i/s - 2.85x slower
48
+
49
+
50
+ # rubinius 2.5.8 (2.1.0 bef51ae3 2015-07-14 3.5.1 JI) [x86_64-darwin14.4.0]
51
+ # Calculating -------------------------------------
52
+ # HotTub::Pool 12.259k i/100ms
53
+ # ConnectionPool 8.771k i/100ms
54
+ # -------------------------------------------------
55
+ # HotTub::Pool 415.132k (± 2.9%) i/s - 2.072M
56
+ # ConnectionPool 172.584k (± 1.7%) i/s - 868.329k
57
+
58
+ # Comparison:
59
+ # HotTub::Pool: 415131.6 i/s
60
+ # ConnectionPool: 172584.0 i/s - 2.41x slower