Skip to content

Add upload buffering, enabled by default on clients

Andrej Shadura requested to merge upload-buffering into main

Quart doesn’t yet support backpressure to handle situations when an application cannot handle the incoming data as fast as it’s received.

This happens, for example, when the proxy client run on the same machine as the worker, and a worker attempts to upload a multi-gigabyte build artifact. In this case, the artifact is consumed almost immediately, as the local connection is fast enough to allow this, but the upload to the upstream proxy doesn’t keep up and the artifact ends up being buffered in memory. If there’s not enough RAM and swap space, the upload terminates with an out of memory exception.

To work around this until this missing feature is implemented in Quart, we buffer incoming uploads at clients in a temporary files, as disk space is still cheaper than RAM at the time of writing.

Uploads on the server are not buffered on the server, as typically the upload speed isn’t high enough to cause excessive buffering.

This brings a new dependency on aiofiles which implements async temporary files, as using a normal tempfile functionality would block on I/O.

While httpx supports passing file-like objects as content directly, the current implementation doesn’t use them in an optimal way (despite a fix in https://github.com/encode/httpx/pull/1948), so instead of that, we read the temporary file chunk by chunk manually.

Edited by Andrej Shadura

Merge request reports