X-Git-Url: http://www.dolda2000.com/gitweb/?a=blobdiff_plain;f=python%2Fashd-wsgi;h=e43dbc0c04f11f3e6d665cff39ceb2c7ea1c63a0;hb=3e11d7ede84e52974e41542ed60899ff340088d7;hp=74624929c84ea74d4e3452c3467a85c767485b3f;hpb=09c82f9c7bc563c081425141853e6ff8e402e358;p=ashd.git diff --git a/python/ashd-wsgi b/python/ashd-wsgi index 7462492..e43dbc0 100755 --- a/python/ashd-wsgi +++ b/python/ashd-wsgi @@ -1,13 +1,14 @@ #!/usr/bin/python -import sys, os, getopt, threading +import sys, os, getopt, threading, time import ashd.proto, ashd.util def usage(out): - out.write("usage: ashd-wsgi [-hA] [-p MODPATH] HANDLER-MODULE [ARGS...]\n") + out.write("usage: ashd-wsgi [-hA] [-p MODPATH] [-l REQLIMIT] HANDLER-MODULE [ARGS...]\n") +reqlimit = 0 modwsgi_compat = False -opts, args = getopt.getopt(sys.argv[1:], "+hAp:") +opts, args = getopt.getopt(sys.argv[1:], "+hAp:l:") for o, a in opts: if o == "-h": usage(sys.stdout) @@ -16,6 +17,8 @@ for o, a in opts: sys.path.insert(0, a) elif o == "-A": modwsgi_compat = True + elif o == "-l": + reqlimit = int(a) if len(args) < 1: usage(sys.stderr) sys.exit(1) @@ -36,6 +39,10 @@ else: sys.exit(1) handler = handlermod.application +class closed(IOError): + def __init__(self): + super(closed, self).__init__("The client has closed the connection.") + cwd = os.getcwd() def absolutify(path): if path[0] != '/': @@ -49,22 +56,22 @@ def unquoteurl(url): c = url[i] i += 1 if c == '%': - if len(url) > i + 2: + if len(url) >= i + 2: c = 0 if '0' <= url[i] <= '9': c |= (ord(url[i]) - ord('0')) << 4 elif 'a' <= url[i] <= 'f': - c |= (ord(url[i]) - ord('a')) << 4 + c |= (ord(url[i]) - ord('a') + 10) << 4 elif 'A' <= url[i] <= 'F': - c |= (ord(url[i]) - ord('A')) << 4 + c |= (ord(url[i]) - ord('A') + 10) << 4 else: raise ValueError("Illegal URL escape character") if '0' <= url[i + 1] <= '9': c |= ord(url[i + 1]) - ord('0') elif 'a' <= url[i + 1] <= 'f': - c |= ord(url[i + 1]) - ord('a') + c |= ord(url[i + 1]) - ord('a') + 10 elif 'A' <= url[i + 1] <= 'F': - c |= ord(url[i + 1]) - ord('A') + c |= ord(url[i + 1]) - ord('A') + 10 else: raise ValueError("Illegal URL escape character") buf += chr(c) @@ -122,17 +129,23 @@ def dowsgi(req): raise Exception, "Trying to write data before starting response." status, headers = resp respsent[:] = [True] - req.sk.write("HTTP/1.1 %s\n" % status) - for nm, val in headers: - req.sk.write("%s: %s\n" % (nm, val)) - req.sk.write("\n") + try: + req.sk.write("HTTP/1.1 %s\n" % status) + for nm, val in headers: + req.sk.write("%s: %s\n" % (nm, val)) + req.sk.write("\n") + except IOError: + raise closed() def write(data): if not data: return flushreq() - req.sk.write(data) - req.sk.flush() + try: + req.sk.write(data) + req.sk.flush() + except IOError: + raise closed() def startreq(status, headers, exc_info = None): if resp: @@ -149,22 +162,48 @@ def dowsgi(req): respiter = handler(env, startreq) try: - for data in respiter: - write(data) - if resp: - flushreq() + try: + for data in respiter: + write(data) + if resp: + flushreq() + except closed: + pass finally: if hasattr(respiter, "close"): respiter.close() +flightlock = threading.Condition() +inflight = 0 + class reqthread(threading.Thread): def __init__(self, req): super(reqthread, self).__init__(name = "Request handler") self.req = req.dup() def run(self): + global inflight try: - dowsgi(self.req) + flightlock.acquire() + try: + if reqlimit != 0: + start = time.time() + while inflight >= reqlimit: + flightlock.wait(10) + if time.time() - start > 10: + os.abort() + inflight += 1 + finally: + flightlock.release() + try: + dowsgi(self.req) + finally: + flightlock.acquire() + try: + inflight -= 1 + flightlock.notify() + finally: + flightlock.release() finally: self.req.close()