Skip to content

Commit fed3dcd

Browse files
committed
Add ability to configure default settings for new connections
1 parent ab525c9 commit fed3dcd

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

lib/net/http.rb

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,29 +1091,61 @@ def HTTP.new(address, port = nil, p_addr = :ENV, p_port = nil, p_user = nil, p_p
10911091
http
10921092
end
10931093

1094+
class << HTTP
1095+
# Allows to set the default configuration that will be used
1096+
# when creating a new connection.
1097+
#
1098+
# Example:
1099+
#
1100+
# Net::HTTP.default_configuration = {
1101+
# read_timeout: 1,
1102+
# write_timeout: 1
1103+
# }
1104+
# http = Net::HTTP.new(hostname)
1105+
# http.open_timeout # => 60
1106+
# http.read_timeout # => 1
1107+
# http.write_timeout # => 1
1108+
#
1109+
attr_accessor :default_configuration
1110+
end
1111+
10941112
# Creates a new \Net::HTTP object for the specified server address,
10951113
# without opening the TCP connection or initializing the \HTTP session.
10961114
# The +address+ should be a DNS hostname or IP address.
10971115
def initialize(address, port = nil) # :nodoc:
1116+
defaults = {
1117+
keep_alive_timeout: 2,
1118+
close_on_empty_response: false,
1119+
open_timeout: 60,
1120+
read_timeout: 60,
1121+
write_timeout: 60,
1122+
continue_timeout: nil,
1123+
max_retries: 1,
1124+
debug_output: nil,
1125+
response_body_encoding: false,
1126+
ignore_eof: true
1127+
}
1128+
options = defaults.merge(self.class.default_configuration || {})
1129+
10981130
@address = address
10991131
@port = (port || HTTP.default_port)
11001132
@ipaddr = nil
11011133
@local_host = nil
11021134
@local_port = nil
11031135
@curr_http_version = HTTPVersion
1104-
@keep_alive_timeout = 2
1136+
@keep_alive_timeout = options[:keep_alive_timeout]
11051137
@last_communicated = nil
1106-
@close_on_empty_response = false
1138+
@close_on_empty_response = options[:close_on_empty_response]
11071139
@socket = nil
11081140
@started = false
1109-
@open_timeout = 60
1110-
@read_timeout = 60
1111-
@write_timeout = 60
1112-
@continue_timeout = nil
1113-
@max_retries = 1
1114-
@debug_output = nil
1115-
@response_body_encoding = false
1116-
@ignore_eof = true
1141+
@open_timeout = options[:open_timeout]
1142+
@read_timeout = options[:read_timeout]
1143+
@write_timeout = options[:write_timeout]
1144+
@continue_timeout = options[:continue_timeout]
1145+
@max_retries = options[:max_retries]
1146+
@debug_output = options[:debug_output]
1147+
@response_body_encoding = options[:response_body_encoding]
1148+
@ignore_eof = options[:ignore_eof]
11171149

11181150
@proxy_from_env = false
11191151
@proxy_uri = nil

test/net/http/test_http.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,18 @@ def host.to_str; raise SocketError, "open failure"; end
254254
end
255255
end
256256

257+
def test_default_configuration
258+
Net::HTTP.default_configuration = { open_timeout: 5 }
259+
http = Net::HTTP.new 'hostname.example'
260+
assert_equal 5, http.open_timeout
261+
assert_equal 60, http.read_timeout
262+
263+
http.open_timeout = 10
264+
assert_equal 10, http.open_timeout
265+
ensure
266+
Net::HTTP.default_configuration = nil
267+
end
268+
257269
end
258270

259271
module TestNetHTTP_version_1_1_methods

0 commit comments

Comments
 (0)