[CalendarServer-changes] [9835] CalendarServer/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Sep 21 23:23:03 PDT 2012


Revision: 9835
          http://trac.calendarserver.org//changeset/9835
Author:   glyph at apple.com
Date:     2012-09-21 23:23:03 -0700 (Fri, 21 Sep 2012)
Log Message:
-----------
Make _SubprocessSocket non-blocking.

Modified Paths:
--------------
    CalendarServer/trunk/twext/internet/sendfdport.py
    CalendarServer/trunk/twext/internet/test/test_sendfdport.py

Property Changed:
----------------
    CalendarServer/trunk/

Modified: CalendarServer/trunk/twext/internet/sendfdport.py
===================================================================
--- CalendarServer/trunk/twext/internet/sendfdport.py	2012-09-22 05:48:32 UTC (rev 9834)
+++ CalendarServer/trunk/twext/internet/sendfdport.py	2012-09-22 06:23:03 UTC (rev 9835)
@@ -192,11 +192,15 @@
         @param description: some text to identify to the subprocess's
             L{InheritedPort} what type of transport to create for this socket.
         """
-        
-        # We want None to sort after 0 and before 1, so coerce to 0.5 - this allows the master
-        # to first schedule all child process that are up but not yet busy ahead of those that
-        # are still starting up.
-        self._subprocessSockets.sort(key=lambda conn: 0.5 if conn.status is None else conn.status)
+        # We want None to sort after 0 and before 1, so coerce to 0.5 - this
+        # allows the master to first schedule all child process that are up but
+        # not yet busy ahead of those that are still starting up.
+        def sortKey(conn):
+            if conn.status is None:
+                return 0.5
+            else:
+                return conn.status
+        self._subprocessSockets.sort(key=sortKey)
         selectedSocket = self._subprocessSockets[0]
         selectedSocket.sendSocketToPeer(skt, description)
         # XXX Maybe want to send along 'description' or 'skt' or some
@@ -223,6 +227,8 @@
             C{spawnProcess()}, then close it.
         """
         i, o = socketpair(AF_UNIX, SOCK_DGRAM)
+        i.setblocking(False)
+        o.setblocking(False)
         a = _SubprocessSocket(self, o)
         self._subprocessSockets.append(a)
         if self._isDispatching:
@@ -279,9 +285,6 @@
         else:
             try:
                 skt = fromfd(fd, getsockfam(fd), SOCK_STREAM)
-                # XXX it could be AF_UNIX, I guess?  or even something else?
-                # should this be on the transportFactory's side of things?
-
                 close(fd)       # fromfd() calls dup()
                 try:
                     peeraddr = skt.getpeername()

Modified: CalendarServer/trunk/twext/internet/test/test_sendfdport.py
===================================================================
--- CalendarServer/trunk/twext/internet/test/test_sendfdport.py	2012-09-22 05:48:32 UTC (rev 9834)
+++ CalendarServer/trunk/twext/internet/test/test_sendfdport.py	2012-09-22 06:23:03 UTC (rev 9835)
@@ -19,6 +19,9 @@
 Tests for L{twext.internet.sendfdport}.
 """
 
+import os
+import fcntl
+
 from twext.internet.sendfdport import InheritedSocketDispatcher,\
     _SubprocessSocket
 from twext.web2.metafd import ConnectionLimiter
@@ -41,11 +44,43 @@
 
 
 
+def isNonBlocking(skt):
+    """
+    Determine if the given socket is blocking or not.
+
+    @param skt: a socket.
+    @type skt: L{socket.socket}
+
+    @return: L{True} if the socket is non-blocking, L{False} if the socket is
+        blocking.
+    @rtype: L{bool}
+    """
+    return bool(fcntl.fcntl(skt.fileno(), fcntl.F_GETFL) & os.O_NONBLOCK)
+
+
+
 class InheritedSocketDispatcherTests(TestCase):
     """
     Inherited socket dispatcher tests.
     """
 
+    def test_nonBlocking(self):
+        """
+        Creating a L{_SubprocessSocket} via
+        L{InheritedSocketDispatcher.addSocket} results in a non-blocking
+        L{socket.socket} object being assigned to its C{skt} attribute, as well
+        as a non-blocking L{socket.socket} object being returned.
+        """
+        dispatcher = InheritedSocketDispatcher(None)
+        dispatcher.startDispatching()
+        reactor = ReaderAdder()
+        dispatcher.reactor = reactor
+        inputSocket = dispatcher.addSocket()
+        outputSocket = reactor.readers[-1]
+        self.assertTrue(isNonBlocking(inputSocket), "Input is blocking.")
+        self.assertTrue(isNonBlocking(outputSocket), "Output is blocking.")
+
+
     def test_addAfterStart(self):
         """
         Adding a socket to an L{InheritedSocketDispatcher} after it has already
@@ -64,7 +99,7 @@
         Make sure InheritedSocketDispatcher.sendFileDescriptor sorts sockets with status None
         higher than those with int status values.
         """
-        
+
         self.patch(_SubprocessSocket, 'sendSocketToPeer', lambda x, y, z:None)
         dispatcher = InheritedSocketDispatcher(ConnectionLimiter(2, 20))
         dispatcher.addSocket()
@@ -72,39 +107,39 @@
         dispatcher.addSocket()
 
         sockets = dispatcher._subprocessSockets[:]
-        
+
         # Check that 0 is preferred over None
         sockets[0].status = 0
         sockets[1].status = 1
         sockets[2].status = None
-        
+
         dispatcher.sendFileDescriptor(None, "")
-        
+
         self.assertEqual(sockets[0].status, 1)
         self.assertEqual(sockets[1].status, 1)
         self.assertEqual(sockets[2].status, None)
-        
+
         dispatcher.sendFileDescriptor(None, "")
-        
+
         self.assertEqual(sockets[0].status, 1)
         self.assertEqual(sockets[1].status, 1)
         self.assertEqual(sockets[2].status, 1)
 
-        # Check that after going to 1 and back to 0 that is still preferred over None 
+        # Check that after going to 1 and back to 0 that is still preferred over None
         sockets[0].status = 0
         sockets[1].status = 1
         sockets[2].status = None
-        
+
         dispatcher.sendFileDescriptor(None, "")
-        
+
         self.assertEqual(sockets[0].status, 1)
         self.assertEqual(sockets[1].status, 1)
         self.assertEqual(sockets[2].status, None)
-        
+
         sockets[1].status = 0
 
         dispatcher.sendFileDescriptor(None, "")
-        
+
         self.assertEqual(sockets[0].status, 1)
         self.assertEqual(sockets[1].status, 1)
         self.assertEqual(sockets[2].status, None)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120921/3f7c2f42/attachment-0001.html>


More information about the calendarserver-changes mailing list