epoll_wait: flags argument is unused
[rainbows.git] / lib / rainbows / epoll.rb
blob8f3d020f35f36ef69417c6773b8cb35b71a9eb32
1 # -*- encoding: binary -*-
2 require 'sleepy_penguin'
3 require 'sendfile'
5 # Edge-triggered epoll concurrency model using
6 # {sleepy_penguin}[http://bogomips.org/sleepy_penguin/] for epoll.
8 # Unlike more portable options like Coolio and EventMachine, this
9 # is Linux-only, but uses edge-triggering instead of level-triggering,
10 # so it may perform better in some cases.  Coolio and EventMachine have
11 # better library support and may be widely-used, however.
13 # Consider using XEpoll instead of this if you are using Ruby 1.9,
14 # it will avoid accept()-scalability issues with many worker processes.
16 # When serving static files, this is extremely unfair and optimized
17 # for throughput at the expense of fairness.  This is not an issue
18 # if you're not serving static files, or if your working set is
19 # small enough to aways be in your kernel page cache.  This concurrency
20 # model may starve clients if you have slow disks and large static files.
21 module Rainbows::Epoll
22   # :stopdoc:
23   include Rainbows::Base
24   ReRun = []
25   autoload :Server, 'rainbows/epoll/server'
26   autoload :Client, 'rainbows/epoll/client'
27   autoload :ResponsePipe, 'rainbows/epoll/response_pipe'
28   autoload :ResponseChunkPipe, 'rainbows/epoll/response_chunk_pipe'
29   class << self
30     attr_writer :nr_clients
31   end
33   def self.loop
34     begin
35       EP.wait(nil, 1000) { |_, obj| obj.epoll_run }
36       while obj = ReRun.shift
37         obj.epoll_run
38       end
39       Rainbows::Epoll::Client.expire
40     rescue Errno::EINTR
41     rescue => e
42       Rainbows::Error.listen_loop(e)
43     end while Rainbows.tick || @nr_clients.call > 0
44   end
46   def init_worker_process(worker)
47     super
48     Rainbows::Epoll.const_set :EP, SleepyPenguin::Epoll.new
49     Rainbows::Client.__send__ :include, Client
50   end
52   def worker_loop(worker) # :nodoc:
53     init_worker_process(worker)
54     Server.run
55   end
56   # :startdoc:
57 end