about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-27 13:10:57 -0800
committerEric Wong <normalperson@yhbt.net>2010-12-27 21:11:31 +0000
commit728496a31f34234b46d7025a23933aa06dd824f5 (patch)
tree3853385379df54b0f1d2d3927e2fa5817cbe2bf2
parent0766c1eb631190ee514a90e4d20a941f0a310054 (diff)
Blindly resuming fibers every second is a waste of cycles, we
can use the ZZ hash in regular FiberSpawn to resume expired
fibers on an as-needed basis.

While we're at it, merge the keepalive-timeout class into the
heartbeat, there's no reason to have separate timers and
classes here.
-rw-r--r--lib/rainbows/fiber/rev.rb1
-rw-r--r--lib/rainbows/fiber/rev/heartbeat.rb7
-rw-r--r--lib/rainbows/fiber/rev/kato.rb22
-rw-r--r--lib/rainbows/fiber/rev/methods.rb1
-rw-r--r--lib/rainbows/rev_fiber_spawn.rb2
5 files changed, 7 insertions, 26 deletions
diff --git a/lib/rainbows/fiber/rev.rb b/lib/rainbows/fiber/rev.rb
index be1b3d9..cf70367 100644
--- a/lib/rainbows/fiber/rev.rb
+++ b/lib/rainbows/fiber/rev.rb
@@ -11,7 +11,6 @@ require 'rainbows/fiber/io'
 
 module Rainbows::Fiber::Rev
   autoload :Heartbeat, 'rainbows/fiber/rev/heartbeat'
-  autoload :Kato, 'rainbows/fiber/rev/kato'
   autoload :Server, 'rainbows/fiber/rev/server'
   autoload :Sleeper, 'rainbows/fiber/rev/sleeper'
 end
diff --git a/lib/rainbows/fiber/rev/heartbeat.rb b/lib/rainbows/fiber/rev/heartbeat.rb
index 9411b4a..f9ef573 100644
--- a/lib/rainbows/fiber/rev/heartbeat.rb
+++ b/lib/rainbows/fiber/rev/heartbeat.rb
@@ -2,7 +2,14 @@
 # :enddoc:
 class Rainbows::Fiber::Rev::Heartbeat < Rev::TimerWatcher
   G = Rainbows::G
+
+  # ZZ gets populated by read_expire in rainbows/fiber/io/methods
+  ZZ = Rainbows::Fiber::ZZ
   def on_timer
     exit if (! G.tick && G.cur <= 0)
+    now = Time.now
+    fibs = []
+    ZZ.delete_if { |fib, time| now >= time ? fibs << fib : ! fib.alive? }
+    fibs.each { |fib| fib.resume if fib.alive? }
   end
 end
diff --git a/lib/rainbows/fiber/rev/kato.rb b/lib/rainbows/fiber/rev/kato.rb
deleted file mode 100644
index 056b6ef..0000000
--- a/lib/rainbows/fiber/rev/kato.rb
+++ /dev/null
@@ -1,22 +0,0 @@
-# -*- encoding: binary -*-
-# :enddoc:
-# keep-alive timeout class
-class Rainbows::Fiber::Rev::Kato < Rev::TimerWatcher
-  def initialize
-    @watch = []
-    super(1, true)
-  end
-
-  def <<(fiber)
-    @watch << fiber
-    enable unless enabled?
-  end
-
-  def on_timer
-    @watch.uniq!
-    while f = @watch.shift
-      f.resume if f.alive?
-    end
-    disable
-  end
-end
diff --git a/lib/rainbows/fiber/rev/methods.rb b/lib/rainbows/fiber/rev/methods.rb
index 4345bdb..4421fd3 100644
--- a/lib/rainbows/fiber/rev/methods.rb
+++ b/lib/rainbows/fiber/rev/methods.rb
@@ -31,7 +31,6 @@ module Rainbows::Fiber::Rev::Methods
   def kgio_wait_readable
     @r = Watcher.new(self, :r) unless defined?(@r)
     @r.enable unless @r.enabled?
-    KATO << Fiber.current
     Fiber.yield
     @r.disable
   end
diff --git a/lib/rainbows/rev_fiber_spawn.rb b/lib/rainbows/rev_fiber_spawn.rb
index 4338703..8d0d625 100644
--- a/lib/rainbows/rev_fiber_spawn.rb
+++ b/lib/rainbows/rev_fiber_spawn.rb
@@ -22,8 +22,6 @@ module Rainbows::RevFiberSpawn
     Rainbows::Fiber::Base.setup(Server, nil)
     Server.const_set(:APP, G.server.app)
     Heartbeat.new(1, true).attach(Rev::Loop.default)
-    kato = Kato.new.attach(Rev::Loop.default)
-    Rainbows::Fiber::Rev::Methods.const_set(:KATO, kato)
     LISTENERS.map! { |s| Server.new(s).attach(Rev::Loop.default) }
     Rev::Loop.default.run
   end