class Roda::RodaPlugins::Streaming::AsyncStream
data can be streamed to clients while it is being prepared by the application.
Uses a separate thread that pushes streaming results to a queue, so that
Class of the response body if you use #stream with :async set to true.
def close
def close @queue.close # terminate the producer thread @stream.close end
def dequeue_chunks
def dequeue_chunks while chunk = @queue.pop yield chunk end end
def each(&out)
def each(&out) dequeue_chunks(&out) @thread.join end
def enqueue_chunks
def enqueue_chunks @stream.each do |chunk| @queue.push(chunk) end rescue ClosedQueueError # connection was closed ensure @queue.close end
def initialize(opts=OPTS, &block)
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