mstar.api_server.streaming_response

mstar.api_server.streaming_response#

Streaming-response helpers: a response that cleans up after a body Starlette never started, and a body that reports a mid-stream error in band.

Functions

errors_in_band(request_id, chunks)

chunks, with an HTTPException raised mid-stream (--timeout) ending it as the error chunk an engine error ends it with: the 200 is already sent, so raising would only cut the body off.

Classes

ReleasingStreamingResponse(content, *, ...)

A StreamingResponse that calls release when its body never started.

class mstar.api_server.streaming_response.ReleasingStreamingResponse(content, *, release, **kwargs)[source]#

Bases: StreamingResponse

A StreamingResponse that calls release when its body never started.

Starlette sends the headers before it iterates the body, and leaves the body unstarted when that send raises: uvicorn 0.28.0 (the release that reports ASGI 2.4) raises from send once the client has left. A server that cancels the response before its first step would do the same; current uvicorn does neither, so there the body starts and aborts the request itself, and this is a guard. An async generator that never started never runs its finally, so what the body releases on an early exit (its engine request, read-ahead pieces, uploaded files) would be held until the process restarts.

Parameters:
async mstar.api_server.streaming_response.errors_in_band(request_id, chunks)[source]#

chunks, with an HTTPException raised mid-stream (--timeout) ending it as the error chunk an engine error ends it with: the 200 is already sent, so raising would only cut the body off.

Parameters:

request_id (str)