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

Handle empty response body case #112

Merged
merged 4 commits into from Sep 15, 2017
Merged

Conversation

nurse
Copy link
Collaborator

@nurse nurse commented Sep 14, 2017

No description provided.

If HTTP response body is empty, HTTPClient doesn't yield given block,
and its response object's body attribute is nil.
@nurse nurse requested a review from nahi September 14, 2017 10:09
@coveralls
Copy link

coveralls commented Sep 14, 2017

Coverage Status

Coverage decreased (-0.04%) to 77.454% when pulling 708557d on handle-empty-response-body-case into e21f4d4 on master.

@nurse nurse requested a review from uu59 September 14, 2017 21:50
end
status = response.code
# retry if the HTTP error code is 500 or higher and we did not run out of retrying attempts
if !block_given? && status >= 500 && cumul_retry_delay < @max_cumul_retry_delay
Copy link
Contributor

Choose a reason for hiding this comment

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

This is in else for if block, so !block_given? is always true. We don't need to check it.

sleep retry_delay
cumul_retry_delay += retry_delay
retry_delay *= 2
redo # restart from beginning of do-while loop
Copy link
Contributor

Choose a reason for hiding this comment

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

Original code had redo out of if-else. Now it only exists in else.
Thus don't retry if block given and response code is 5xx. Is this behavior change intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Original code also had redo inside if !block_given?, isn't it?

Why not retry if block given is because if block given the block may be not idempotent.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I meant if-else for if block ... else ...

if block
current_total_chunk_size = 0
response = client.get(target, params, header) {|res, chunk|
current_total_chunk_size += chunk.bytesize
block.call(res, chunk, current_total_chunk_size)
}
# XXX ext/openssl raises EOFError in case where underlying connection causes an error,
# and msgpack-ruby that used in block handles it as an end of stream == no exception.
# Therefor, check content size.
validate_content_length!(response, current_total_chunk_size) if @ssl
else
response = client.get(target, params, header)
validate_content_length!(response, response.body.bytesize) if @ssl
end
status = response.code
# retry if the HTTP error code is 500 or higher and we did not run out of retrying attempts
if !block_given? && status >= 500 && cumul_retry_delay < @max_cumul_retry_delay
$stderr.puts "Error #{status}: #{get_error(response)}. Retrying after #{retry_delay} seconds..."
sleep retry_delay
cumul_retry_delay += retry_delay
retry_delay *= 2
redo # restart from beginning of do-while loop
end

if block
  ...
else
  ...
end

if !block_given? && ..
  ...
  redo
end

changed to

if block
  ...
else
  ...
  if !block_given? && ...
    ...
    redo
  end
end

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah nvm, sorry I got wrong about it. Behavior didn't changed

current_total_chunk_size += chunk.bytesize
chunk = infl.inflate(chunk) if infl
yield chunk, current_total_chunk_size
end
validate_response_status(response)
Copy link
Contributor

Choose a reason for hiding this comment

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

This line is intended for empty response body, so client.get(...) do ... end block wasn't called but response is assigned, right?
I need some comments for that situation because looks like just validate_response_status is called twice.
Also I think validate_response_status(response, 0) if response.body.nil? would be more explicit.

@nurse
Copy link
Collaborator Author

nurse commented Sep 15, 2017

@uu59 fixed!

@coveralls
Copy link

coveralls commented Sep 15, 2017

Coverage Status

Coverage decreased (-0.04%) to 77.454% when pulling e355ba1 on handle-empty-response-body-case into e21f4d4 on master.

Copy link
Contributor

@uu59 uu59 left a comment

Choose a reason for hiding this comment

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

👍

@nurse nurse merged commit 580abd8 into master Sep 15, 2017
@nurse nurse deleted the handle-empty-response-body-case branch September 15, 2017 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants