rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob ecf83d8849a6d66ad9cd1dac33c99b39bea04a5d 1019 bytes (raw)
$ git show v0.97.0:lib/rainbows/fiber_spawn.rb	# shows this blob on the CLI

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
# -*- encoding: binary -*-
require 'rainbows/fiber'

module Rainbows

  # Simple Fiber-based concurrency model for 1.9.  This spawns a new
  # Fiber for every incoming client connection and the root Fiber for
  # scheduling and connection acceptance.  This exports a streaming
  # "rack.input" with lightweight concurrency.  Applications are
  # strongly advised to wrap all slow IO objects (sockets, pipes) using
  # the Rainbows::Fiber::IO class whenever possible.

  module FiberSpawn
    include Fiber::Base
    include Rainbows::Acceptor

    def worker_loop(worker) # :nodoc:
      init_worker_process(worker)
      Fiber::Base.setup(self.class, app)
      limit = worker_connections
      fio = Rainbows::Fiber::IO

      begin
        schedule do |l|
          break if G.cur >= limit
          io = accept(l) or next
          ::Fiber.new { process_client(fio.new(io, ::Fiber.current)) }.resume
        end
      rescue => e
        Error.listen_loop(e)
      end while G.alive || G.cur > 0
    end

  end
end

git clone https://yhbt.net/rainbows.git