diff options
author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2019-07-24 14:14:50 +0100 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2019-08-06 11:21:31 +0100 |
commit | d40d7e43856f176c45cf515644b5f211c708e237 (patch) | |
tree | 4b0e8393ae5438790b802b28a630109bdf714c43 /bitbake/lib | |
parent | 94d5c61c9afd286e15f6ccd7b7b9e695bfcb0f97 (diff) | |
download | poky-d40d7e43856f176c45cf515644b5f211c708e237.tar.gz |
bitbake: hashserv: Use separate threads for answering requests and handling them
Experience with the prserv shows that having two threads, one accepting
and queueing connections and one handling the requests leads to much
more reliable behaviour than having everything in a single thread.
(Bitbake rev: a03d60671a53d9ff70e07cc42fe35f6f8776dac2)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib')
-rw-r--r-- | bitbake/lib/hashserv/__init__.py | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/bitbake/lib/hashserv/__init__.py b/bitbake/lib/hashserv/__init__.py index 544bc86b16..86aa7e9de6 100644 --- a/bitbake/lib/hashserv/__init__.py +++ b/bitbake/lib/hashserv/__init__.py | |||
@@ -10,6 +10,9 @@ import sqlite3 | |||
10 | import json | 10 | import json |
11 | import traceback | 11 | import traceback |
12 | import logging | 12 | import logging |
13 | import socketserver | ||
14 | import queue | ||
15 | import threading | ||
13 | from datetime import datetime | 16 | from datetime import datetime |
14 | 17 | ||
15 | logger = logging.getLogger('hashserv') | 18 | logger = logging.getLogger('hashserv') |
@@ -135,6 +138,41 @@ class HashEquivalenceServer(BaseHTTPRequestHandler): | |||
135 | self.send_error(400, explain=traceback.format_exc()) | 138 | self.send_error(400, explain=traceback.format_exc()) |
136 | return | 139 | return |
137 | 140 | ||
141 | class ThreadedHTTPServer(HTTPServer): | ||
142 | quit = False | ||
143 | |||
144 | def serve_forever(self): | ||
145 | self.requestqueue = queue.Queue() | ||
146 | self.handlerthread = threading.Thread(target=self.process_request_thread) | ||
147 | self.handlerthread.daemon = False | ||
148 | |||
149 | self.handlerthread.start() | ||
150 | super().serve_forever() | ||
151 | |||
152 | def process_request_thread(self): | ||
153 | while not self.quit: | ||
154 | try: | ||
155 | (request, client_address) = self.requestqueue.get(True) | ||
156 | except queue.Empty: | ||
157 | continue | ||
158 | if request is None: | ||
159 | continue | ||
160 | try: | ||
161 | self.finish_request(request, client_address) | ||
162 | except Exception: | ||
163 | self.handle_error(request, client_address) | ||
164 | finally: | ||
165 | self.shutdown_request(request) | ||
166 | |||
167 | def process_request(self, request, client_address): | ||
168 | self.requestqueue.put((request, client_address)) | ||
169 | |||
170 | def server_close(self): | ||
171 | super().server_close() | ||
172 | self.quit = True | ||
173 | self.requestqueue.put((None, None)) | ||
174 | self.handlerthread.join() | ||
175 | |||
138 | def create_server(addr, dbname, prefix=''): | 176 | def create_server(addr, dbname, prefix=''): |
139 | class Handler(HashEquivalenceServer): | 177 | class Handler(HashEquivalenceServer): |
140 | pass | 178 | pass |
@@ -171,4 +209,5 @@ def create_server(addr, dbname, prefix=''): | |||
171 | cursor.execute('CREATE INDEX IF NOT EXISTS outhash_lookup ON tasks_v2 (method, outhash)') | 209 | cursor.execute('CREATE INDEX IF NOT EXISTS outhash_lookup ON tasks_v2 (method, outhash)') |
172 | 210 | ||
173 | logger.info('Starting server on %s', addr) | 211 | logger.info('Starting server on %s', addr) |
174 | return HTTPServer(addr, Handler) | 212 | |
213 | return ThreadedHTTPServer(addr, Handler) | ||