[Serve] Performance: Use uvloop when possible (#9216)

This commit is contained in:
Simon Mo
2020-06-30 15:59:13 -07:00
committed by GitHub
parent 77933c922d
commit be647b69ab
8 changed files with 118 additions and 47 deletions
+10 -21
View File
@@ -1,5 +1,4 @@
import asyncio
import socket
import uvicorn
@@ -46,15 +45,6 @@ class HTTPProxy:
def set_route_table(self, route_table):
self.route_table = route_table
async def handle_lifespan_message(self, scope, receive, send):
assert scope["type"] == "lifespan"
message = await receive()
if message["type"] == "lifespan.startup":
await send({"type": "lifespan.startup.complete"})
elif message["type"] == "lifespan.shutdown":
await send({"type": "lifespan.shutdown.complete"})
async def receive_http_body(self, scope, receive, send):
body_buffer = []
more_body = True
@@ -116,10 +106,6 @@ class HTTPProxy:
# NOTE: This implements ASGI protocol specified in
# https://asgi.readthedocs.io/en/latest/specs/index.html
if scope["type"] == "lifespan":
await self.handle_lifespan_message(scope, receive, send)
return
error_sender = self._make_error_sender(scope, receive, send)
assert self.route_table is not None, (
@@ -202,18 +188,21 @@ class HTTPProxyActor:
asyncio.get_event_loop().create_task(self.run())
async def run(self):
sock = socket.socket()
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((self.host, self.port))
sock.set_inheritable(True)
config = uvicorn.Config(self.app, lifespan="on", access_log=False)
# Note(simon): we have to use lower level uvicorn Config and Server
# class because we want to run the server as a coroutine. The only
# alternative is to call uvicorn.run which is blocking.
config = uvicorn.Config(
self.app,
host=self.host,
port=self.port,
lifespan="off",
access_log=False)
server = uvicorn.Server(config=config)
# TODO(edoakes): we need to override install_signal_handlers here
# because the existing implementation fails if it isn't running in
# the main thread and uvicorn doesn't expose a way to configure it.
server.install_signal_handlers = lambda: None
await server.serve(sockets=[sock])
await server.serve()
async def set_route_table(self, route_table):
self.app.set_route_table(route_table)