From 29c6f3c49fc5fdd013cac7fa7492332e2b5a8cc2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=2E=20K=C3=A4rkk=C3=A4inen?= Date: Wed, 26 Feb 2020 19:23:55 +0200 Subject: [PATCH] Entirely remove request body queue. --- sanic/asgi.py | 6 +--- sanic/request.py | 19 ++----------- sanic/server.py | 4 +-- tests/test_request_buffer_queue_size.py | 37 ------------------------- 4 files changed, 4 insertions(+), 62 deletions(-) delete mode 100644 tests/test_request_buffer_queue_size.py diff --git a/sanic/asgi.py b/sanic/asgi.py index 0234d7c2..6800b5a8 100644 --- a/sanic/asgi.py +++ b/sanic/asgi.py @@ -251,11 +251,7 @@ class ASGIApp: instance.transport, sanic_app, ) - - instance.request.stream = StreamBuffer( - sanic_app.config.REQUEST_BUFFER_QUEUE_SIZE, - protocol=instance - ) + instance.request.stream = StreamBuffer(protocol=instance) return instance diff --git a/sanic/request.py b/sanic/request.py index 5328fe70..bcfd6bec 100644 --- a/sanic/request.py +++ b/sanic/request.py @@ -47,17 +47,12 @@ class RequestParameters(dict): class StreamBuffer: - def __init__(self, buffer_size=100, protocol=None): - self._queue = asyncio.Queue(buffer_size) + def __init__(self, protocol=None): self._protocol = protocol async def read(self): """ Stop reading when gets None """ - if self._protocol: - return await self._protocol.stream_body() - payload = await self._queue.get() - self._queue.task_done() - return payload + return await self._protocol.stream_body() async def __aiter__(self): while True: @@ -66,16 +61,6 @@ class StreamBuffer: return yield data - async def put(self, payload): - await self._queue.put(payload) - - def is_full(self): - return self._queue.full() - - @property - def buffer_size(self): - return self._queue.maxsize - class Request: """Properties of an HTTP request such as URL, headers, etc.""" diff --git a/sanic/server.py b/sanic/server.py index a1bbd226..ba1945ef 100644 --- a/sanic/server.py +++ b/sanic/server.py @@ -266,9 +266,7 @@ class HttpProtocol(asyncio.Protocol): if headers.get(EXPECT_HEADER): self._status = Status.EXPECT self.expect_handler() - self.request.stream = StreamBuffer( - self.request_buffer_queue_size, protocol=self, - ) + self.request.stream = StreamBuffer(protocol=self) if body is True: self._request_chunked = True pos -= 2 # One CRLF stays in buffer diff --git a/tests/test_request_buffer_queue_size.py b/tests/test_request_buffer_queue_size.py deleted file mode 100644 index 8e42c79a..00000000 --- a/tests/test_request_buffer_queue_size.py +++ /dev/null @@ -1,37 +0,0 @@ -import io - -from sanic.response import text - - -data = "abc" * 10_000_000 - - -def test_request_buffer_queue_size(app): - default_buf_qsz = app.config.get("REQUEST_BUFFER_QUEUE_SIZE") - qsz = 1 - while qsz == default_buf_qsz: - qsz += 1 - app.config.REQUEST_BUFFER_QUEUE_SIZE = qsz - - @app.post("/post", stream=True) - async def post(request): - assert request.stream.buffer_size == qsz - print("request.stream.buffer_size =", request.stream.buffer_size) - - bio = io.BytesIO() - while True: - bdata = await request.stream.read() - if not bdata: - break - bio.write(bdata) - - head = bdata[:3].decode("utf-8") - tail = bdata[3:][-3:].decode("utf-8") - print(head, "...", tail) - - bio.seek(0) - return text(bio.read().decode("utf-8")) - - request, response = app.test_client.post("/post", data=data) - assert response.status == 200 - assert response.text == data