send nice 503s and avoid scary stack traces...

... at shutdown
This commit is contained in:
Noah Levitt 2018-10-26 15:26:27 -07:00
parent 89212e782d
commit 52f2ac0f4e
3 changed files with 16 additions and 8 deletions

View File

@ -40,7 +40,7 @@ except:
setuptools.setup(
name='warcprox',
version='2.4b3.dev187',
version='2.4b3.dev188',
description='WARC writing MITM HTTP/S proxy',
url='https://github.com/internetarchive/warcprox',
author='Noah Levitt',

View File

@ -326,9 +326,9 @@ class WarcproxController(object):
to finish processing.
1. stop accepting new connections
2. shut down active connections to remote servers (resulting in sending
http 502 to the proxy clients)
3. shut down the postfetch processors one by one, in order, letting
2. shut down active connections to remote servers
3. send "503 warcprox shutting down" response to active requests
4. shut down the postfetch processors one by one, in order, letting
them finish process their queues
'''
with self._start_stop_lock:

View File

@ -384,10 +384,16 @@ class MitmProxyHandler(http_server.BaseHTTPRequestHandler):
try:
return self._proxy_request()
except Exception as e:
self.logger.error(
'error from remote server(?) %r: %r',
self.requestline, e, exc_info=True)
self.send_error(502, str(e))
if self.server.shutting_down:
self.logger.warn(
'sending 503 warcprox shutting down %r: %r',
self.requestline, e)
self.send_error(503, 'warcprox shutting down')
else:
self.logger.error(
'error from remote server(?) %r: %r',
self.requestline, e, exc_info=True)
self.send_error(502, str(e))
return
def send_error(self, code, message=None, explain=None):
@ -616,6 +622,7 @@ class PooledMitmProxy(PooledMixIn, MitmProxy):
PooledMixIn.__init__(self, options.max_threads)
MitmProxy.__init__(self)
self.profilers = collections.defaultdict(cProfile.Profile)
self.shutting_down = False
if options.profile:
self.process_request_thread = self._profile_process_request_thread
@ -648,6 +655,7 @@ class PooledMitmProxy(PooledMixIn, MitmProxy):
'''
Abort active connections to remote servers to achieve prompt shutdown.
'''
self.shutting_down = True
for sock in self.remote_server_socks:
self.shutdown_request(sock)