tests: "wc -c" portability for *BSDs
[rainbows.git] / lib / rainbows / fiber_spawn.rb
blob2c6d13d3960aed47869dc65f23d4a3d1f9080472
1 # -*- encoding: binary -*-
2 require 'rainbows/fiber'
4 # Simple Fiber-based concurrency model for 1.9.  This spawns a new Fiber
5 # for every incoming client connection and the root Fiber for scheduling
6 # and connection acceptance.
8 # This concurrency model is difficult to use with existing applications,
9 # lacks third-party support, and is thus NOT recommended.
11 # This exports a streaming "rack.input" with lightweight concurrency.
12 # Applications are strongly advised to wrap all slow IO objects
13 # (sockets, pipes) using the Rainbows::Fiber::IO class whenever
14 # possible.
15 module Rainbows::FiberSpawn
16   include Rainbows::Fiber::Base
18   def worker_loop(worker) # :nodoc:
19     init_worker_process(worker)
20     Rainbows::Fiber::Base.setup(self.class, app)
21     limit = worker_connections
23     begin
24       schedule do |l|
25         break if Rainbows.cur >= limit
26         io = l.kgio_tryaccept or next
27         Fiber.new { process(io) }.resume
28       end
29     rescue => e
30       Rainbows::Error.listen_loop(e)
31     end while Rainbows.cur_alive
32   end
33 end