code shuffling for kgio
[rainbows.git] / lib / rainbows / fiber_spawn.rb
blob1a0da04234b30c85c57a37154cd281345955b5bf
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) # :nodoc:
17       init_worker_process(worker)
18       Fiber::Base.setup(self.class, app)
19       limit = worker_connections
21       begin
22         schedule do |l|
23           break if G.cur >= limit
24           io = l.kgio_tryaccept or next
25           ::Fiber.new { process(io) }.resume
26         end
27       rescue => e
28         Error.listen_loop(e)
29       end while G.alive || G.cur > 0
30     end
32   end
33 end