You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2008/06/18 03:20:22 UTC

svn commit: r669042 - in /incubator/thrift/trunk/lib/rb: lib/thrift/server/nonblockingserver.rb lib/thrift/transport.rb lib/thrift/transport/socket.rb lib/thrift/transport/unixsocket.rb spec/socket_spec.rb spec/unixsocket_spec.rb

Author: kclark
Date: Tue Jun 17 18:20:22 2008
New Revision: 669042

URL: http://svn.apache.org/viewvc?rev=669042&view=rev
Log:
rb: NonblockingServer: Use a select() loop in the acceptor thread

Using a select() loop with a timeout allows the acceptor thread to be shut down
cleanly under JRuby.

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/server/nonblockingserver.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
    incubator/thrift/trunk/lib/rb/lib/thrift/transport/unixsocket.rb
    incubator/thrift/trunk/lib/rb/spec/socket_spec.rb
    incubator/thrift/trunk/lib/rb/spec/unixsocket_spec.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/server/nonblockingserver.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/server/nonblockingserver.rb?rev=669042&r1=669041&r2=669042&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/server/nonblockingserver.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/server/nonblockingserver.rb Tue Jun 17 18:20:22 2008
@@ -25,14 +25,17 @@
 
       begin
         loop do
+          break if @serverTransport.closed?
+          rd, = select([@serverTransport], nil, nil, 0.1)
+          next if rd.nil?
           socket = @serverTransport.accept
           @logger.debug "Accepted socket: #{socket.inspect}"
           @io_manager.add_connection socket
         end
       rescue IOError => e
-        # we must be shutting down
-        @logger.info "#{self} is shutting down, goodbye"
       end
+      # we must be shutting down
+      @logger.info "#{self} is shutting down, goodbye"
     ensure
       @transport_semaphore.synchronize do
         @serverTransport.close

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb?rev=669042&r1=669041&r2=669042&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb Tue Jun 17 18:20:22 2008
@@ -62,6 +62,8 @@
     def accept; nil; end
 
     def close; nil; end
+
+    def closed?; nil; end
   end
   deprecate_class! :TServerTransport => ServerTransport
 

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb?rev=669042&r1=669041&r2=669042&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/socket.rb Tue Jun 17 18:20:22 2008
@@ -110,6 +110,12 @@
      @handle.close unless @handle.nil? or @handle.closed?
      @handle = nil
     end
+
+    def closed?
+      @handle.nil? or @handle.closed?
+    end
+
+    alias to_io handle
   end
   deprecate_class! :TServerSocket => ServerSocket
 end

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport/unixsocket.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport/unixsocket.rb?rev=669042&r1=669041&r2=669042&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport/unixsocket.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport/unixsocket.rb Tue Jun 17 18:20:22 2008
@@ -47,5 +47,11 @@
         File.delete(@path)
       end
     end
+
+    def closed?
+      @handle.nil? or @handle.closed?
+    end
+
+    alias to_io handle
   end
 end

Modified: incubator/thrift/trunk/lib/rb/spec/socket_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/socket_spec.rb?rev=669042&r1=669041&r2=669042&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/socket_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/socket_spec.rb Tue Jun 17 18:20:22 2008
@@ -69,5 +69,19 @@
     it "should return nil when accepting if there is no handle" do
       @socket.accept.should be_nil
     end
+
+    it "should return true for closed? when appropriate" do
+      handle = mock("TCPServer", :closed? => false)
+      TCPServer.stub!(:new).and_return(handle)
+      @socket.listen
+      @socket.should_not be_closed
+      handle.stub!(:close)
+      @socket.close
+      @socket.should be_closed
+      @socket.listen
+      @socket.should_not be_closed
+      handle.stub!(:closed?).and_return(true)
+      @socket.should be_closed
+    end
   end
 end

Modified: incubator/thrift/trunk/lib/rb/spec/unixsocket_spec.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/spec/unixsocket_spec.rb?rev=669042&r1=669041&r2=669042&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/spec/unixsocket_spec.rb (original)
+++ incubator/thrift/trunk/lib/rb/spec/unixsocket_spec.rb Tue Jun 17 18:20:22 2008
@@ -66,5 +66,20 @@
     it "should return nil when accepting if there is no handle" do
       @socket.accept.should be_nil
     end
+
+    it "should return true for closed? when appropriate" do
+      handle = mock("UNIXServer", :closed? => false)
+      UNIXServer.stub!(:new).and_return(handle)
+      File.stub!(:delete)
+      @socket.listen
+      @socket.should_not be_closed
+      handle.stub!(:close)
+      @socket.close
+      @socket.should be_closed
+      @socket.listen
+      @socket.should_not be_closed
+      handle.stub!(:closed?).and_return(true)
+      @socket.should be_closed
+    end
   end
 end