Remove aiohttp enable_compression helper (#104174)

This commit is contained in:
J. Nick Koston 2023-11-29 10:24:34 -07:00 committed by GitHub
parent 2287c45afc
commit dfed10420c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 24 deletions

View File

@ -41,7 +41,6 @@ from homeassistant.exceptions import (
Unauthorized, Unauthorized,
) )
from homeassistant.helpers import config_validation as cv, template from homeassistant.helpers import config_validation as cv, template
from homeassistant.helpers.aiohttp_compat import enable_compression
from homeassistant.helpers.event import EventStateChangedData from homeassistant.helpers.event import EventStateChangedData
from homeassistant.helpers.json import json_dumps from homeassistant.helpers.json import json_dumps
from homeassistant.helpers.service import async_get_all_descriptions from homeassistant.helpers.service import async_get_all_descriptions
@ -218,9 +217,11 @@ class APIStatesView(HomeAssistantView):
if entity_perm(state.entity_id, "read") if entity_perm(state.entity_id, "read")
) )
response = web.Response( response = web.Response(
body=f'[{",".join(states)}]', content_type=CONTENT_TYPE_JSON body=f'[{",".join(states)}]',
content_type=CONTENT_TYPE_JSON,
zlib_executor_size=32768,
) )
enable_compression(response) response.enable_compression()
return response return response

View File

@ -17,7 +17,6 @@ from yarl import URL
from homeassistant.components.http import HomeAssistantView from homeassistant.components.http import HomeAssistantView
from homeassistant.core import HomeAssistant, callback from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.aiohttp_client import async_get_clientsession from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.aiohttp_compat import enable_compression
from homeassistant.helpers.typing import UNDEFINED from homeassistant.helpers.typing import UNDEFINED
from .const import X_HASS_SOURCE, X_INGRESS_PATH from .const import X_HASS_SOURCE, X_INGRESS_PATH
@ -188,11 +187,12 @@ class HassIOIngress(HomeAssistantView):
status=result.status, status=result.status,
content_type=content_type, content_type=content_type,
body=body, body=body,
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 or simple_response.content_type
): ):
enable_compression(simple_response) simple_response.enable_compression()
await simple_response.prepare(request) await simple_response.prepare(request)
return simple_response return simple_response

View File

@ -20,7 +20,6 @@ import voluptuous as vol
from homeassistant import exceptions from homeassistant import exceptions
from homeassistant.const import CONTENT_TYPE_JSON from homeassistant.const import CONTENT_TYPE_JSON
from homeassistant.core import Context, HomeAssistant, is_callback from homeassistant.core import Context, HomeAssistant, is_callback
from homeassistant.helpers.aiohttp_compat import enable_compression
from homeassistant.helpers.json import ( from homeassistant.helpers.json import (
find_paths_unserializable_data, find_paths_unserializable_data,
json_bytes, json_bytes,
@ -72,8 +71,9 @@ class HomeAssistantView:
content_type=CONTENT_TYPE_JSON, content_type=CONTENT_TYPE_JSON,
status=int(status_code), status=int(status_code),
headers=headers, headers=headers,
zlib_executor_size=32768,
) )
enable_compression(response) response.enable_compression()
return response return response
def json_message( def json_message(

View File

@ -1,7 +1,7 @@
"""Helper to restore old aiohttp behavior.""" """Helper to restore old aiohttp behavior."""
from __future__ import annotations from __future__ import annotations
from aiohttp import web, web_protocol, web_server from aiohttp import web_protocol, web_server
class CancelOnDisconnectRequestHandler(web_protocol.RequestHandler): class CancelOnDisconnectRequestHandler(web_protocol.RequestHandler):
@ -23,19 +23,3 @@ def restore_original_aiohttp_cancel_behavior() -> None:
""" """
web_protocol.RequestHandler = CancelOnDisconnectRequestHandler # type: ignore[misc] web_protocol.RequestHandler = CancelOnDisconnectRequestHandler # type: ignore[misc]
web_server.RequestHandler = CancelOnDisconnectRequestHandler # type: ignore[misc] web_server.RequestHandler = CancelOnDisconnectRequestHandler # type: ignore[misc]
def enable_compression(response: web.Response) -> None:
"""Enable compression on the response."""
#
# Set _zlib_executor_size in the constructor once support for
# aiohttp < 3.9.0 is dropped
#
# We want large zlib payloads to be compressed in the executor
# to avoid blocking the event loop.
#
# 32KiB was chosen based on testing in production.
# aiohttp will generate a warning for payloads larger than 1MiB
#
response._zlib_executor_size = 32768 # pylint: disable=protected-access
response.enable_compression()