Skip to content

Commit 3aa97e3

Browse files
authored
Merge branch 'master' into test_fix
2 parents df177a6 + 4cb8246 commit 3aa97e3

File tree

9 files changed

+129
-28
lines changed

9 files changed

+129
-28
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
/TAGS
44
/doc
55
/pkg
6+
.idea
7+
/Gemfile.lock

.travis.yml

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
after_script:
33
- rake travis:after -t
44
before_script:
5-
- gem install hoe-travis --no-rdoc --no-ri
5+
- gem install hoe-travis --no-document
66
- rake travis:before -t
77
language: ruby
88
notifications:
99
email:
1010
1111
rvm:
12-
- 2.1.10
13-
- 2.2.5
14-
- 2.3.1
12+
- 2.1
13+
- 2.2
14+
- 2.3
15+
- 2.4
16+
- 2.5
17+
- 2.6
1518
script: rake travis
19+
install: "" # avoid running default bundler install
20+
21+
matrix:
22+
include:
23+
- rvm: "2.6"
24+
env: TRAVIS_MATRIX=pipeline

Gemfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# -*- ruby -*-
2+
3+
# DO NOT EDIT THIS FILE. Instead, edit Rakefile, and run `rake bundler:gemfile`.
4+
5+
source "https://rubygems.org/"
6+
7+
gem "connection_pool", "~>2.2"
8+
9+
gem "minitest", "~>5.11", :group => [:development, :test]
10+
gem "hoe-bundler", "~>1.5", :group => [:development, :test]
11+
gem "hoe-travis", "~>1.4", ">=1.4.1", :group => [:development, :test]
12+
gem "rdoc", ">=4.0", "<7", :group => [:development, :test]
13+
gem "hoe", "~>3.17", :group => [:development, :test]
14+
15+
# vim: syntax=ruby

Manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.autotest
22
.gemtest
33
.travis.yml
4+
Gemfile
45
History.txt
56
Manifest.txt
67
README.rdoc

README.rdoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ connection is kept alive between requests:
3232

3333
uri = URI 'http://example.com/awesome/web/service'
3434

35-
http = Net::HTTP::Persistent.new 'my_app_name'
35+
http = Net::HTTP::Persistent.new name: 'my_app_name'
3636

3737
# perform a GET
3838
response = http.request uri

Rakefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# -*- ruby -*-
22

3-
require 'rubygems'
43
require 'hoe'
54

5+
Hoe.plugin :bundler
66
Hoe.plugin :git
77
Hoe.plugin :minitest
88
Hoe.plugin :travis
@@ -22,7 +22,10 @@ Hoe.spec 'net-http-persistent' do
2222

2323
dependency 'connection_pool', '~> 2.2'
2424
dependency 'minitest', '~> 5.2', :development
25-
dependency 'net-http-pipeline', '~> 1.0', :development
25+
dependency 'hoe-bundler', '~> 1.5', :development
26+
dependency 'hoe-travis', ['~> 1.4', '>= 1.4.1'], :development
27+
dependency 'net-http-pipeline', '~> 1.0' if
28+
ENV['TRAVIS_MATRIX'] == 'pipeline'
2629
end
2730

2831
# vim: syntax=Ruby

lib/net/http/persistent.rb

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#
3434
# uri = URI 'http://example.com/awesome/web/service'
3535
#
36-
# http = Net::HTTP::Persistent.new 'my_app_name'
36+
# http = Net::HTTP::Persistent.new name: 'my_app_name'
3737
#
3838
# # perform a GET
3939
# response = http.request uri
@@ -153,7 +153,7 @@
153153
# uri = URI 'http://example.com/awesome/web/service'
154154
# post_uri = uri + 'create'
155155
#
156-
# http = Net::HTTP::Persistent.new 'my_app_name'
156+
# http = Net::HTTP::Persistent.new name: 'my_app_name'
157157
#
158158
# post = Net::HTTP::Post.new post_uri.path
159159
# # ... fill in POST request
@@ -202,7 +202,11 @@ class Net::HTTP::Persistent
202202
##
203203
# The default connection pool size is 1/4 the allowed open files.
204204

205-
DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
205+
if Gem.win_platform? then
206+
DEFAULT_POOL_SIZE = 256
207+
else
208+
DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
209+
end
206210

207211
##
208212
# The version of Net::HTTP::Persistent you are using
@@ -406,6 +410,11 @@ def self.detect_idle_timeout uri, max = 10
406410

407411
attr_accessor :read_timeout
408412

