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

better logging

parent 99b65f22
No related branches found
No related tags found
No related merge requests found
...@@ -52,7 +52,7 @@ with open(__file__) as f: ...@@ -52,7 +52,7 @@ with open(__file__) as f:
_MY_VERSION = hashlib.sha1(f.read()).hexdigest() _MY_VERSION = hashlib.sha1(f.read()).hexdigest()
def refill_pool(max_timeout): def refill_pool(max_timeout, req):
"""Scan for ports not marked for being in use""" """Scan for ports not marked for being in use"""
for i in range(1025, 32767): for i in range(1025, 32767):
if len(pool) > 100: break if len(pool) > 100: break
...@@ -60,11 +60,13 @@ def refill_pool(max_timeout): ...@@ -60,11 +60,13 @@ def refill_pool(max_timeout):
age = time.time() - in_use[i] age = time.time() - in_use[i]
if age < max_timeout: if age < max_timeout:
continue continue
req.log_message("kill old request %d" % i)
del in_use[i] del in_use[i]
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try: try:
s.bind(('localhost', i)) s.bind(('localhost', i))
req.log_message("found available port %d" % i)
pool.append(i) pool.append(i)
except: except:
pass # we really don't care about failures pass # we really don't care about failures
...@@ -72,13 +74,14 @@ def refill_pool(max_timeout): ...@@ -72,13 +74,14 @@ def refill_pool(max_timeout):
s.close() s.close()
def allocate_port(): def allocate_port(req):
global pool global pool
global in_use global in_use
max_timeout = 600 max_timeout = 600
while not pool: while not pool:
refill_pool(max_timeout) refill_pool(max_timeout, req)
if not pool: if not pool:
req.log_message("failed to find ports: retrying soon")
time.sleep(1) time.sleep(1)
max_timeout /= 2 max_timeout /= 2
port = pool[0] port = pool[0]
...@@ -100,7 +103,7 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler): ...@@ -100,7 +103,7 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
self.send_response(200) self.send_response(200)
self.send_header('Content-Type', 'text/plain') self.send_header('Content-Type', 'text/plain')
self.end_headers() self.end_headers()
p = allocate_port() p = allocate_port(self)
self.log_message('allocated port %d' % p) self.log_message('allocated port %d' % p)
self.wfile.write('%d' % p) self.wfile.write('%d' % p)
elif self.path[0:6] == '/drop/': elif self.path[0:6] == '/drop/':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment