Skip to content

Commit

Permalink
Make keep_alive_timeout configurable
Browse files Browse the repository at this point in the history
Allow keep_alive_timeout to be set in persistent:

HTTP.persistent("http...", timeout: 120)
  • Loading branch information
nestegg committed Apr 13, 2017
1 parent 5bbaecc commit 8bb2a6c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 9 additions & 6 deletions lib/http/chainable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,13 @@ def timeout(klass, options = {}) # rubocop:disable Style/OptionHash
)
end

# @overload persistent(host)
# @overload persistent(host, timeout: nil)
# Flags as persistent
# @param [String] host
# @raise [Request::Error] if Host is invalid
# @param [String] host
# @option [Integer] timeout Keep alive timeout
# @raise [Request::Error] if Host is invalid
# @return [HTTP::Client] Persistent client
# @overload persistent(host, &block)
# @overload persistent(host, timeout: nil, &block)
# Executes given block with persistent client and automatically closes
# connection at the end of execution.
#
Expand All @@ -138,8 +139,10 @@ def timeout(klass, options = {}) # rubocop:disable Style/OptionHash
#
# @yieldparam [HTTP::Client] client Persistent client
# @return [Object] result of last expression in the block
def persistent(host)
p_client = branch default_options.with_persistent host
def persistent(host, timeout: nil)
opts = {}
opts[:keep_alive_timeout] = timeout if timeout
p_client = branch default_options.merge(opts).with_persistent host
return p_client unless block_given?
yield p_client
ensure
Expand Down
10 changes: 9 additions & 1 deletion spec/lib/http_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# coding: utf-8
# frozen_string_literal: true
# encoding: utf-8

require "json"

Expand Down Expand Up @@ -288,6 +288,14 @@
end
end
end

context "with timeout specified" do
subject(:client) { HTTP.persistent host, :timeout => 100 }
it "sets keep_alive_timeout" do
options = client.default_options
expect(options.keep_alive_timeout).to eq(100)
end
end
end

describe ".timeout" do
Expand Down

0 comments on commit 8bb2a6c

Please sign in to comment.