Small performance improvements to ingress forwarding (#116457)

This commit is contained in:
J. Nick Koston 2024-04-30 11:43:58 -05:00 committed by GitHub
parent 1e63665bf2
commit ff104f54b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -177,11 +177,13 @@ class HassIOIngress(HomeAssistantView):
if maybe_content_type := result.headers.get(hdrs.CONTENT_TYPE): if maybe_content_type := result.headers.get(hdrs.CONTENT_TYPE):
content_type: str = (maybe_content_type.partition(";"))[0].strip() content_type: str = (maybe_content_type.partition(";"))[0].strip()
else: else:
content_type = result.content_type # default value according to RFC 2616
content_type = "application/octet-stream"
# Simple request # Simple request
if result.status in (204, 304) or ( if result.status in (204, 304) or (
content_length is not UNDEFINED content_length is not UNDEFINED
and (content_length_int := int(content_length or 0)) and (content_length_int := int(content_length))
<= MAX_SIMPLE_RESPONSE_SIZE <= MAX_SIMPLE_RESPONSE_SIZE
): ):
# Return Response # Return Response
@ -194,17 +196,17 @@ class HassIOIngress(HomeAssistantView):
zlib_executor_size=32768, zlib_executor_size=32768,
) )
if content_length_int > MIN_COMPRESSED_SIZE and should_compress( if content_length_int > MIN_COMPRESSED_SIZE and should_compress(
content_type or simple_response.content_type content_type
): ):
simple_response.enable_compression() simple_response.enable_compression()
return simple_response return simple_response
# Stream response # Stream response
response = web.StreamResponse(status=result.status, headers=headers) response = web.StreamResponse(status=result.status, headers=headers)
response.content_type = result.content_type response.content_type = content_type
try: try:
if should_compress(response.content_type): if should_compress(content_type):
response.enable_compression() response.enable_compression()
await response.prepare(request) await response.prepare(request)
# In testing iter_chunked, iter_any, and iter_chunks: # In testing iter_chunked, iter_any, and iter_chunks: