diff --git a/lib/semian/net_http.rb b/lib/semian/net_http.rb index 7121796d..a7be75b6 100644 --- a/lib/semian/net_http.rb +++ b/lib/semian/net_http.rb @@ -75,10 +75,19 @@ def reset_exceptions def raw_semian_options @raw_semian_options ||= begin @raw_semian_options = Semian::NetHTTP.retrieve_semian_configuration(address, port) - @raw_semian_options = @raw_semian_options.dup unless @raw_semian_options.nil? + unless @raw_semian_options.nil? + @raw_semian_options = @raw_semian_options.dup + calculate_error_timeout if @raw_semian_options[:capacity] && !@raw_semian_options[:error_timeout] + end end end + def calculate_error_timeout + capacity = @raw_semian_options[:capacity] + error_timeout = (read_timeout * capacity) / (1 - capacity) + @raw_semian_options.merge!({ error_timeout: error_timeout}) + end + def resource_exceptions Semian::NetHTTP.exceptions end diff --git a/test/net_http_test.rb b/test/net_http_test.rb index b2e3a66c..9eec402f 100644 --- a/test/net_http_test.rb +++ b/test/net_http_test.rb @@ -353,6 +353,29 @@ def test_persistent_state_after_server_restart end end + def test_capacity_option_calculates_the_error_timeout_based_upon_read_timeout + options_with_capacity = DEFAULT_SEMIAN_OPTIONS.dup.merge!({ capacity: 0.75 }) + options_with_capacity.delete(:error_timeout) + semian_configuration_proc = proc do |host, port| + options_with_capacity.merge(name: "#{host}_#{port}") + end + with_semian_configuration(semian_configuration_proc) do + http = Net::HTTP.new(HOSTNAME, TOXIC_PORT) + assert_equal 180, http.raw_semian_options[:error_timeout] + end + end + + def test_capacity_option_defaults_to_error_timeout_when_both_are_provided + options_with_capacity = DEFAULT_SEMIAN_OPTIONS.dup.merge!({ capacity: 0.75 }) + semian_configuration_proc = proc do |host, port| + options_with_capacity.merge(name: "#{host}_#{port}") + end + with_semian_configuration(semian_configuration_proc) do + http = Net::HTTP.new(HOSTNAME, TOXIC_PORT) + assert_equal 10, http.raw_semian_options[:error_timeout] + end + end + private def with_semian_configuration(options = DEFAULT_SEMIAN_CONFIGURATION)