about summary refs log tree commit homepage
path: root/lib/rainbows/xepoll
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2011-02-04 18:58:26 -0800
committerEric Wong <normalperson@yhbt.net>2011-02-04 18:58:26 -0800
commit8e2e8adda8adbadee8ab31cde700b7e486b1154c (patch)
treeb4fadc909c9636486ee8e1bebd735786063b9af7 /lib/rainbows/xepoll
parent0f4f015737297ac5245d0be9ee83553770ade0a9 (diff)
downloadrainbows-8e2e8adda8adbadee8ab31cde700b7e486b1154c.tar.gz
It's too long especially since XEpollThreadPool is planned :>
Diffstat (limited to 'lib/rainbows/xepoll')
-rw-r--r--lib/rainbows/xepoll/client.rb60
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/rainbows/xepoll/client.rb b/lib/rainbows/xepoll/client.rb
new file mode 100644
index 0000000..fa5999b
--- /dev/null
+++ b/lib/rainbows/xepoll/client.rb
@@ -0,0 +1,60 @@
+# -*- encoding: binary -*-
+# :enddoc:
+
+module Rainbows::XEpoll::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 Errno::EINTR
+    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