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
Don't assume that rack/chunked
exists.
#47078
Conversation
Would not this change make Rails on Rack 3.0 show a deprecation warning every time this feature is used, and on Rack 3.1 we would have an error? It seems we need to rewrite |
The deprecation warning will be shown only once (and only if warnings are turned on), and I suggest we rewrite this feature for Rails 8 and leave the current implementation for Rails 7.x. AFAICT, there are no tests for this feature either. |
Why? I don't want to ship code to users that show deprecation. If the framework is using deprecated code, and we can avoid this, we should stop using deprecated code. Even if that means copying the code from chunked to inside Rails while Rack doesn't have an API for it (assuming that is the limitation). |
How much feature churn do you want in a minor release? The streaming function will work (as much as it did before), and there is no need to copy the code to retain compatibility. The problem with We could try to provide a Rack 3 compatible rewrite, but strictly speaking that's introducing new features rather than simply enabling compatibility. If that's what you'd like to do, I suggest we merge this PR, and then make a new feature PR to implement that change. I'd be happy to work on that after the |
Are those warnings turned on by default on new rails app with Rack 3? |
Yes, IIUC, |
Unfortunately not compatible with Ruby 2.7:
|
@rafaelfranca what would you like to do here? cc @tenderlove |
I thought about this a bit more. We don't have to merge this PR for Rack 3.0, but if we don't the warning will be displayed every time Rails is loaded vs only when the feature is used (via autoload). That being said, I believe some of the test suites expect no warnings to be printed, so maybe it is required to get the test suite passing completely. |
`Rack::Chunked` was a bad idea and has been deprecated in Rack 3.0 and will be removed in Rack 3.1. Therefore, don't require that file directly. In Rack 2, it will be autoloaded correctly.
3df4418
to
afed7be
Compare
@ioquatix as @rafaelfranca suggested earlier, can we just bring the relevant code to Rails and then do the required changes to get this working fine with the next version of Rack? This seems to be passing fine with all relevant tests: diff --git a/actionpack/lib/action_controller/metal/streaming.rb b/actionpack/lib/action_controller/metal/streaming.rb
index 58a6d9c573..a457ca2d59 100644
--- a/actionpack/lib/action_controller/metal/streaming.rb
+++ b/actionpack/lib/action_controller/metal/streaming.rb
@@ -1,7 +1,5 @@
# frozen_string_literal: true
-require "rack/chunked"
-
module ActionController # :nodoc:
# = Action Controller \Streaming
#
@@ -208,6 +206,35 @@ module ActionController # :nodoc:
# * https://www.phusionpassenger.com/docs/references/config_reference/nginx/#passenger_buffer_response
#
module Streaming
+ class Body # :nodoc:
+ TERM = "\r\n"
+ TAIL = "0#{TERM}"
+
+ # Store the response body to be chunked.
+ def initialize(body)
+ @body = body
+ end
+
+ # For each element yielded by the response body, yield
+ # the element in chunked encoding.
+ def each(&block)
+ term = TERM
+ @body.each do |chunk|
+ size = chunk.bytesize
+ next if size == 0
+
+ yield [size.to_s(16), term, chunk.b, term].join
+ end
+ yield TAIL
+ yield term
+ end
+
+ # Close the response body if the response body supports it.
+ def close
+ @body.close if @body.respond_to?(:close)
+ end
+ end
+
private
# Set proper cache control and transfer encoding when streaming
def _process_options(options)
@@ -226,7 +253,7 @@ def _process_options(options)
# Call render_body if we are streaming instead of usual +render+.
def _render_template(options)
if options.delete(:stream)
- Rack::Chunked::Body.new view_renderer.render_body(view_context, options)
+ Body.new view_renderer.render_body(view_context, options)
else
super
end |
Based on my testing, it's okay. |
Closing as no longer needed. |
Rack::Chunked
was a bad idea and has been deprecated in Rack 3.0 and will be removed in Rack 3.1. Therefore, don't require that file directly. In Rack 2, it will be autoloaded correctly.