class Roda::RodaPlugins::Streaming::AsyncStream
Class of the response body if you use stream with :async set to true. Uses a separate thread that pushes streaming results to a queue, so that data can be streamed to clients while it is being prepared by the application.
Public Class Methods
Source
# File lib/roda/plugins/streaming.rb, line 100 def initialize(opts=OPTS, &block) @stream = Stream.new(opts, &block) @queue = opts[:queue] || SizedQueue.new(10) # have some default backpressure @thread = Thread.new { enqueue_chunks } end
Handle streaming options, see Streaming
for details.
Public Instance Methods
Source
# File lib/roda/plugins/streaming.rb, line 113 def close @queue.close # terminate the producer thread @stream.close end
Stop streaming.
Source
# File lib/roda/plugins/streaming.rb, line 107 def each(&out) dequeue_chunks(&out) @thread.join end
Continue streaming data until the stream is finished.
Private Instance Methods
Source
# File lib/roda/plugins/streaming.rb, line 132 def dequeue_chunks while chunk = @queue.pop yield chunk end end
Pop each streaming chunk from the queue and yield it.
Source
# File lib/roda/plugins/streaming.rb, line 121 def enqueue_chunks @stream.each do |chunk| @queue.push(chunk) end rescue ClosedQueueError # connection was closed ensure @queue.close end
Push each streaming chunk onto the queue.