about summary refs log tree commit homepage
path: root/lib/rainbows/base.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2009-10-11 12:15:47 -0700
committerEric Wong <normalperson@yhbt.net>2009-10-11 16:55:58 -0700
commitdf204a05d3a5bda8f716fa9f51be464fa59a3af1 (patch)
tree1391300d9dfe0a01a5f38958e3cf8c4c30c2fa0a /lib/rainbows/base.rb
parent4f8ae9abbb985a4091acbb7f57fb7f88fa2d43ba (diff)
downloadrainbows-df204a05d3a5bda8f716fa9f51be464fa59a3af1.tar.gz
The process-based heartbeat continues, but we no longer time
threads out just because a client is idle for any reason (for
now).
Diffstat (limited to 'lib/rainbows/base.rb')
-rw-r--r--lib/rainbows/base.rb26
1 files changed, 22 insertions, 4 deletions
diff --git a/lib/rainbows/base.rb b/lib/rainbows/base.rb
index 2da6d41..8a38117 100644
--- a/lib/rainbows/base.rb
+++ b/lib/rainbows/base.rb
@@ -2,7 +2,8 @@
 
 module Rainbows
 
-  # base class for Rainbows concurrency models
+  # base class for Rainbows concurrency models, this is currently
+  # used by ThreadSpawn and ThreadPool models
   module Base
 
     include Unicorn
@@ -41,10 +42,10 @@ module Rainbows
       buf = client.readpartial(CHUNK_SIZE)
       hp = HttpParser.new
       env = {}
+      alive = true
       remote_addr = TCPSocket === client ? client.peeraddr.last : LOCALHOST
 
       begin # loop
-        Thread.current[:t] = Time.now
         while ! hp.headers(env, buf)
           buf << client.readpartial(CHUNK_SIZE)
         end
@@ -61,9 +62,10 @@ module Rainbows
           response = app.call(env)
         end
 
-        out = [ hp.keepalive? ? CONN_ALIVE : CONN_CLOSE ] if hp.headers?
+        alive = hp.keepalive? && ! Thread.current[:quit]
+        out = [ alive ? CONN_ALIVE : CONN_CLOSE ] if hp.headers?
         HttpResponse.write(client, response, out)
-      end while hp.keepalive? and hp.reset.nil? and env.clear
+      end while alive and hp.reset.nil? and env.clear
       client.close
     # if we get any error, try to write something back to the client
     # assuming we haven't closed the socket, but don't get hung up
@@ -79,6 +81,22 @@ module Rainbows
       logger.error e.backtrace.join("\n")
     end
 
+    def join_threads(threads)
+      logger.info "Joining threads..."
+      threads.each { |thr| thr[:quit] = true }
+      t0 = Time.now
+      timeleft = timeout * 2.0
+      m = 0
+      while (nr = threads.count { |thr| thr.alive? }) > 0 && timeleft > 0
+        threads.each { |thr|
+          worker.tmp.chmod(m = 0 == m ? 1 : 0)
+          thr.join(1)
+          break if (timeleft -= (Time.now - t0)) < 0
+        }
+      end
+      logger.info "Done joining threads. #{nr} left running"
+    end
+
     def self.included(klass)
       klass.const_set :LISTENERS, HttpServer::LISTENERS
     end