From c50b69ddf0f1305bb39ed812d084f59db6dd9897 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 1 Dec 2009 22:39:38 -0800 Subject: more consistent code for worker timeout/exits We now correctly exit!(2) if our master can't kill us. --- lib/rainbows.rb | 4 +++- lib/rainbows/base.rb | 9 +++++---- lib/rainbows/revactor.rb | 4 +--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/rainbows.rb b/lib/rainbows.rb index c8f77c0..9e07185 100644 --- a/lib/rainbows.rb +++ b/lib/rainbows.rb @@ -8,14 +8,16 @@ module Rainbows # global vars because class/instance variables are confusing me :< # this struct is only accessed inside workers and thus private to each # G.cur may not be used in the network concurrency model - class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp) + class State < Struct.new(:alive,:m,:cur,:kato,:server,:tmp,:expire) def tick tmp.chmod(self.m = m == 0 ? 1 : 0) + exit!(2) if expire && Time.now >= expire alive && server.master_pid == Process.ppid or quit! end def quit! self.alive = false + self.expire ||= Time.now + (server.timeout * 2.0) server.class.const_get(:LISTENERS).map! { |s| s.close rescue nil } false end diff --git a/lib/rainbows/base.rb b/lib/rainbows/base.rb index 3336c3b..424b43b 100644 --- a/lib/rainbows/base.rb +++ b/lib/rainbows/base.rb @@ -70,10 +70,11 @@ module Rainbows end def join_threads(threads) - expire = Time.now + (timeout * 2.0) - until threads.empty? || Time.now >= expire - threads.delete_if { |thr| thr.alive? ? thr.join(0.01) : true } - end + G.quit! + threads.delete_if do |thr| + G.tick + thr.alive? ? thr.join(0.01) : true + end until threads.empty? end def self.included(klass) diff --git a/lib/rainbows/revactor.rb b/lib/rainbows/revactor.rb index 125e148..b647d48 100644 --- a/lib/rainbows/revactor.rb +++ b/lib/rainbows/revactor.rb @@ -116,9 +116,7 @@ module Rainbows end end - Actor.sleep 1 while G.tick - expire = Time.now + timeout * 2.0 - Actor.sleep 1 while nr > 0 && Time.now < expire + Actor.sleep 1 while G.tick || nr > 0 rescue Errno::EMFILE => e end -- cgit v1.2.3-24-ge0c7