From 8bb2a6cc0b4578637263dc3bafa18289393faa04 Mon Sep 17 00:00:00 2001 From: Doug Renn Date: Wed, 12 Apr 2017 16:08:05 -0600 Subject: [PATCH] Make keep_alive_timeout configurable Allow keep_alive_timeout to be set in persistent: HTTP.persistent("http...", timeout: 120) --- lib/http/chainable.rb | 15 +++++++++------ spec/lib/http_spec.rb | 10 +++++++++- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/http/chainable.rb b/lib/http/chainable.rb index a7858f1a..306a05d3 100644 --- a/lib/http/chainable.rb +++ b/lib/http/chainable.rb @@ -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. # @@ -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 diff --git a/spec/lib/http_spec.rb b/spec/lib/http_spec.rb index bb3c5314..6f41b55f 100644 --- a/spec/lib/http_spec.rb +++ b/spec/lib/http_spec.rb @@ -1,5 +1,5 @@ +# coding: utf-8 # frozen_string_literal: true -# encoding: utf-8 require "json" @@ -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