move http.WaitForSocketCondition to utils
authorGuido Trotter <ultrotter@google.com>
Tue, 16 Mar 2010 13:59:25 +0000 (13:59 +0000)
committerGuido Trotter <ultrotter@google.com>
Thu, 18 Mar 2010 11:39:01 +0000 (11:39 +0000)
Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Michael Hanselmann <hansmi@google.com>

lib/http/__init__.py
lib/http/client.py
lib/utils.py

index 1d32e64..d0db1ce 100644 (file)
@@ -311,44 +311,6 @@ class HttpJsonConverter: # pylint: disable-msg=W0232
     return serializer.LoadJson(data)
 
 
-def WaitForSocketCondition(sock, event, timeout):
-  """Waits for a condition to occur on the socket.
-
-  @type sock: socket
-  @param sock: Wait for events on this socket
-  @type event: int
-  @param event: ORed condition (see select module)
-  @type timeout: float or None
-  @param timeout: Timeout in seconds
-  @rtype: int or None
-  @return: None for timeout, otherwise occured conditions
-
-  """
-  check = (event | select.POLLPRI |
-           select.POLLNVAL | select.POLLHUP | select.POLLERR)
-
-  if timeout is not None:
-    # Poller object expects milliseconds
-    timeout *= 1000
-
-  poller = select.poll()
-  poller.register(sock, event)
-  try:
-    while True:
-      # TODO: If the main thread receives a signal and we have no timeout, we
-      # could wait forever. This should check a global "quit" flag or
-      # something every so often.
-      io_events = poller.poll(timeout)
-      if not io_events:
-        # Timeout
-        return None
-      for (_, evcond) in io_events:
-        if evcond & check:
-          return evcond
-  finally:
-    poller.unregister(sock)
-
-
 def SocketOperation(sock, op, arg1, timeout):
   """Wrapper around socket functions.
 
@@ -399,7 +361,7 @@ def SocketOperation(sock, op, arg1, timeout):
       else:
         wait_for_event = event_poll
 
-      event = WaitForSocketCondition(sock, wait_for_event, timeout)
+      event = utils.WaitForSocketCondition(sock, wait_for_event, timeout)
       if event is None:
         raise HttpSocketTimeout()
 
index ba27d1a..6b70a04 100644 (file)
@@ -37,6 +37,7 @@ import threading
 
 from ganeti import workerpool
 from ganeti import http
+from ganeti import utils
 
 
 HTTP_CLIENT_THREADS = 10
@@ -249,8 +250,8 @@ class HttpClientRequestExecutor(http.HttpBase):
 
     if not connected:
       # Wait for connection
-      event = http.WaitForSocketCondition(self.sock, select.POLLOUT,
-                                          self.CONNECT_TIMEOUT)
+      event = utils.WaitForSocketCondition(self.sock, select.POLLOUT,
+                                           self.CONNECT_TIMEOUT)
       if event is None:
         raise http.HttpError("Timeout while connecting to server")
 
index edc92b4..7f2c861 100644 (file)
@@ -1497,6 +1497,44 @@ except NameError:
     return False
 
 
+def WaitForSocketCondition(sock, event, timeout):
+  """Waits for a condition to occur on the socket.
+
+  @type sock: socket
+  @param sock: Wait for events on this socket
+  @type event: int
+  @param event: ORed condition (see select module)
+  @type timeout: float or None
+  @param timeout: Timeout in seconds
+  @rtype: int or None
+  @return: None for timeout, otherwise occured conditions
+
+  """
+  check = (event | select.POLLPRI |
+           select.POLLNVAL | select.POLLHUP | select.POLLERR)
+
+  if timeout is not None:
+    # Poller object expects milliseconds
+    timeout *= 1000
+
+  poller = select.poll()
+  poller.register(sock, event)
+  try:
+    while True:
+      # TODO: If the main thread receives a signal and we have no timeout, we
+      # could wait forever. This should check a global "quit" flag or
+      # something every so often.
+      io_events = poller.poll(timeout)
+      if not io_events:
+        # Timeout
+        return None
+      for (_, evcond) in io_events:
+        if evcond & check:
+          return evcond
+  finally:
+    poller.unregister(sock)
+
+
 def partition(seq, pred=bool): # # pylint: disable-msg=W0622
   "Partition a list in two, based on the given predicate"
   return (list(itertools.ifilter(pred, seq)),