about summary refs log tree commit homepage
path: root/lib/rainbows/rev/thread_client.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-26 21:40:34 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-26 21:40:34 +0000
commit869e0d5da8cbe8959983ed66e2b4c31dc7a37d92 (patch)
treee42cd022f3d8366effcab1ac437e0ad7783b27a4 /lib/rainbows/rev/thread_client.rb
parentc1655a501fc26f7100dd788b42a1914be833fea4 (diff)
downloadrainbows-869e0d5da8cbe8959983ed66e2b4c31dc7a37d92.tar.gz
rename rev/thread => rev/thread_client
While we're at it, unindent
Diffstat (limited to 'lib/rainbows/rev/thread_client.rb')
-rw-r--r--lib/rainbows/rev/thread_client.rb38
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/rainbows/rev/thread_client.rb b/lib/rainbows/rev/thread_client.rb
new file mode 100644
index 0000000..e529b5b
--- /dev/null
+++ b/lib/rainbows/rev/thread_client.rb
@@ -0,0 +1,38 @@
+# -*- encoding: binary -*-
+# :enddoc:
+require 'thread'
+require 'rainbows/rev/master'
+
+RUBY_VERSION =~ %r{\A1\.8} and
+  warn "Rev and Threads do not mix well under Ruby 1.8"
+
+class Rainbows::Rev::ThreadClient < Rainbows::Rev::Client
+  def app_call
+    KATO.delete(self)
+    disable if enabled?
+    @env[RACK_INPUT] = @input
+    app_dispatch # must be implemented by subclass
+  end
+
+  # this is only called in the master thread
+  def response_write(response)
+    alive = @hp.next? && G.alive
+    rev_write_response(response, alive)
+    return quit unless alive && :close != @state
+
+    @state = :headers
+  end
+
+  # fails-safe application dispatch, we absolutely cannot
+  # afford to fail or raise an exception (killing the thread)
+  # here because that could cause a deadlock and we'd leak FDs
+  def app_response
+    begin
+      @env[REMOTE_ADDR] = @_io.kgio_addr
+      APP.call(@env.update(RACK_DEFAULTS))
+    rescue => e
+      Rainbows::Error.app(e) # we guarantee this does not raise
+      [ 500, {}, [] ]
+    end
+  end
+end