Code and docstring style fixes
[ganeti-local] / lib / http / server.py
index b74eb36..d7e374c 100644 (file)
@@ -30,10 +30,8 @@ import select
 import socket
 import time
 import signal
+import asyncore
 
-from ganeti import constants
-from ganeti import serializer
-from ganeti import utils
 from ganeti import http
 
 
@@ -418,7 +416,7 @@ class HttpServerRequestExecutor(object):
     """
     return self.error_message_format % values
 
-class HttpServer(http.HttpBase):
+class HttpServer(http.HttpBase, asyncore.dispatcher):
   """Generic HTTP server class
 
   Users of this class must subclass it and override the HandleRequest function.
@@ -448,6 +446,7 @@ class HttpServer(http.HttpBase):
 
     """
     http.HttpBase.__init__(self)
+    asyncore.dispatcher.__init__(self)
 
     if request_executor_class is None:
       self.request_executor = HttpServerRequestExecutor
@@ -464,8 +463,8 @@ class HttpServer(http.HttpBase):
     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
 
     self._children = []
-
-    mainloop.RegisterIO(self, self.socket.fileno(), select.POLLIN)
+    self.set_socket(self.socket)
+    self.accepting = True
     mainloop.RegisterSignal(self)
 
   def Start(self):
@@ -475,9 +474,8 @@ class HttpServer(http.HttpBase):
   def Stop(self):
     self.socket.close()
 
-  def OnIO(self, fd, condition):
-    if condition & select.POLLIN:
-      self._IncomingConnection()
+  def handle_accept(self):
+    self._IncomingConnection()
 
   def OnSignal(self, signum):
     if signum == signal.SIGCHLD:
@@ -498,7 +496,7 @@ class HttpServer(http.HttpBase):
           # As soon as too many children run, we'll not respond to new
           # requests. The real solution would be to add a timeout for children
           # and killing them after some time.
-          pid, status = os.waitpid(0, 0)
+          pid, _ = os.waitpid(0, 0)
         except os.error:
           pid = None
         if pid and pid in self._children:
@@ -524,6 +522,16 @@ class HttpServer(http.HttpBase):
     if pid == 0:
       # Child process
       try:
+        # The client shouldn't keep the listening socket open. If the parent
+        # process is restarted, it would fail when there's already something
+        # listening (in this case its own child from a previous run) on the
+        # same port.
+        try:
+          self.socket.close()
+        except socket.error:
+          pass
+        self.socket = None
+
         self.request_executor(self, connection, client_addr)
       except Exception:
         logging.exception("Error while handling request from %s:%s",
@@ -536,14 +544,14 @@ class HttpServer(http.HttpBase):
   def PreHandleRequest(self, req):
     """Called before handling a request.
 
-    Can be overriden by a subclass.
+    Can be overridden by a subclass.
 
     """
 
   def HandleRequest(self, req):
     """Handles a request.
 
-    Must be overriden by subclass.
+    Must be overridden by subclass.
 
     """
     raise NotImplementedError()