413+
##
414+
# Seconds to wait until writing one block. See Net::HTTP#write_timeout
415+
416+
attr_accessor :write_timeout
417+
409418
##
410419
# By default SSL sessions are reused to avoid extra SSL handshakes. Set
411420
# this to false if you have problems communicating with an HTTPS server
@@ -440,10 +449,26 @@ def self.detect_idle_timeout uri, max = 10
440449
# SSL version to use.
441450
#
442451
# By default, the version will be negotiated automatically between client
443-
# and server. Ruby 1.9 and newer only.
452+
# and server. Ruby 1.9 and newer only. Deprecated since Ruby 2.5.
444453

445454
attr_reader :ssl_version
446455

456+
##
457+
# Minimum SSL version to use, e.g. :TLS1_1
458+
#
459+
# By default, the version will be negotiated automatically between client
460+
# and server. Ruby 2.5 and newer only.
461+
462+
attr_reader :min_version
463+
464+
##
465+
# Maximum SSL version to use, e.g. :TLS1_2
466+
#
467+
# By default, the version will be negotiated automatically between client
468+
# and server. Ruby 2.5 and newer only.
469+
470+
attr_reader :max_version
471+
447472
##
448473
# Where this instance's last-use times live in the thread local variables
449474

@@ -514,6 +539,7 @@ def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
514539
@keep_alive = 30
515540
@open_timeout = nil
516541
@read_timeout = nil
542+
@write_timeout = nil
517543
@idle_timeout = 5
518544
@max_requests = nil
519545
@socket_options = []
@@ -533,6 +559,8 @@ def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
533559
@private_key = nil
534560
@ssl_timeout = nil
535561
@ssl_version = nil
562+
@min_version = nil
563+
@max_version = nil
536564
@verify_callback = nil
537565
@verify_depth = nil
538566
@verify_mode = nil
@@ -607,8 +635,11 @@ def connection_for uri
607635

608636
net_http_args = [uri.host, uri.port]
609637

610-
net_http_args.concat @proxy_args if
611-
@proxy_uri and not proxy_bypass? uri.host, uri.port
638+
if @proxy_uri and not proxy_bypass? uri.host, uri.port then
639+
net_http_args.concat @proxy_args
640+
else
641+
net_http_args.concat [nil, nil, nil, nil]
642+
end
612643

613644
connection = @pool.checkout net_http_args
614645

@@ -625,6 +656,7 @@ def connection_for uri
625656
end
626657

627658
http.read_timeout = @read_timeout if @read_timeout
659+
http.write_timeout = @write_timeout if @write_timeout && http.respond_to?(:write_timeout=)
628660
http.keep_alive_timeout = @idle_timeout if @idle_timeout
629661

630662
return yield connection
@@ -744,7 +776,7 @@ def normalize_uri uri
744776

745777
##
746778
# Pipelines +requests+ to the HTTP server at +uri+ yielding responses if a
747-
# block is given. Returns all responses recieved.
779+
# block is given. Returns all responses received.
748780
#
749781
# See
750782
# Net::HTTP::Pipeline[http://docs.seattlerb.org/net-http-pipeline/Net/HTTP/Pipeline.html]
@@ -1030,9 +1062,7 @@ def request_setup req_or_uri # :nodoc:
10301062
# #shutdown when you are completely done making requests!
10311063

10321064
def shutdown
1033-
@pool.available.shutdown do |http|
1034-
http.finish
1035-
end
1065+
@pool.shutdown { |http| http.finish }
10361066
end
10371067

10381068
##
@@ -1044,6 +1074,8 @@ def ssl connection
10441074
connection.ciphers = @ciphers if @ciphers
10451075
connection.ssl_timeout = @ssl_timeout if @ssl_timeout
10461076
connection.ssl_version = @ssl_version if @ssl_version
1077+
connection.min_version = @min_version if @min_version
1078+
connection.max_version = @max_version if @max_version
10471079

10481080
connection.verify_depth = @verify_depth
10491081
connection.verify_mode = @verify_mode
@@ -1115,6 +1147,24 @@ def ssl_version= ssl_version
11151147
reconnect_ssl
11161148
end
11171149

1150+
##
1151+
# Minimum SSL version to use
1152+
1153+
def min_version= min_version
1154+
@min_version = min_version
1155+
1156+
reconnect_ssl
1157+
end
1158+
1159+
##
1160+
# maximum SSL version to use
1161+
1162+
def max_version= max_version
1163+
@max_version = max_version
1164+
1165+
reconnect_ssl
1166+
end
1167+
11181168
##
11191169
# Sets the depth of SSL certificate verification
11201170

lib/net/http/persistent/pool.rb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def initialize(options = {}, &block)
77
super
88

99
@available = Net::HTTP::Persistent::TimedStackMulti.new(@size, &block)
10-
@key = :"current-#{@available.object_id}"
10+
@key = "current-#{@available.object_id}"
1111
end
1212

