Skip to content

Commit 4738d22

Browse files
nesteggixti
authored andcommitted
Make keep_alive_timeout configurable
Allow keep_alive_timeout to be set in persistent: HTTP.persistent("http...", timeout: 120)
1 parent 5bbaecc commit 4738d22

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

lib/http/chainable.rb

+8-6
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,13 @@ def timeout(klass, options = {}) # rubocop:disable Style/OptionHash
109109
)
110110
end
111111

112-
# @overload persistent(host)
112+
# @overload persistent(host, timeout: 5)
113113
# Flags as persistent
114-
# @param [String] host
115-
# @raise [Request::Error] if Host is invalid
114+
# @param [String] host
115+
# @option [Integer] timeout Keep alive timeout
116+
# @raise [Request::Error] if Host is invalid
116117
# @return [HTTP::Client] Persistent client
117-
# @overload persistent(host, &block)
118+
# @overload persistent(host, timeout: 5, &block)
118119
# Executes given block with persistent client and automatically closes
119120
# connection at the end of execution.
120121
#
@@ -138,8 +139,9 @@ def timeout(klass, options = {}) # rubocop:disable Style/OptionHash
138139
#
139140
# @yieldparam [HTTP::Client] client Persistent client
140141
# @return [Object] result of last expression in the block
141-
def persistent(host)
142-
p_client = branch default_options.with_persistent host
142+
def persistent(host, timeout: 5)
143+
options = {:keep_alive_timeout => timeout}
144+
p_client = branch default_options.merge(options).with_persistent host
143145
return p_client unless block_given?
144146
yield p_client
145147
ensure

spec/lib/http_spec.rb

+8
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,14 @@
288288
end
289289
end
290290
end
291+
292+
context "with timeout specified" do
293+
subject(:client) { HTTP.persistent host, :timeout => 100 }
294+
it "sets keep_alive_timeout" do
295+
options = client.default_options
296+
expect(options.keep_alive_timeout).to eq(100)
297+
end
298+
end
291299
end
292300

293301
describe ".timeout" do

0 commit comments

Comments
 (0)