Class: Roda::RodaPlugins::Streaming::AsyncStream

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/roda/plugins/streaming.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(opts = OPTS, &block) ⇒ AsyncStream

Handle streaming options, see Streaming for details.



100
101
102
103
104
# 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

Instance Method Details

#closeObject

Stop streaming.



113
114
115
116
# File 'lib/roda/plugins/streaming.rb', line 113

def close
  @queue.close # terminate the producer thread
  @stream.close
end

#each(&out) ⇒ Object

Continue streaming data until the stream is finished.



107
108
109
110
# File 'lib/roda/plugins/streaming.rb', line 107

def each(&out)
  dequeue_chunks(&out)
  @thread.join
end