From 40c22b3f0a2ace6e4b51a39624ac5ed5ee3d0162 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Mon, 27 Dec 2010 03:56:30 +0000 Subject: fiber/*: more efficient keepalive_timeout expiry We can use the same interface as Rainbows::Fiber.sleep to avoid blindly waking up readers at ever scheduler invocation. --- lib/rainbows/fiber/base.rb | 13 ++++++------- lib/rainbows/fiber/io.rb | 4 ++-- lib/rainbows/fiber/io/methods.rb | 6 ++++++ 3 files changed, 14 insertions(+), 9 deletions(-) (limited to 'lib/rainbows/fiber') diff --git a/lib/rainbows/fiber/base.rb b/lib/rainbows/fiber/base.rb index ccfda96..b693451 100644 --- a/lib/rainbows/fiber/base.rb +++ b/lib/rainbows/fiber/base.rb @@ -18,11 +18,10 @@ module Rainbows::Fiber::Base # for one second (returned by the schedule_sleepers method) which # will cause it. def schedule(&block) - ret = begin + begin G.tick - RD.compact.each { |c| c.f.resume } # attempt to time out idle clients t = schedule_sleepers - select(RD.compact.concat(LISTENERS), WR.compact, nil, t) or return + ret = select(RD.compact.concat(LISTENERS), WR.compact, nil, t) rescue Errno::EINTR retry rescue Errno::EBADF, TypeError @@ -30,15 +29,15 @@ module Rainbows::Fiber::Base raise end or return - # active writers first, then _all_ readers for keepalive timeout - ret[1].concat(RD.compact).each { |c| c.f.resume } + # active writers first, then readers + ret[1].concat(RD.compact & ret[0]).each { |c| c.f.resume } # accept is an expensive syscall, filter out listeners we don't want (ret[0] & LISTENERS).each(&block) end - # wakes up any sleepers that need to be woken and - # returns an interval to IO.select on + # wakes up any sleepers or keepalive-timeout violators that need to be + # woken and returns an interval to IO.select on def schedule_sleepers max = nil now = Time.now diff --git a/lib/rainbows/fiber/io.rb b/lib/rainbows/fiber/io.rb index e96f4de..94996ee 100644 --- a/lib/rainbows/fiber/io.rb +++ b/lib/rainbows/fiber/io.rb @@ -82,7 +82,7 @@ class Rainbows::Fiber::IO case rv = @to_io.kgio_tryread(16384, buf) when :wait_readable return if expire && expire < Time.now - expire ||= Time.now + G.kato + expire ||= read_expire kgio_wait_readable else return rv @@ -93,7 +93,7 @@ class Rainbows::Fiber::IO return @to_io.read_nonblock(16384, buf) rescue Errno::EAGAIN return if expire && expire < Time.now - expire ||= Time.now + G.kato + expire ||= read_expire kgio_wait_readable end while true end diff --git a/lib/rainbows/fiber/io/methods.rb b/lib/rainbows/fiber/io/methods.rb index 6c4d44d..29c9854 100644 --- a/lib/rainbows/fiber/io/methods.rb +++ b/lib/rainbows/fiber/io/methods.rb @@ -8,8 +8,13 @@ module Rainbows::Fiber::IO::Methods RD = Rainbows::Fiber::RD WR = Rainbows::Fiber::WR + ZZ = Rainbows::Fiber::ZZ attr_accessor :f + def read_expire + ZZ[Fiber.current] = super + end + # for wrapping output response bodies def each(&block) if buf = kgio_read(16384) @@ -30,6 +35,7 @@ module Rainbows::Fiber::IO::Methods @f = Fiber.current RD[fd] = self Fiber.yield + ZZ.delete @f RD[fd] = nil end -- cgit v1.2.3-24-ge0c7