eliminate G constant and just use the Rainbows! module
[rainbows.git] / lib / rainbows / fiber_spawn.rb
blob84df30dc70e147051899a9e6e0d4e795c35531ed
1 # -*- encoding: binary -*-
2 require 'rainbows/fiber'
4 # Simple Fiber-based concurrency model for 1.9.  This spawns a new
5 # Fiber for every incoming client connection and the root Fiber for
6 # scheduling and connection acceptance.  This exports a streaming
7 # "rack.input" with lightweight concurrency.  Applications are
8 # strongly advised to wrap all slow IO objects (sockets, pipes) using
9 # the Rainbows::Fiber::IO class whenever possible.
10 module Rainbows::FiberSpawn
11   include Rainbows::Fiber::Base
13   def worker_loop(worker) # :nodoc:
14     init_worker_process(worker)
15     Rainbows::Fiber::Base.setup(self.class, app)
16     limit = worker_connections
18     begin
19       schedule do |l|
20         break if Rainbows.cur >= limit
21         io = l.kgio_tryaccept or next
22         Fiber.new { process(io) }.resume
23       end
24     rescue => e
25       Rainbows::Error.listen_loop(e)
26     end while Rainbows.alive || Rainbows.cur > 0
27   end
28 end