[bitbake-devel,2/2] hashserv: Drop handler thread

Submitted by Richard Purdie on Sept. 10, 2019, 2:15 p.m. | Patch ID: 164818

Details

Message ID 20190910141555.6710-2-richard.purdie@linuxfoundation.org
State Master Next
Commit 30139ac9e239f222756bdbca60c237453fbb0afc
Headers show

Commit Message

Richard Purdie Sept. 10, 2019, 2:15 p.m.
With performance measurements we've proven the handler thread is slower than not
bothering with threading so drop it.

There was also a bug in this code where the database was being repeatedly being
reopened/closed.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/hashserv/__init__.py | 62 ++++------------------------------------
 1 file changed, 5 insertions(+), 57 deletions(-)

Patch hide | download patch | download mbox

diff --git a/lib/hashserv/__init__.py b/lib/hashserv/__init__.py
index eb03c32213..9d5722ab88 100644
--- a/lib/hashserv/__init__.py
+++ b/lib/hashserv/__init__.py
@@ -24,17 +24,8 @@  class HashEquivalenceServer(BaseHTTPRequestHandler):
     def log_message(self, f, *args):
         logger.debug(f, *args)
 
-    def opendb(self):
-        self.db = sqlite3.connect(self.dbname)
-        self.db.row_factory = sqlite3.Row
-        self.db.execute("PRAGMA synchronous = OFF;")
-        self.db.execute("PRAGMA journal_mode = MEMORY;")
-
     def do_GET(self):
         try:
-            if not self.db:
-                self.opendb()
-
             p = urllib.parse.urlparse(self.path)
 
             if p.path != self.prefix + '/v1/equivalent':
@@ -67,9 +58,6 @@  class HashEquivalenceServer(BaseHTTPRequestHandler):
 
     def do_POST(self):
         try:
-            if not self.db:
-                self.opendb()
-
             p = urllib.parse.urlparse(self.path)
 
             if p.path != self.prefix + '/v1/equivalent':
@@ -141,62 +129,22 @@  class HashEquivalenceServer(BaseHTTPRequestHandler):
             self.send_error(400, explain=traceback.format_exc())
             return
 
-class ThreadedHTTPServer(HTTPServer):
-    quit = False
-
-    def serve_forever(self):
-        self.requestqueue = queue.Queue()
-        self.handlerthread = threading.Thread(target=self.process_request_thread)
-        self.handlerthread.daemon = False
-
-        self.handlerthread.start()
-
-        signal.signal(signal.SIGTERM, self.sigterm_exception)
-        super().serve_forever()
-        os._exit(0)
-
-    def sigterm_exception(self, signum, stackframe):
-        self.server_close()
-        os._exit(0)
-
+class HashHTTPServer(HTTPServer):
     def server_bind(self):
         HTTPServer.server_bind(self)
         self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
 
-    def process_request_thread(self):
-        while not self.quit:
-            try:
-                (request, client_address) = self.requestqueue.get(True)
-            except queue.Empty:
-                continue
-            if request is None:
-                continue
-            try:
-                self.finish_request(request, client_address)
-            except Exception:
-                self.handle_error(request, client_address)
-            finally:
-                self.shutdown_request(request)
-        os._exit(0)
-
-    def process_request(self, request, client_address):
-        self.requestqueue.put((request, client_address))
-
-    def server_close(self):
-        super().server_close()
-        self.quit = True
-        self.requestqueue.put((None, None))
-        self.handlerthread.join()
-
 def create_server(addr, dbname, prefix=''):
     class Handler(HashEquivalenceServer):
         pass
 
     db = sqlite3.connect(dbname)
     db.row_factory = sqlite3.Row
+    db.execute("PRAGMA synchronous = OFF;")
+    db.execute("PRAGMA journal_mode = MEMORY;")
 
     Handler.prefix = prefix
-    Handler.db = None
+    Handler.db = db
     Handler.dbname = dbname
 
     with contextlib.closing(db.cursor()) as cursor:
@@ -223,7 +171,7 @@  def create_server(addr, dbname, prefix=''):
         cursor.execute('CREATE INDEX IF NOT EXISTS taskhash_lookup ON tasks_v2 (method, taskhash)')
         cursor.execute('CREATE INDEX IF NOT EXISTS outhash_lookup ON tasks_v2 (method, outhash)')
 
-    ret = ThreadedHTTPServer(addr, Handler)
+    ret = HashHTTPServer(addr, Handler)
 
     logger.info('Starting server on %s\n', ret.server_port)