Sha256: e3ee260be5ef76c24bf380cf8de8938e7a9c1e9a643c64115f974d37db056f93

Contents?: true

Size: 848 Bytes

Versions: 5

Compression:

Stored size: 848 Bytes

Contents

# frozen_string_literal: true

##
# The middleware wraps every request in a separate Fiber. It should always be on the top of the middleware stack,
# as it implements a custom defer protocol, which may break middlewares located above.
#
class Rage::FiberWrapper
  def initialize(app)
    Iodine.on_state(:on_start) do
      unless Fiber.scheduler
        Fiber.set_scheduler(Rage::FiberScheduler.new)
      end
    end
    @app = app
  end

  def call(env)
    fiber = Fiber.schedule do
      @app.call(env)
    ensure
      # notify Iodine the request can now be resumed
      Iodine.publish(Fiber.current.__get_id, "", Iodine::PubSub::PROCESS)
    end

    # the fiber encountered blocking IO and yielded; instruct Iodine to pause the request
    if fiber.alive?
      [:__http_defer__, fiber]
    else
      fiber.__get_result
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rage-rb-1.11.0 lib/rage/middleware/fiber_wrapper.rb
rage-rb-1.10.1 lib/rage/middleware/fiber_wrapper.rb
rage-rb-1.10.0 lib/rage/middleware/fiber_wrapper.rb
rage-rb-1.9.0 lib/rage/middleware/fiber_wrapper.rb
rage-rb-1.8.0 lib/rage/middleware/fiber_wrapper.rb