I am experimenting with Rails 6 for an API use case. Users would post large requests which rails would receive and stream to another place. This would probably run with JRuby, but right now I am testing with MRI Ruby. The webserver is Puma (out of the box rails install).
Is there any way to read the request body as a stream as it is uploaded, or does Rails need to wait for the entire request to upload before it can access it?
Using this Curl command:
curl -i -X PUT -T data.bin "http://localhost:3000/welcome/stream"
And this simple controller:
def stream
puts "The stream was called"
io = request.body
until io.eof?
io.read(1024*1024)
puts "Read 1MB"
end
head 200, content_type: "text/html"
end
It seems as though the entire request is staged somewhere before rails get access to it, as there is a long delay before any of my puts appear in the console.
Searching a bit, it seems it may be the websever (Puma, Unicorn, etc) that buffers the data and it only invokes Rails when all the data has been received.
Is there anyway to start reading the stream as it comes in and or avoid buffering it?
Is it Rack or Puma which does the buffering? If so, where does it buffer the data?