234
234
self.assertReposEqual(clone, self._source_repo)
237
class ShutdownServerMixIn:
238
"""Mixin that allows serve_forever to be shut down.
240
The methods in this mixin are backported from SocketServer.py in the Python
241
2.6.4 standard library. The mixin is unnecessary in 2.6 and later, when
242
BaseServer supports the shutdown method directly.
246
self.__is_shut_down = threading.Event()
247
self.__serving = False
249
def serve_forever(self, poll_interval=0.5):
250
"""Handle one request at a time until shutdown.
252
Polls for shutdown every poll_interval seconds. Ignores
253
self.timeout. If you need to do periodic tasks, do them in
256
self.__serving = True
257
self.__is_shut_down.clear()
258
while self.__serving:
259
# XXX: Consider using another file descriptor or
260
# connecting to the socket to wake this up instead of
261
# polling. Polling reduces our responsiveness to a
262
# shutdown request and wastes cpu at all other times.
263
r, w, e = select.select([self], [], [], poll_interval)
265
self._handle_request_noblock()
266
self.__is_shut_down.set()
268
serve = serve_forever # override alias from TCPGitServer
271
"""Stops the serve_forever loop.
273
Blocks until the loop has finished. This must be called while
274
serve_forever() is running in another thread, or it will deadlock.
276
self.__serving = False
277
self.__is_shut_down.wait()
279
def handle_request(self):
280
"""Handle one request, possibly blocking.
282
Respects self.timeout.
284
# Support people who used socket.settimeout() to escape
285
# handle_request before self.timeout was available.
286
timeout = self.socket.gettimeout()
288
timeout = self.timeout
289
elif self.timeout is not None:
290
timeout = min(timeout, self.timeout)
291
fd_sets = select.select([self], [], [], timeout)
293
self.handle_timeout()
295
self._handle_request_noblock()
297
def _handle_request_noblock(self):
298
"""Handle one request, without blocking.
300
I assume that select.select has returned that the socket is
301
readable before this function was called, so there should be
302
no risk of blocking in get_request().
305
request, client_address = self.get_request()
308
if self.verify_request(request, client_address):
310
self.process_request(request, client_address)
312
self.handle_error(request, client_address)
313
self.close_request(request)
316
237
# TODO(dborowitz): Come up with a better way of testing various permutations of
317
238
# capabilities. The only reason it is the way it is now is that side-band-64k
318
239
# was only recently introduced into git-receive-pack.