writer_thread_spawn: factor out Client.quit
[rainbows.git] / lib / rainbows / fiber_spawn.rb
blob17bd884cf282726eb6803b9effcc07220a7ced4c
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 G.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 G.alive || G.cur > 0
27   end
28 end