rack_fiber.rb

 1# frozen_string_literal: true
 2
 3require "fiber"
 4
 5module Rack
 6	class Fiber
 7		def initialize(app)
 8			@app = app
 9		end
10
11		def call(env)
12			async_callback = env.delete("async.callback")
13			EM.next_tick { run_fiber(env, async_callback) }
14			throw :async
15		end
16
17	protected
18
19		def run_fiber(env, async_callback)
20			::Fiber.new {
21				begin
22					async_callback.call(@app.call(env))
23				rescue ::Exception # rubocop:disable Lint/RescueException
24					async_callback.call([500, {}, [$!.to_s]])
25				end
26			}.resume
27		end
28	end
29end