Skip to content

Commit 8bb2a6c

Browse files
committed
Make keep_alive_timeout configurable
Allow keep_alive_timeout to be set in persistent: HTTP.persistent("http...", timeout: 120)
1 parent 5bbaecc commit 8bb2a6c

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/http/chainable.rb

+9-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: nil)
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: nil, &block)
118119
# Executes given block with persistent client and automatically closes
119120
# connection at the end of execution.
120121
#
@@ -138,8 +139,10 @@ 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: nil)
143+
opts = {}
144+
opts[:keep_alive_timeout] = timeout if timeout
145+
p_client = branch default_options.merge(opts).with_persistent host
143146
return p_client unless block_given?
144147
yield p_client
145148
ensure

spec/lib/http_spec.rb

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
# coding: utf-8
12
# frozen_string_literal: true
2-
# encoding: utf-8
33

44
require "json"
55

@@ -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)