Skip to content

Commit

Permalink
calculate http error_timeout based upon capacity option
Browse files Browse the repository at this point in the history
  • Loading branch information
jpittis committed May 25, 2017
1 parent 023e0c1 commit 8b7a552
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/semian/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,16 @@ 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?
calculate_error_timeout if @raw_semian_options[:capacity] && !@raw_semian_options[:error_timeout]
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
Expand Down
23 changes: 23 additions & 0 deletions test/net_http_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
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
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)
Expand Down

0 comments on commit 8b7a552

Please sign in to comment.