about summary refs log tree commit homepage
path: root/lib/rainbows/read_timeout.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows/read_timeout.rb')
-rw-r--r--lib/rainbows/read_timeout.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/rainbows/read_timeout.rb b/lib/rainbows/read_timeout.rb
new file mode 100644
index 0000000..d8245bd
--- /dev/null
+++ b/lib/rainbows/read_timeout.rb
@@ -0,0 +1,28 @@
+# -*- encoding: binary -*-
+# :enddoc:
+module Rainbows::ReadTimeout
+  G = Rainbows::G # :nodoc:
+
+  def wait_readable
+    IO.select([self], nil, nil, G.kato)
+  end
+
+  # used for reading headers (respecting keepalive_timeout)
+  def read_timeout(buf = "")
+    expire = nil
+    begin
+      case rv = kgio_tryread(16384, buf)
+      when :wait_readable
+        now = Time.now.to_f
+        if expire
+          now > expire and return
+        else
+          expire = now + G.kato
+        end
+        wait_readable
+      else
+        return rv
+      end
+    end while true
+  end
+end