Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make keep_alive_timeout configurable #405

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We officially support ruby 2.0+, so it's better to use kwargs in here:

def persistent(host, timeout: nil)
  # ...

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this duplicates line 3

# 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