Skip to content

Commit

Permalink
🚨 Cover protocols/http/flow_control.py on mypy (#1061)
Browse files Browse the repository at this point in the history
Co-authored-by: euri10 <euri10@users.noreply.github.com>
  • Loading branch information
Kludex and euri10 committed Jun 1, 2021
1 parent b4b860c commit 5241b2e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ files =
uvicorn/__init__.py,
uvicorn/__main__.py,
uvicorn/subprocess.py,
uvicorn/protocols/http/flow_control.py,
uvicorn/supervisors/statreload.py,
uvicorn/supervisors/multiprocess.py

Expand Down
51 changes: 33 additions & 18 deletions uvicorn/protocols/http/flow_control.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import asyncio

from asgiref.typing import (
ASGIReceiveCallable,
ASGISendCallable,
HTTPResponseBodyEvent,
HTTPResponseStartEvent,
Scope,
)

CLOSE_HEADER = (b"connection", b"close")

HIGH_WATER_LIMIT = 65536
Expand All @@ -8,46 +16,53 @@


class FlowControl:
def __init__(self, transport):
def __init__(self, transport: asyncio.Transport) -> None:
self._transport = transport
self.read_paused = False
self.write_paused = False
self._is_writable_event = asyncio.Event()
self._is_writable_event.set()

async def drain(self):
async def drain(self) -> None:
await self._is_writable_event.wait()

def pause_reading(self):
def pause_reading(self) -> None:
if not self.read_paused:
self.read_paused = True
self._transport.pause_reading()

def resume_reading(self):
def resume_reading(self) -> None:
if self.read_paused:
self.read_paused = False
self._transport.resume_reading()

def pause_writing(self):
def pause_writing(self) -> None:
if not self.write_paused:
self.write_paused = True
self._is_writable_event.clear()

def resume_writing(self):
def resume_writing(self) -> None:
if self.write_paused:
self.write_paused = False
self._is_writable_event.set()


async def service_unavailable(scope, receive, send):
await send(
{
"type": "http.response.start",
"status": 503,
"headers": [
(b"content-type", b"text/plain; charset=utf-8"),
(b"connection", b"close"),
],
}
)
await send({"type": "http.response.body", "body": b"Service Unavailable"})
async def service_unavailable(
scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable
) -> None:
response_start: HTTPResponseStartEvent = {
"type": "http.response.start",
"status": 503,
"headers": [
(b"content-type", b"text/plain; charset=utf-8"),
(b"connection", b"close"),
],
}
await send(response_start)

response_body: HTTPResponseBodyEvent = {
"type": "http.response.body",
"body": b"Service Unavailable",
"more_body": False,
}
await send(response_body)

0 comments on commit 5241b2e

Please sign in to comment.