Action Controller Live
将此模块混合到您的控制器中,该控制器中的所有操作都将能够在写入时将数据流式传输到客户端。
class MyController < ActionController::Base
include ActionController::Live
def stream
response.headers['Content-Type'] = 'text/event-stream'
100.times {
response.stream.write "hello world\n"
sleep 1
}
ensure
response.stream.close
end
end
此模块有一些注意事项。您**不能**在响应提交后写入标头(Response#committed? 将返回真值)。在响应流上调用write
或close
将导致响应对象被提交。确保在调用流上的 write 或 close 之前设置所有标头。
您**必须**在完成时在流上调用 close,否则套接字可能会永远保持打开状态。
最后一个注意事项是,您的操作在与主线程不同的线程中执行。确保您的操作是线程安全的,这应该不成问题(不要跨线程共享状态等)。
请注意,Rails 默认情况下包含Rack::ETag
,它将缓冲您的响应。因此,流式传输响应可能无法与 Rack 2.2.x 正确配合,您可能需要在应用程序中实现解决方法。您可以设置ETag
或Last-Modified
响应标头,或者从中间件堆栈中删除Rack::ETag
来解决此问题。
以下是如何在 Rack 版本为 2.2.x 时设置Last-Modified
标头的示例
def stream
response.headers["Content-Type"] = "text/event-stream"
response.headers["Last-Modified"] = Time.now.httpdate # Add this line if your Rack version is 2.2.x
...
end
- 模块 ActionController::Live::ClassMethods
- 类 ActionController::Live::ClientDisconnected
- 类 ActionController::Live::SSE
- P
- R
- S
实例公共方法
process(name) 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal/live.rb, line 273 def process(name) t1 = Thread.current locals = t1.keys.map { |key| [key, t1[key]] } error = nil # This processes the action in a child thread. It lets us return the # response code and headers back up the Rack stack, and still process # the body in parallel with sending data to the client. new_controller_thread { ActiveSupport::Dependencies.interlock.running do t2 = Thread.current # Since we're processing the view in a different thread, copy the # thread locals from the main thread to the child thread. :'( locals.each { |k, v| t2[k] = v } ActiveSupport::IsolatedExecutionState.share_with(t1) begin super(name) rescue => e if @_response.committed? begin @_response.stream.write(ActionView::Base.streaming_completion_on_exception) if request.format == :html @_response.stream.call_on_error rescue => exception log_error(exception) ensure log_error(e) @_response.stream.close end else error = e end ensure @_response.commit! end end } ActiveSupport::Dependencies.interlock.permit_concurrent_loads do @_response.await_commit end raise error if error end
response_body=(body) 链接
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal/live.rb, line 319 def response_body=(body) super response.close if response end
send_stream(filename:, disposition: "attachment", type: nil) 链接
将流发送到浏览器,这在您生成导出或其他运行数据时很有用,因为您不希望整个文件首先在内存中缓冲。类似于 send_data,但数据是实时生成的。
选项
-
:filename
- 建议浏览器使用的文件名。 -
:type
- 指定 HTTP 内容类型。您可以指定字符串或使用Mime::Type.register
注册的类型的符号,例如 :json。如果省略,类型将从:filename
中指定的扩展名推断。如果扩展名没有注册内容类型,则将使用默认类型“application/octet-stream”。 -
:disposition
- 指定文件将内联显示还是下载。有效值为“inline”和“attachment”(默认值)。
生成 csv 导出的示例
send_stream(filename: "subscribers.csv") do |stream|
stream.write "email_address,updated_at\n"
@subscribers.find_each do |subscriber|
stream.write "#{subscriber.email_address},#{subscriber.updated_at}\n"
end
end
来源:显示 | 在 GitHub 上
# File actionpack/lib/action_controller/metal/live.rb, line 345 def send_stream(filename:, disposition: "attachment", type: nil) response.headers["Content-Type"] = (type.is_a?(Symbol) ? Mime[type].to_s : type) || Mime::Type.lookup_by_extension(File.extname(filename).downcase.delete("."))&.to_s || "application/octet-stream" response.headers["Content-Disposition"] = ActionDispatch::Http::ContentDisposition.format(disposition: disposition, filename: filename) yield response.stream ensure response.stream.close end