1313
def checkin net_http_args
14-
stack = Thread.current[@key][net_http_args]
14+
stack = Thread.current[@key][net_http_args] ||= []
1515

1616
raise ConnectionPool::Error, 'no connections are checked out' if
1717
stack.empty?
@@ -20,14 +20,17 @@ def checkin net_http_args
2020

2121
if stack.empty?
2222
@available.push conn, connection_args: net_http_args
23+
24+
Thread.current[@key].delete(net_http_args)
25+
Thread.current[@key] = nil if Thread.current[@key].empty?
2326
end
2427

2528
nil
2629
end
2730

2831
def checkout net_http_args
29-
stacks = Thread.current[@key] ||= Hash.new { |h, k| h[k] = [] }
30-
stack = stacks[net_http_args]
32+
stacks = Thread.current[@key] ||= {}
33+
stack = stacks[net_http_args] ||= []
3134

3235
if stack.empty? then
3336
conn = @available.pop connection_args: net_http_args
@@ -40,6 +43,10 @@ def checkout net_http_args
4043
conn
4144
end
4245

46+
def shutdown
47+
Thread.current[@key] = nil
48+
super
49+
end
4350
end
4451

4552
require 'net/http/persistent/timed_stack_multi'

test/test_net_http_persistent.rb

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ def teardown
7777
class BasicConnection
7878
attr_accessor :started, :finished, :address, :port, :use_ssl,
7979
:read_timeout, :open_timeout, :keep_alive_timeout
80-
attr_accessor :ciphers, :ssl_timeout, :ssl_version,
81-
:verify_depth, :verify_mode, :cert_store,
80+
attr_accessor :ciphers, :ssl_timeout, :ssl_version, :min_version,
81+
:max_version, :verify_depth, :verify_mode, :cert_store,
8282
:ca_file, :ca_path, :cert, :key
8383
attr_reader :req, :debug_output
8484
def initialize
@@ -120,7 +120,7 @@ def proxy_port
120120
def basic_connection
121121
raise "#{@uri} is not HTTP" unless @uri.scheme.downcase == 'http'
122122

123-
net_http_args = [@uri.host, @uri.port]
123+
net_http_args = [@uri.host, @uri.port, nil, nil, nil, nil]
124124

125125
connection = Net::HTTP::Persistent::Connection.allocate
126126
connection.ssl_generation = @http.ssl_generation
@@ -152,7 +152,7 @@ def r.read_body() :read_body end
152152
def ssl_connection
153153
raise "#{@uri} is not HTTPS" unless @uri.scheme.downcase == 'https'
154154

155-
net_http_args = [@uri.host, @uri.port]
155+
net_http_args = [@uri.host, @uri.port, nil, nil, nil, nil]
156156

157157
connection = Net::HTTP::Persistent::Connection.allocate
158158
connection.ssl_generation = @http.ssl_generation
@@ -279,7 +279,7 @@ def test_connection_for
279279
c
280280
end
281281

282-
stored = @http.pool.checkout ['example.com', 80]
282+
stored = @http.pool.checkout ['example.com', 80, nil, nil, nil, nil]
283283

284284
assert_same used, stored
285285
end
@@ -786,7 +786,7 @@ def test_proxy_equals_env
786786
def test_proxy_equals_nil
787787
@http.proxy = nil
788788

789-
assert_equal nil, @http.proxy_uri
789+
assert_nil @http.proxy_uri
790790

791791
assert_equal 1, @http.generation, 'generation'
792792
assert_equal 1, @http.ssl_generation, 'ssl_generation'
@@ -1094,7 +1094,7 @@ def test_request_connection_close_request
10941094
assert_kind_of Net::HTTP::Get, req
10951095
assert_equal '/path', req.path
10961096
assert_equal 'close', req['connection']
1097-
assert_equal nil, req['keep-alive']
1097+
assert_nil req['keep-alive']
10981098

10991099
assert c.http.finished?
11001100
end
@@ -1527,6 +1527,20 @@ def test_ssl_version_equals
15271527
assert_equal 1, @http.ssl_generation
15281528
end
15291529

1530+
def test_min_version_equals
1531+
@http.min_version = :min_version
1532+
1533+
assert_equal :min_version, @http.min_version
1534+
assert_equal 1, @http.ssl_generation
1535+
end
1536+
1537+
def test_max_version_equals
1538+
@http.max_version = :max_version
1539+
1540+
assert_equal :max_version, @http.max_version
1541+
assert_equal 1, @http.ssl_generation
1542+
end
1543+
15301544
def test_start
15311545
c = basic_connection
15321546
c = c.http

0 commit comments

Comments
 (0)