|
|
|
@ -52,7 +52,7 @@ with open(__file__) as f: |
|
|
|
|
_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""" |
|
|
|
|
for i in range(1025, 32767): |
|
|
|
|
if len(pool) > 100: break |
|
|
|
@ -60,11 +60,13 @@ def refill_pool(max_timeout): |
|
|
|
|
age = time.time() - in_use[i] |
|
|
|
|
if age < max_timeout: |
|
|
|
|
continue |
|
|
|
|
req.log_message("kill old request %d" % i) |
|
|
|
|
del in_use[i] |
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
|
|
|
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
|
|
|
|
try: |
|
|
|
|
s.bind(('localhost', i)) |
|
|
|
|
req.log_message("found available port %d" % i) |
|
|
|
|
pool.append(i) |
|
|
|
|
except: |
|
|
|
|
pass # we really don't care about failures |
|
|
|
@ -72,13 +74,14 @@ def refill_pool(max_timeout): |
|
|
|
|
s.close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def allocate_port(): |
|
|
|
|
def allocate_port(req): |
|
|
|
|
global pool |
|
|
|
|
global in_use |
|
|
|
|
max_timeout = 600 |
|
|
|
|
while not pool: |
|
|
|
|
refill_pool(max_timeout) |
|
|
|
|
refill_pool(max_timeout, req) |
|
|
|
|
if not pool: |
|
|
|
|
req.log_message("failed to find ports: retrying soon") |
|
|
|
|
time.sleep(1) |
|
|
|
|
max_timeout /= 2 |
|
|
|
|
port = pool[0] |
|
|
|
@ -100,7 +103,7 @@ class Handler(BaseHTTPServer.BaseHTTPRequestHandler): |
|
|
|
|
self.send_response(200) |
|
|
|
|
self.send_header('Content-Type', 'text/plain') |
|
|
|
|
self.end_headers() |
|
|
|
|
p = allocate_port() |
|
|
|
|
p = allocate_port(self) |
|
|
|
|
self.log_message('allocated port %d' % p) |
|
|
|
|
self.wfile.write('%d' % p) |
|
|
|
|
elif self.path[0:6] == '/drop/': |
|
|
|
|