Class: Rage::FiberWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/rage/middleware/fiber_wrapper.rb

Overview

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.

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ FiberWrapper

Returns a new instance of FiberWrapper.



8
9
10
11
12
13
14
15
# File 'lib/rage/middleware/fiber_wrapper.rb', line 8

def initialize(app)
  Iodine.on_state(:on_start) do
    unless Fiber.scheduler
      Fiber.set_scheduler(Rage::FiberScheduler.new)
    end
  end
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rage/middleware/fiber_wrapper.rb', line 17

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