Skip to content

Allow Response::Body#each to take a buffer_size - #856

Open
hikmetba-bit wants to merge 1 commit into
httprb:mainfrom
hikmetba-bit:feature/response-body-each-buffer-size
Open

hikmetba-bit wants to merge 1 commit into
httprb:mainfrom
hikmetba-bit:feature/response-body-each-buffer-size

Conversation

@hikmetba-bit

Copy link
Copy Markdown

Summary

Fixes #782.

Body#each always read in HTTP::Connection::BUFFER_SIZE (16KB) chunks with no way to override it, which meant streaming large responses (e.g. multi-gigabyte files via ActionController::Live) required many more reads than necessary.

Change

Adds an optional buffer_size: keyword to #each, defaulting to the existing Connection::BUFFER_SIZE so behavior is unchanged unless a caller opts in — per the implementation @ixti sketched directly in the issue:

def each(buffer_size: Connection::BUFFER_SIZE)
  while (chunk = readpartial(buffer_size))
    yield chunk
  end
end

(adapted slightly to the current readpartial(*)-forwarding signature and loop/rescue EOFError control flow already in this codebase).

Also updated the RBS signature in sig/http.rbs for Response::Body#each.

Testing

  • Added two regression tests: one confirming buffer_size: is forwarded to the underlying readpartial, one confirming the default is still Connection::BUFFER_SIZE when no buffer_size: is given.
  • Verified both new tests fail against the code without this fix (ArgumentError: wrong number of arguments / default nil instead of 16384), and pass with it.
  • Ran all test/http/response/** and body_test.rb files: 502 runs, 0 failures, 0 errors.
  • bundle exec steep check: no new type errors (only 4 pre-existing, unrelated Redundant ignore comment warnings in other files).
  • bundle exec rubocop on the changed Ruby files: no offenses.

🤖 Generated with Claude Code

Body#each always read in HTTP::Connection::BUFFER_SIZE (16KB) chunks
with no way to override it, which meant streaming large responses
(e.g. multi-gigabyte files via ActionController::Live) required many
more reads than necessary.

Add an optional buffer_size: keyword to #each, defaulting to the
existing Connection::BUFFER_SIZE so behavior is unchanged unless a
caller opts in, per the implementation @ixti sketched in httprb#782.

Fixes httprb#782.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
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.

HTTP::Response::Body#each and BUFFER_SIZE

1 participant