dev: isolate: bump cramp to 0.11
[rainbows.git] / lib / rainbows / fiber_spawn.rb
blob6104a7b02821ccbb20937f098aed09d728e3fa94
1 # -*- encoding: binary -*-
2 require 'rainbows/fiber'
4 module Rainbows
6   # Simple Fiber-based concurrency model for 1.9.  This spawns a new
7   # Fiber for every incoming client connection and the root Fiber for
8   # scheduling and connection acceptance.  This exports a streaming
9   # "rack.input" with lightweight concurrency.  Applications are
10   # strongly advised to wrap all slow IO objects (sockets, pipes) using
11   # the Rainbows::Fiber::IO class whenever possible.
13   module FiberSpawn
14     include Fiber::Base
16     def worker_loop(worker)
17       init_worker_process(worker)
18       Fiber::Base.const_set(:APP, app)
19       limit = worker_connections
20       fio = Rainbows::Fiber::IO
22       begin
23         schedule do |l|
24           break if G.cur >= limit
25           io = Rainbows.accept(l) or next
26           ::Fiber.new { process_client(fio.new(io, ::Fiber.current)) }.resume
27         end
28       rescue => e
29         Error.listen_loop(e)
30       end while G.alive || G.cur > 0
31     end
33   end
34 end