Skip to content

Commit 5d4b76c

Browse files
committed
Make pool size configurable
1 parent 02aab6d commit 5d4b76c

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

lib/net/http/persistent.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,11 @@ class Net::HTTP::Persistent
200200

201201
HAVE_OPENSSL = defined? OpenSSL::SSL # :nodoc:
202202

203+
##
204+
# The default connection pool size is 1/4 the allowed open files.
205+
206+
DEFAULT_POOL_SIZE = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
207+
203208
##
204209
# The version of Net::HTTP::Persistent you are using
205210

@@ -499,8 +504,12 @@ def self.detect_idle_timeout uri, max = 10
499504
# proxy = URI 'http://proxy.example'
500505
# proxy.user = 'AzureDiamond'
501506
# proxy.password = 'hunter2'
507+
#
508+
# Set +pool_size+ to limit the maximum number of connections allowed.
509+
# Defaults to 1/4 the number of allowed file handles. You can have no more
510+
# than this many threads with active HTTP transactions.
502511

503-
def initialize name = nil, proxy = nil
512+
def initialize name: nil, proxy: nil, pool_size: DEFAULT_POOL_SIZE
504513
@name = name
505514

506515
@debug_output = nil
@@ -525,8 +534,7 @@ def initialize name = nil, proxy = nil
525534
@ssl_generation_key = [key, 'ssl_generations'].join('_').intern
526535
@timeout_key = [key, 'timeouts' ].join('_').intern
527536

528-
pool_size = Process.getrlimit(Process::RLIMIT_NOFILE).first / 4
529-
@pool = Net::HTTP::Persistent::Pool.new size: pool_size do |http_args|
537+
@pool = Net::HTTP::Persistent::Pool.new size: pool_size do |http_args|
530538
Net::HTTP::Persistent::Connection.new Net::HTTP, http_args, @ssl_generation
531539
end
532540

0 commit comments

Comments
 (0)