Sha256: 04e096a5a53fb8f3273f7bc3b699f1cc85f6dccbf9469777581dbddfe4c7efcf

Contents?: true

Size: 773 Bytes

Versions: 6

Compression:

Stored size: 773 Bytes

Contents

# frozen_string_literal: true

require 'fiber'

module CottonTail
  module Queue
    # Queue Reader
    class Reader
      def self.spawn(queue, **kwargs)
        Thread.new { new(queue, **kwargs).start }
      end

      def initialize(queue, app:)
        @queue = queue
        @app = app
      end

      def fiber
        @fiber ||= Fiber.new do
          Fiber.yield @queue.pop until @queue.empty? && @queue.closed?
        end
      end

      def start
        call_next while fiber.alive?
      end

      private

      def call_next
        request = fiber.resume
        middleware.call([env, request, Response.new]) if request
      end

      def middleware
        @app.config.middleware
      end

      def env
        @app.env
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cotton-tail-0.7.0 lib/cotton_tail/queue/reader.rb
cotton-tail-0.6.1 lib/cotton_tail/queue/reader.rb
cotton-tail-0.6.0 lib/cotton_tail/queue/reader.rb
cotton-tail-0.5.0 lib/cotton_tail/queue/reader.rb
cotton-tail-0.4.1 lib/cotton_tail/queue/reader.rb
cotton-tail-0.4.0 lib/cotton_tail/queue/reader.rb