python: Added a simple, non-threaded handler.
[ashd.git] / python3 / ashd / serve.py
CommitLineData
46adc298
FT
1import sys, os, threading, time, logging, select
2from . import perf
552a70bf
FT
3
4log = logging.getLogger("ashd.serve")
5seq = 1
6seqlk = threading.Lock()
7
8def reqseq():
9 global seq
10 with seqlk:
11 s = seq
12 seq += 1
13 return s
14
552a70bf
FT
15class closed(IOError):
16 def __init__(self):
17 super().__init__("The client has closed the connection.")
18
46adc298
FT
19class reqthread(threading.Thread):
20 def __init__(self, *, name=None, **kw):
21 if name is None:
22 name = "Request handler %i" % reqseq()
23 super().__init__(name=name, **kw)
24
25class wsgirequest(object):
26 def __init__(self, handler):
552a70bf
FT
27 self.status = None
28 self.headers = []
29 self.respsent = False
46adc298
FT
30 self.handler = handler
31 self.buffer = bytearray()
552a70bf
FT
32
33 def handlewsgi(self):
34 raise Exception()
46adc298
FT
35 def fileno(self):
36 raise Exception()
552a70bf
FT
37 def writehead(self, status, headers):
38 raise Exception()
46adc298 39 def flush(self):
552a70bf 40 raise Exception()
46adc298
FT
41 def close(self):
42 pass
43 def writedata(self, data):
44 self.buffer.extend(data)
552a70bf
FT
45
46 def flushreq(self):
47 if not self.respsent:
48 if not self.status:
49 raise Exception("Cannot send response body before starting response.")
50 self.respsent = True
51 self.writehead(self.status, self.headers)
52
46adc298
FT
53 def write(self, data):
54 if not data:
55 return
56 self.flushreq()
57 self.writedata(data)
58 self.handler.ckflush(self)
59
552a70bf
FT
60 def startreq(self, status, headers, exc_info=None):
61 if self.status:
46adc298 62 if exc_info:
552a70bf
FT
63 try:
64 if self.respsent:
65 raise exc_info[1]
66 finally:
46adc298 67 exc_info = None
552a70bf
FT
68 else:
69 raise Exception("Can only start responding once.")
70 self.status = status
71 self.headers = headers
72 return self.write
46adc298
FT
73
74class handler(object):
75 def handle(self, request):
76 raise Exception()
77 def ckflush(self, req):
dd1e6b98
FT
78 while len(req.buffer) > 0:
79 rls, wls, els = select.select([], [req], [req])
80 req.flush()
46adc298
FT
81 def close(self):
82 pass
83
8db41888
FT
84 @classmethod
85 def parseargs(cls, **args):
86 if len(args) > 0:
87 raise ValueError("unknown handler argument: " + next(iter(args)))
88 return {}
89
dd1e6b98
FT
90class single(handler):
91 def handle(self, req):
92 try:
93 env = req.mkenv()
94 with perf.request(env) as reqevent:
95 respiter = req.handlewsgi(env, req.startreq)
96 for data in respiter:
97 req.write(data)
98 if req.status:
99 reqevent.response([req.status, req.headers])
100 req.flushreq()
101 self.ckflush(req)
102 except closed:
103 pass
104 except:
105 log.error("exception occurred when handling request", exc_info=True)
106 finally:
107 req.close()
108
46adc298 109class freethread(handler):
3a3b78e3 110 def __init__(self, *, max=None, timeout=None, **kw):
46adc298
FT
111 super().__init__(**kw)
112 self.current = set()
113 self.lk = threading.Lock()
002ee932
FT
114 self.tcond = threading.Condition(self.lk)
115 self.max = max
3a3b78e3 116 self.timeout = timeout
002ee932
FT
117
118 @classmethod
3a3b78e3 119 def parseargs(cls, *, max=None, abort=None, **args):
002ee932
FT
120 ret = super().parseargs(**args)
121 if max:
122 ret["max"] = int(max)
3a3b78e3
FT
123 if abort:
124 ret["timeout"] = int(abort)
002ee932 125 return ret
46adc298
FT
126
127 def handle(self, req):
002ee932 128 with self.lk:
3a3b78e3
FT
129 if self.max is not None:
130 if self.timeout is not None:
131 now = start = time.time()
132 while len(self.current) >= self.max:
133 self.tcond.wait(start + self.timeout - now)
134 now = time.time()
135 if now - start > self.timeout:
136 os.abort()
137 else:
138 while len(self.current) >= self.max:
139 self.tcond.wait()
002ee932
FT
140 th = reqthread(target=self.run, args=[req])
141 th.start()
142 while th.is_alive() and th not in self.current:
143 self.tcond.wait()
46adc298 144
46adc298 145 def run(self, req):
552a70bf 146 try:
46adc298
FT
147 th = threading.current_thread()
148 with self.lk:
149 self.current.add(th)
002ee932 150 self.tcond.notify_all()
552a70bf 151 try:
46adc298
FT
152 env = req.mkenv()
153 with perf.request(env) as reqevent:
154 respiter = req.handlewsgi(env, req.startreq)
155 for data in respiter:
156 req.write(data)
157 if req.status:
158 reqevent.response([req.status, req.headers])
159 req.flushreq()
160 self.ckflush(req)
161 except closed:
162 pass
163 except:
164 log.error("exception occurred when handling request", exc_info=True)
552a70bf 165 finally:
46adc298
FT
166 with self.lk:
167 self.current.remove(th)
002ee932 168 self.tcond.notify_all()
46adc298
FT
169 finally:
170 req.close()
171
172 def close(self):
173 while True:
174 with self.lk:
175 if len(self.current) > 0:
176 th = next(iter(self.current))
177 else:
8db41888 178 return
46adc298
FT
179 th.join()
180
d570c3a5 181class threadpool(handler):
8db41888 182 def __init__(self, *, min=0, max=20, live=300, **kw):
d570c3a5
FT
183 super().__init__(**kw)
184 self.current = set()
185 self.free = set()
186 self.lk = threading.RLock()
187 self.pcond = threading.Condition(self.lk)
188 self.rcond = threading.Condition(self.lk)
189 self.wreq = None
190 self.min = min
191 self.max = max
192 self.live = live
193 for i in range(self.min):
194 self.newthread()
195
8db41888
FT
196 @classmethod
197 def parseargs(cls, *, min=None, max=None, live=None, **args):
198 ret = super().parseargs(**args)
199 if min:
200 ret["min"] = int(min)
201 if max:
202 ret["max"] = int(max)
203 if live:
204 ret["live"] = int(live)
205 return ret
206
d570c3a5
FT
207 def newthread(self):
208 with self.lk:
209 th = reqthread(target=self.loop)
210 th.start()
211 while not th in self.current:
212 self.pcond.wait()
213
d570c3a5
FT
214 def _handle(self, req):
215 try:
216 env = req.mkenv()
217 with perf.request(env) as reqevent:
218 respiter = req.handlewsgi(env, req.startreq)
219 for data in respiter:
220 req.write(data)
221 if req.status:
222 reqevent.response([req.status, req.headers])
223 req.flushreq()
224 self.ckflush(req)
225 except closed:
226 pass
227 except:
228 log.error("exception occurred when handling request", exc_info=True)
229 finally:
230 req.close()
231
232 def loop(self):
233 th = threading.current_thread()
234 with self.lk:
235 self.current.add(th)
236 try:
237 while True:
238 with self.lk:
239 self.free.add(th)
240 try:
241 self.pcond.notify_all()
242 now = start = time.time()
243 while self.wreq is None:
244 self.rcond.wait(start + self.live - now)
245 now = time.time()
246 if now - start > self.live:
247 if len(self.current) > self.min:
248 self.current.remove(th)
249 return
250 else:
251 start = now
252 req, self.wreq = self.wreq, None
253 self.pcond.notify_all()
254 finally:
255 self.free.remove(th)
256 self._handle(req)
257 req = None
258 finally:
259 with self.lk:
260 try:
261 self.current.remove(th)
262 except KeyError:
263 pass
264 self.pcond.notify_all()
265
266 def handle(self, req):
267 while True:
268 with self.lk:
269 if len(self.free) < 1 and len(self.current) < self.max:
270 self.newthread()
271 while self.wreq is not None:
272 self.pcond.wait()
273 if self.wreq is None:
274 self.wreq = req
275 self.rcond.notify(1)
276 return
277
278 def close(self):
279 self.live = 0
280 self.min = 0
281 with self.lk:
282 while len(self.current) > 0:
283 self.rcond.notify_all()
284 self.pcond.wait(1)
285
dd1e6b98
FT
286names = {"single": single,
287 "free": freethread,
d570c3a5 288 "pool": threadpool}
8db41888
FT
289
290def parsehspec(spec):
291 if ":" not in spec:
292 return spec, {}
293 nm, spec = spec.split(":", 1)
294 args = {}
295 while spec:
296 if "," in spec:
297 part, spec = spec.split(",", 1)
298 else:
299 part, spec = spec, None
300 if "=" in part:
301 key, val = part.split("=", 1)
302 else:
303 key, val = part, ""
304 args[key] = val
305 return nm, args