rainbows.git  about / heads / tags
Unicorn for sleepy apps and slow clients
blob 075fcfb3141b5875a0eb1ae15d419a9274042f0d 1143 bytes (raw)
$ git show v3.2.0:lib/rainbows/epoll.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
36
37
38
39
40
41
42
43
 
# -*- encoding: binary -*-
# :enddoc:
require 'sleepy_penguin'
require 'sendfile'

# Edge-triggered epoll concurrency model.  This is extremely unfair
# and optimized for throughput at the expense of fairness
module Rainbows::Epoll
  include Rainbows::Base
  ReRun = []
  autoload :Server, 'rainbows/epoll/server'
  autoload :Client, 'rainbows/epoll/client'
  autoload :ResponsePipe, 'rainbows/epoll/response_pipe'
  autoload :ResponseChunkPipe, 'rainbows/epoll/response_chunk_pipe'
  class << self
    attr_writer :nr_clients
  end

  def self.loop
    timeout = Rainbows.server.timeout
    begin
      EP.wait(nil, timeout) { |flags, obj| obj.epoll_run }
      while obj = ReRun.shift
        obj.epoll_run
      end
      Rainbows::Epoll::Client.expire
    rescue Errno::EINTR
    rescue => e
      Rainbows::Error.listen_loop(e)
    end while Rainbows.tick || @nr_clients.call > 0
  end

  def init_worker_process(worker)
    super
    Rainbows::Epoll.const_set :EP, SleepyPenguin::Epoll.new
    Rainbows::Client.__send__ :include, Client
  end

  def worker_loop(worker) # :nodoc:
    init_worker_process(worker)
    Server.run
  end
end

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