From 6a7d389ed0bae2083b6608072fad34e64b3901d7 Mon Sep 17 00:00:00 2001 From: akr Date: Sun, 21 Sep 2014 17:06:05 +0000 Subject: * lib/drb/drb.rb: Support graceful shutdown. (DRbTCPSocket#initialize): Create a pipe for shutdown notification. (DRbTCPSocket#close): Invoke close_shutdown_pipe. (DRbTCPSocket#close_shutdown_pipe): New private method. (DRbTCPSocket#accept): Use accept_or_shutdown. (DRbTCPSocket#accept_or_shutdown): New private method which returns nil on shutdown. (DRbServer#stop_service): Use shutdown instead of Thread#kill. (DRbServer#run): Break infinite loop when main_loop returns nil. (DRbServer#main_loop): @protocol.accept may return nil. * lib/drb/ssl.rb: Follow above change. * lib/drb/unix.rb: Ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@47678 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- ChangeLog | 17 +++++++++++++++++ lib/drb/drb.rb | 45 ++++++++++++++++++++++++++++++++++++++++----- lib/drb/ssl.rb | 3 ++- lib/drb/unix.rb | 4 +++- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 471d0ab608..cf44890b46 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,20 @@ +Mon Sep 22 02:04:25 2014 Tanaka Akira + + * lib/drb/drb.rb: Support graceful shutdown. + (DRbTCPSocket#initialize): Create a pipe for shutdown notification. + (DRbTCPSocket#close): Invoke close_shutdown_pipe. + (DRbTCPSocket#close_shutdown_pipe): New private method. + (DRbTCPSocket#accept): Use accept_or_shutdown. + (DRbTCPSocket#accept_or_shutdown): New private method which returns + nil on shutdown. + (DRbServer#stop_service): Use shutdown instead of Thread#kill. + (DRbServer#run): Break infinite loop when main_loop returns nil. + (DRbServer#main_loop): @protocol.accept may return nil. + + * lib/drb/ssl.rb: Follow above change. + + * lib/drb/unix.rb: Ditto. + Sun Sep 21 13:54:36 2014 Masaki Matsushita * time.c: raise exception when minutes of utc_offset is out of 00-59. diff --git a/lib/drb/drb.rb b/lib/drb/drb.rb index 26f39b789f..ab352afa1d 100644 --- a/lib/drb/drb.rb +++ b/lib/drb/drb.rb @@ -905,6 +905,7 @@ module DRb @acl = config[:tcp_acl] @msg = DRbMessage.new(config) set_sockopt(@socket) + @shutdown_pipe_r, @shutdown_pipe_w = IO.pipe end # Get the URI that we are connected to. @@ -952,14 +953,28 @@ module DRb @socket.close @socket = nil end + close_shutdown_pipe end + def close_shutdown_pipe + if @shutdown_pipe_r && !@shutdown_pipe_r.closed? + @shutdown_pipe_r.close + @shutdown_pipe_r = nil + end + if @shutdown_pipe_w && !@shutdown_pipe_w.closed? + @shutdown_pipe_w.close + @shutdown_pipe_w = nil + end + end + private :close_shutdown_pipe + # On the server side, for an instance returned by #open_server, # accept a client connection and return a new instance to handle # the server's side of this client-server session. def accept while true - s = @socket.accept + s = accept_or_shutdown + return nil unless s break if (@acl ? @acl.allow_socket?(s) : true) s.close end @@ -971,6 +986,20 @@ module DRb self.class.new(uri, s, @config) end + def accept_or_shutdown + readables, = IO.select([@socket, @shutdown_pipe_r]) + if readables.include? @shutdown_pipe_r + return nil + end + @socket.accept + end + private :accept_or_shutdown + + # Graceful shutdown + def shutdown + @shutdown_pipe_w.close if @shutdown_pipe_w && !@shutdown_pipe_w.closed? + end + # Check to see if this connection is alive. def alive? return false unless @socket @@ -1438,7 +1467,12 @@ module DRb if Thread.current['DRb'] && Thread.current['DRb']['server'] == self Thread.current['DRb']['stop_service'] = true else - @thread.kill.join + if @protocol.respond_to? :shutdown + @protocol.shutdown + else + @thread.kill # xxx: Thread#kill + end + @thread.join end end @@ -1463,8 +1497,7 @@ module DRb def run Thread.start do begin - while true - main_loop + while main_loop end ensure @protocol.close if @protocol @@ -1607,7 +1640,9 @@ module DRb # returning responses, until the client closes the connection # or a local method call fails. def main_loop - Thread.start(@protocol.accept) do |client| + client0 = @protocol.accept + return nil if !client0 + Thread.start(client0) do |client| @grp.add Thread.current Thread.current['DRb'] = { 'client' => client , 'server' => self } diff --git a/lib/drb/ssl.rb b/lib/drb/ssl.rb index dd4df4e129..efd8271a78 100644 --- a/lib/drb/ssl.rb +++ b/lib/drb/ssl.rb @@ -322,7 +322,8 @@ module DRb def accept # :nodoc: begin while true - soc = @socket.accept + soc = accept_or_shutdown + return nil unless soc break if (@acl ? @acl.allow_socket?(soc) : true) soc.close end diff --git a/lib/drb/unix.rb b/lib/drb/unix.rb index 4d245780a5..3fb8d0ecce 100644 --- a/lib/drb/unix.rb +++ b/lib/drb/unix.rb @@ -98,10 +98,12 @@ module DRb @socket.close File.unlink(path) if @server_mode @socket = nil + close_shutdown_pipe end def accept - s = @socket.accept + s = accept_or_shutdown + return nil unless s self.class.new(nil, s, @config) end -- cgit v1.2.3