Skip to content
Snippets Groups Projects
Commit b219338e authored by Craig Tiller's avatar Craig Tiller Committed by GitHub
Browse files

Merge pull request #10822 from ctiller/threaded_port_server

Threaded port_server
parents dc2e0843 ec495242
No related branches found
No related tags found
No related merge requests found
...@@ -33,18 +33,20 @@ ...@@ -33,18 +33,20 @@
from __future__ import print_function from __future__ import print_function
import argparse import argparse
from six.moves import BaseHTTPServer from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import hashlib import hashlib
import os import os
import socket import socket
import sys import sys
import time import time
from SocketServer import ThreadingMixIn
import threading
# increment this number whenever making a change to ensure that # increment this number whenever making a change to ensure that
# the changes are picked up by running CI servers # the changes are picked up by running CI servers
# note that all changes must be backwards compatible # note that all changes must be backwards compatible
_MY_VERSION = 9 _MY_VERSION = 11
if len(sys.argv) == 2 and sys.argv[1] == 'dump_version': if len(sys.argv) == 2 and sys.argv[1] == 'dump_version':
...@@ -68,6 +70,7 @@ print('port server running on port %d' % args.port) ...@@ -68,6 +70,7 @@ print('port server running on port %d' % args.port)
pool = [] pool = []
in_use = {} in_use = {}
mu = threading.Lock()
def refill_pool(max_timeout, req): def refill_pool(max_timeout, req):
...@@ -95,28 +98,33 @@ def refill_pool(max_timeout, req): ...@@ -95,28 +98,33 @@ def refill_pool(max_timeout, req):
def allocate_port(req): def allocate_port(req):
global pool global pool
global in_use global in_use
global mu
mu.acquire()
max_timeout = 600 max_timeout = 600
while not pool: while not pool:
refill_pool(max_timeout, req) refill_pool(max_timeout, req)
if not pool: if not pool:
req.log_message("failed to find ports: retrying soon") req.log_message("failed to find ports: retrying soon")
mu.release()
time.sleep(1) time.sleep(1)
mu.acquire()
max_timeout /= 2 max_timeout /= 2
port = pool[0] port = pool[0]
pool = pool[1:] pool = pool[1:]
in_use[port] = time.time() in_use[port] = time.time()
mu.release()
return port return port
keep_running = True keep_running = True
class Handler(BaseHTTPServer.BaseHTTPRequestHandler): class Handler(BaseHTTPRequestHandler):
def setup(self): def setup(self):
# If the client is unreachable for 5 seconds, close the connection # If the client is unreachable for 5 seconds, close the connection
self.timeout = 5 self.timeout = 5
BaseHTTPServer.BaseHTTPRequestHandler.setup(self) BaseHTTPRequestHandler.setup(self)
def do_GET(self): def do_GET(self):
global keep_running global keep_running
...@@ -158,12 +166,12 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -158,12 +166,12 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
elif self.path == '/quitquitquit': elif self.path == '/quitquitquit':
self.send_response(200) self.send_response(200)
self.end_headers() self.end_headers()
keep_running = False sys.exit(0)
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread"""
httpd = BaseHTTPServer.HTTPServer(('', args.port), Handler)
while keep_running:
httpd.handle_request()
sys.stderr.flush()
print('done') httpd = ThreadedHTTPServer(('', args.port), Handler)
httpd.serve_forever()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment