about summary refs log tree commit homepage
diff options
context:
space:
mode:
-rw-r--r--lib/rainbows.rb1
-rw-r--r--lib/rainbows/http_server.rb2
-rw-r--r--lib/rainbows/max_body.rb2
-rw-r--r--lib/rainbows/xaccept_epoll.rb25
-rw-r--r--lib/rainbows/xaccept_epoll/client.rb59
-rw-r--r--t/GNUmakefile1
-rw-r--r--t/simple-http_XAcceptEpoll.ru9
-rwxr-xr-xt/t0113-rewindable-input-false.sh2
-rwxr-xr-xt/t0114-rewindable-input-true.sh2
-rw-r--r--t/test_isolate.rb1
10 files changed, 100 insertions, 4 deletions
diff --git a/lib/rainbows.rb b/lib/rainbows.rb
index 4fa488e..f22a927 100644
--- a/lib/rainbows.rb
+++ b/lib/rainbows.rb
@@ -120,6 +120,7 @@ module Rainbows
     :CoolioThreadPool => 50,
     :CoolioFiberSpawn => 50,
     :Epoll => 50,
+    :XAcceptEpoll => 50,
     :EventMachine => 50,
     :FiberSpawn => 50,
     :FiberPool => 50,
diff --git a/lib/rainbows/http_server.rb b/lib/rainbows/http_server.rb
index a9767bf..91c6cad 100644
--- a/lib/rainbows/http_server.rb
+++ b/lib/rainbows/http_server.rb
@@ -72,7 +72,7 @@ class Rainbows::HttpServer < Unicorn::HttpServer
     new_defaults = {
       'rainbows.model' => (@use = model.to_sym),
       'rack.multithread' => !!(model.to_s =~ /Thread/),
-      'rainbows.autochunk' => [:Coolio,:Rev,:Epoll,
+      'rainbows.autochunk' => [:Coolio,:Rev,:Epoll,:XAcceptEpoll,
                                :EventMachine,:NeverBlock].include?(@use),
     }
     Rainbows::Const::RACK_DEFAULTS.update(new_defaults)
diff --git a/lib/rainbows/max_body.rb b/lib/rainbows/max_body.rb
index 105bbb0..68ae3e3 100644
--- a/lib/rainbows/max_body.rb
+++ b/lib/rainbows/max_body.rb
@@ -56,7 +56,7 @@ class Rainbows::MaxBody
     when :Rev, :Coolio, :EventMachine, :NeverBlock,
          :RevThreadSpawn, :RevThreadPool,
          :CoolioThreadSpawn, :CoolioThreadPool,
-         :Epoll
+         :Epoll, :XAcceptEpoll
       return
     end
 
diff --git a/lib/rainbows/xaccept_epoll.rb b/lib/rainbows/xaccept_epoll.rb
new file mode 100644
index 0000000..0690c07
--- /dev/null
+++ b/lib/rainbows/xaccept_epoll.rb
@@ -0,0 +1,25 @@
+# -*- encoding: binary -*-
+# :enddoc:
+require 'raindrops'
+require 'rainbows/epoll'
+
+# Edge-triggered epoll concurrency model with blocking accept() in
+# a (hopefully) native thread.  This is recommended over Epoll for
+# Ruby 1.9 users as it can workaround accept()-scalability issues
+# on multicore machines.
+module Rainbows::XAcceptEpoll
+  include Rainbows::Base
+  autoload :Client, 'rainbows/xaccept_epoll/client'
+
+  def init_worker_process(worker)
+    super
+    Rainbows::Epoll.const_set :EP, SleepyPenguin::Epoll.new
+    trap(:QUIT) { Rainbows::Epoll.quit! }
+    Rainbows::Client.__send__ :include, Client
+  end
+
+  def worker_loop(worker) # :nodoc:
+    init_worker_process(worker)
+    Client.run
+  end
+end
diff --git a/lib/rainbows/xaccept_epoll/client.rb b/lib/rainbows/xaccept_epoll/client.rb
new file mode 100644
index 0000000..6c3a0f5
--- /dev/null
+++ b/lib/rainbows/xaccept_epoll/client.rb
@@ -0,0 +1,59 @@
+# -*- encoding: binary -*-
+# :enddoc:
+
+module Rainbows::XAcceptEpoll::Client
+  include Rainbows::Epoll::Client
+  MAX = Rainbows.server.worker_connections
+  THRESH = MAX - 1
+  EP = Rainbows::Epoll::EP
+  N = Raindrops.new(1)
+  @timeout = Rainbows.server.timeout / 2.0
+  THREADS = Rainbows::HttpServer::LISTENERS.map do |sock|
+    Thread.new(sock) do |sock|
+      sleep
+      begin
+        if io = sock.kgio_accept
+          N.incr(0, 1)
+          io.epoll_once
+        end
+        sleep while N[0] >= MAX
+      rescue => e
+        Rainbows::Error.listen_loop(e)
+      end while Rainbows.alive
+    end
+  end
+
+  def self.run
+    THREADS.each { |t| t.run }
+    begin
+      EP.wait(nil, @timeout) { |flags, obj| obj.epoll_run }
+      Rainbows::Epoll::Client.expire
+    rescue => e
+      Rainbows::Error.listen_loop(e)
+    end while Rainbows.tick
+
+    THREADS.delete_if do |thr|
+      Rainbows.tick
+      begin
+        thr.run
+        thr.join(0.01)
+      rescue
+        true
+      end
+    end until THREADS.empty?
+  end
+
+  # only call this once
+  def epoll_once
+    @wr_queue = [] # may contain String, ResponsePipe, and StreamFile objects
+    post_init
+    EP.set(self, IN) # wake up the main thread
+    rescue => e
+      Rainbows::Error.write(self, e)
+  end
+
+  def on_close
+    KATO.delete(self)
+    N.decr(0, 1) == THRESH and THREADS.each { |t| t.run }
+  end
+end
diff --git a/t/GNUmakefile b/t/GNUmakefile
index 7b50944..538065c 100644
--- a/t/GNUmakefile
+++ b/t/GNUmakefile
@@ -19,6 +19,7 @@ endif
 RUBY_ENGINE := $(shell $(RUBY) -e 'puts((RUBY_ENGINE rescue "ruby"))')
 export RUBY_VERSION RUBY_ENGINE
 
+models += XAcceptEpoll
 models += Epoll
 models += WriterThreadPool
 models += WriterThreadSpawn
diff --git a/t/simple-http_XAcceptEpoll.ru b/t/simple-http_XAcceptEpoll.ru
new file mode 100644
index 0000000..fc0baa6
--- /dev/null
+++ b/t/simple-http_XAcceptEpoll.ru
@@ -0,0 +1,9 @@
+use Rack::ContentLength
+use Rack::ContentType
+run lambda { |env|
+  if env['rack.multithread'] == false && env['rainbows.model'] == :XAcceptEpoll
+    [ 200, {}, [ Thread.current.inspect << "\n" ] ]
+  else
+    raise env.inspect
+  end
+}
diff --git a/t/t0113-rewindable-input-false.sh b/t/t0113-rewindable-input-false.sh
index 82b0fb7..1119dbf 100755
--- a/t/t0113-rewindable-input-false.sh
+++ b/t/t0113-rewindable-input-false.sh
@@ -3,7 +3,7 @@
 skip_models EventMachine NeverBlock
 skip_models Rev RevThreadSpawn RevThreadPool
 skip_models Coolio CoolioThreadSpawn CoolioThreadPool
-skip_models Epoll
+skip_models Epoll XAcceptEpoll
 
 t_plan 4 "rewindable_input toggled to false"
 
diff --git a/t/t0114-rewindable-input-true.sh b/t/t0114-rewindable-input-true.sh
index fd8561c..f4ef796 100755
--- a/t/t0114-rewindable-input-true.sh
+++ b/t/t0114-rewindable-input-true.sh
@@ -3,7 +3,7 @@
 skip_models EventMachine NeverBlock
 skip_models Rev RevThreadSpawn RevThreadPool
 skip_models Coolio CoolioThreadSpawn CoolioThreadPool
-skip_models Epoll
+skip_models Epoll XAcceptEpoll
 
 t_plan 4 "rewindable_input toggled to true"
 
diff --git a/t/test_isolate.rb b/t/test_isolate.rb
index 7b601e7..7ab4dd8 100644
--- a/t/test_isolate.rb
+++ b/t/test_isolate.rb
@@ -18,6 +18,7 @@ lock.flock(File::LOCK_EX)
 Isolate.now!(opts) do
   gem 'unicorn', '3.3.1'
   gem 'kcar', '0.1.2'
+  gem 'raindrops', '0.4.1'
 
   if engine == "ruby"
     gem 'sendfile', '1.0.0' # next Rubinius should support this