Sha256: ee4b7c8079fe491c87c6c5ad176f4b8db4dc23869af574ebe69e6d4042f03f03

Contents?: true

Size: 1.63 KB

Versions: 39

Compression:

Stored size: 1.63 KB

Contents

# there is also another in cramp this is from https://github.com/macournoyer/thin_async/blob/master/lib/thin/async.rb
module Thin
  unless defined?(DeferrableBody)
    # Based on version from James Tucker <raggi@rubyforge.org>
    class DeferrableBody
      include ::EM::Deferrable

      def initialize
        @queue = []
      end

      def call(body)
        @queue << body
        schedule_dequeue
      end

      def each(&blk)
        @body_callback = blk
        schedule_dequeue
      end

      private
        def schedule_dequeue
          return unless @body_callback
          ::EM.next_tick do
            next unless body = @queue.shift
            body.each do |chunk|
              @body_callback.call(chunk)
            end
            schedule_dequeue unless @queue.empty?
          end
        end
    end
  end

  # Response whos body is sent asynchronously.
  class AsyncResponse
    include Rack::Response::Helpers

    attr_reader :headers, :callback, :closed
    attr_accessor :status

    def initialize(env, status=200, headers={})
      @callback = env['async.callback']
      @body = DeferrableBody.new
      @status = status
      @headers = headers
      @headers_sent = false
    end

    def send_headers
      return if @headers_sent
      @callback.call [@status, @headers, @body]
      @headers_sent = true
    end

    def write(body)
      send_headers
      @body.call(body.respond_to?(:each) ? body : [body])
    end
    alias :<< :write

    # Tell Thin the response is complete and the connection can be closed.
    def done
      @closed = true
      send_headers
      ::EM.next_tick { @body.succeed }
    end

  end
end

Version data entries

39 entries across 39 versions & 1 rubygems

Version Path
message_bus-2.0.2 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.1 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.11 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.10 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.9 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.8 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.7 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.6 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.5 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.4 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.3 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.2 lib/message_bus/rack/thin_ext.rb
message_bus-2.0.0.beta.1 lib/message_bus/rack/thin_ext.rb
message_bus-1.1.1 lib/message_bus/rack/thin_ext.rb
message_bus-1.1.0 lib/message_bus/rack/thin_ext.rb
message_bus-1.0.16 lib/message_bus/rack/thin_ext.rb
message_bus-1.0.15 lib/message_bus/rack/thin_ext.rb
message_bus-1.0.14 lib/message_bus/rack/thin_ext.rb
message_bus-1.0.13 lib/message_bus/rack/thin_ext.rb