Skip to content

Commit 04ed4bd

Browse files
Use DEFAULT_CHUNK_SIZE global (aio-libs#12356) (aio-libs#12365)
(cherry picked from commit bec74bb)
1 parent b502ae6 commit 04ed4bd

23 files changed

Lines changed: 194 additions & 130 deletions

aiohttp/_websocket/writer.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from ..base_protocol import BaseProtocol
1010
from ..client_exceptions import ClientConnectionResetError
1111
from ..compression_utils import ZLibBackend, ZLibCompressor
12+
from ..helpers import DEFAULT_CHUNK_SIZE
1213
from .helpers import (
1314
MASK_LEN,
1415
MSG_SIZE,
@@ -21,8 +22,6 @@
2122
)
2223
from .models import WS_DEFLATE_TRAILING, WSMsgType
2324

24-
DEFAULT_LIMIT: Final[int] = 2**18
25-
2625
# WebSocket opcode boundary: opcodes 0-7 are data frames, 8-15 are control frames
2726
# Control frames (ping, pong, close) are never compressed
2827
WS_CONTROL_FRAME_OPCODE: Final[int] = 8
@@ -52,7 +51,7 @@ def __init__(
5251
transport: asyncio.Transport,
5352
*,
5453
use_mask: bool = False,
55-
limit: int = DEFAULT_LIMIT,
54+
limit: int = DEFAULT_CHUNK_SIZE,
5655
random: random.Random = random.Random(),
5756
compress: int = 0,
5857
notakeover: bool = False,

aiohttp/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
from .helpers import (
9292
_SENTINEL,
9393
DEBUG,
94+
DEFAULT_CHUNK_SIZE,
9495
EMPTY_BODY_METHODS,
9596
BasicAuth,
9697
TimeoutHandle,
@@ -333,7 +334,7 @@ def __init__(
333334
trust_env: bool = False,
334335
requote_redirect_url: bool = True,
335336
trace_configs: list[TraceConfig] | None = None,
336-
read_bufsize: int = 2**18,
337+
read_bufsize: int = DEFAULT_CHUNK_SIZE,
337338
max_line_size: int = 8190,
338339
max_field_size: int = 8190,
339340
max_headers: int = 128,
@@ -1294,7 +1295,7 @@ async def _ws_connect(
12941295

12951296
transport = conn.transport
12961297
assert transport is not None
1297-
reader = WebSocketDataQueue(conn_proto, 2**18, loop=self._loop)
1298+
reader = WebSocketDataQueue(conn_proto, DEFAULT_CHUNK_SIZE, loop=self._loop)
12981299
writer = WebSocketWriter(
12991300
conn_proto,
13001301
transport,

aiohttp/client_proto.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from .helpers import (
1414
_EXC_SENTINEL,
15+
DEFAULT_CHUNK_SIZE,
1516
EMPTY_BODY_STATUS_CODES,
1617
BaseTimerContext,
1718
set_exception,
@@ -230,7 +231,7 @@ def set_response_params(
230231
read_until_eof: bool = False,
231232
auto_decompress: bool = True,
232233
read_timeout: float | None = None,
233-
read_bufsize: int = 2**18,
234+
read_bufsize: int = DEFAULT_CHUNK_SIZE,
234235
timeout_ceil_threshold: float = 5,
235236
max_line_size: int = 8190,
236237
max_field_size: int = 8190,

aiohttp/compression_utils.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@
3434

3535

3636
MAX_SYNC_CHUNK_SIZE = 4096
37-
# Matches the max size we receive from sockets:
38-
# https://github.com/python/cpython/blob/1857a40807daeae3a1bf5efb682de9c9ae6df845/Lib/asyncio/selector_events.py#L766
39-
DEFAULT_MAX_DECOMPRESS_SIZE = 256 * 1024
4037

4138
# Unlimited decompression constants - different libraries use different conventions
4239
ZLIB_MAX_LENGTH_UNLIMITED = 0 # zlib uses 0 to mean unlimited

aiohttp/helpers.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858

5959
PY_311 = sys.version_info >= (3, 11)
6060

61+
# This is the default size/limit for several operations.
62+
# Matches the max size we receive from sockets:
63+
# https://github.com/python/cpython/blob/1857a40807daeae3a1bf5efb682de9c9ae6df845/Lib/asyncio/selector_events.py#L766
64+
DEFAULT_CHUNK_SIZE = 2**18 # 256 KiB
6165

6266
_T = TypeVar("_T")
6367
_S = TypeVar("_S")

aiohttp/http_parser.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from . import hdrs
2323
from .base_protocol import BaseProtocol
2424
from .compression_utils import (
25-
DEFAULT_MAX_DECOMPRESS_SIZE,
2625
HAS_BROTLI,
2726
HAS_ZSTD,
2827
BrotliDecompressor,
@@ -32,6 +31,7 @@
3231
from .helpers import (
3332
_EXC_SENTINEL,
3433
DEBUG,
34+
DEFAULT_CHUNK_SIZE,
3535
EMPTY_BODY_METHODS,
3636
EMPTY_BODY_STATUS_CODES,
3737
NO_EXTENSIONS,
@@ -817,7 +817,7 @@ def __init__(
817817
max_line_size: int = 8190,
818818
max_field_size: int = 8190,
819819
max_trailers: int = 128,
820-
limit: int = DEFAULT_MAX_DECOMPRESS_SIZE,
820+
limit: int = DEFAULT_CHUNK_SIZE,
821821
) -> None:
822822
self._length = 0
823823
self._paused = False
@@ -1074,7 +1074,7 @@ def __init__(
10741074
self,
10751075
out: StreamReader,
10761076
encoding: str | None,
1077-
max_decompress_size: int = DEFAULT_MAX_DECOMPRESS_SIZE,
1077+
max_decompress_size: int = DEFAULT_CHUNK_SIZE,
10781078
) -> None:
10791079
self.out = out
10801080
self.size = 0

aiohttp/multipart.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,15 @@
1414
from multidict import CIMultiDict, CIMultiDictProxy
1515

1616
from .abc import AbstractStreamWriter
17-
from .compression_utils import (
18-
DEFAULT_MAX_DECOMPRESS_SIZE,
19-
ZLibCompressor,
20-
ZLibDecompressor,
21-
)
17+
from .compression_utils import ZLibCompressor, ZLibDecompressor
2218
from .hdrs import (
2319
CONTENT_DISPOSITION,
2420
CONTENT_ENCODING,
2521
CONTENT_LENGTH,
2622
CONTENT_TRANSFER_ENCODING,
2723
CONTENT_TYPE,
2824
)
29-
from .helpers import CHAR, TOKEN, parse_mimetype, reify
25+
from .helpers import CHAR, DEFAULT_CHUNK_SIZE, TOKEN, parse_mimetype, reify
3026
from .http import HeadersParser
3127
from .http_exceptions import BadHttpMessage
3228
from .log import internal_logger
@@ -267,7 +263,7 @@ def __init__(
267263
*,
268264
subtype: str = "mixed",
269265
default_charset: str | None = None,
270-
max_decompress_size: int = DEFAULT_MAX_DECOMPRESS_SIZE,
266+
max_decompress_size: int = DEFAULT_CHUNK_SIZE,
271267
client_max_size: int = sys.maxsize,
272268
max_size_error_cls: type[Exception] = ValueError,
273269
) -> None:
@@ -640,7 +636,7 @@ async def as_bytes(self, encoding: str = "utf-8", errors: str = "strict") -> byt
640636

641637
async def write(self, writer: AbstractStreamWriter) -> None:
642638
field = self._value
643-
while chunk := await field.read_chunk(size=2**18):
639+
while chunk := await field.read_chunk(size=DEFAULT_CHUNK_SIZE):
644640
async for d in field.decode_iter(chunk):
645641
await writer.write(d)
646642

aiohttp/payload.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from .abc import AbstractStreamWriter
1818
from .helpers import (
1919
_SENTINEL,
20+
DEFAULT_CHUNK_SIZE,
2021
content_disposition_header,
2122
guess_filename,
2223
parse_mimetype,
@@ -43,7 +44,6 @@
4344
)
4445

4546
TOO_LARGE_BYTES_BODY: Final[int] = 2**20 # 1 MB
46-
READ_SIZE: Final[int] = 2**18 # 256 KiB
4747
_CLOSE_FUTURES: set[asyncio.Future[None]] = set()
4848

4949

@@ -491,7 +491,7 @@ def _read_and_available_len(
491491
492492
Args:
493493
remaining_content_len: Optional limit on how many bytes to read in this operation.
494-
If None, READ_SIZE will be used as the default chunk size.
494+
If None, DEFAULT_CHUNK_SIZE will be used as the default chunk size.
495495
496496
Returns:
497497
A tuple containing:
@@ -506,7 +506,11 @@ def _read_and_available_len(
506506
self._set_or_restore_start_position()
507507
size = self.size # Call size only once since it does I/O
508508
return size, self._value.read(
509-
min(READ_SIZE, size or READ_SIZE, remaining_content_len or READ_SIZE)
509+
min(
510+
DEFAULT_CHUNK_SIZE,
511+
size or DEFAULT_CHUNK_SIZE,
512+
remaining_content_len or DEFAULT_CHUNK_SIZE,
513+
)
510514
)
511515

512516
def _read(self, remaining_content_len: int | None) -> bytes:
@@ -515,7 +519,7 @@ def _read(self, remaining_content_len: int | None) -> bytes:
515519
516520
Args:
517521
remaining_content_len: Optional maximum number of bytes to read.
518-
If None, READ_SIZE will be used as the default chunk size.
522+
If None, DEFAULT_CHUNK_SIZE will be used as the default chunk size.
519523
520524
Returns:
521525
A chunk of bytes read from the file object, respecting the
@@ -525,7 +529,7 @@ def _read(self, remaining_content_len: int | None) -> bytes:
525529
the initial _read_and_available_len call has been made.
526530
527531
"""
528-
return self._value.read(remaining_content_len or READ_SIZE) # type: ignore[no-any-return]
532+
return self._value.read(remaining_content_len or DEFAULT_CHUNK_SIZE) # type: ignore[no-any-return]
529533

530534
@property
531535
def size(self) -> int | None:
@@ -628,9 +632,9 @@ async def write_with_length(
628632
None,
629633
self._read,
630634
(
631-
min(READ_SIZE, remaining_content_len)
635+
min(DEFAULT_CHUNK_SIZE, remaining_content_len)
632636
if remaining_content_len is not None
633-
else READ_SIZE
637+
else DEFAULT_CHUNK_SIZE
634638
),
635639
)
636640

@@ -756,7 +760,7 @@ def _read_and_available_len(
756760
757761
Args:
758762
remaining_content_len: Optional limit on how many bytes to read in this operation.
759-
If None, READ_SIZE will be used as the default chunk size.
763+
If None, DEFAULT_CHUNK_SIZE will be used as the default chunk size.
760764
761765
Returns:
762766
A tuple containing:
@@ -775,7 +779,11 @@ def _read_and_available_len(
775779
self._set_or_restore_start_position()
776780
size = self.size
777781
chunk = self._value.read(
778-
min(READ_SIZE, size or READ_SIZE, remaining_content_len or READ_SIZE)
782+
min(
783+
DEFAULT_CHUNK_SIZE,
784+
size or DEFAULT_CHUNK_SIZE,
785+
remaining_content_len or DEFAULT_CHUNK_SIZE,
786+
)
779787
)
780788
return size, chunk.encode(self._encoding) if self._encoding else chunk.encode()
781789

@@ -785,7 +793,7 @@ def _read(self, remaining_content_len: int | None) -> bytes:
785793
786794
Args:
787795
remaining_content_len: Optional maximum number of bytes to read.
788-
If None, READ_SIZE will be used as the default chunk size.
796+
If None, DEFAULT_CHUNK_SIZE will be used as the default chunk size.
789797
790798
Returns:
791799
A chunk of bytes read from the file object and encoded using the payload's
@@ -797,7 +805,7 @@ def _read(self, remaining_content_len: int | None) -> bytes:
797805
the specified encoding (or UTF-8 if none was provided).
798806
799807
"""
800-
chunk = self._value.read(remaining_content_len or READ_SIZE)
808+
chunk = self._value.read(remaining_content_len or DEFAULT_CHUNK_SIZE)
801809
return chunk.encode(self._encoding) if self._encoding else chunk.encode()
802810

803811
def decode(self, encoding: str = "utf-8", errors: str = "strict") -> str:
@@ -881,7 +889,7 @@ async def write_with_length(
881889
self._set_or_restore_start_position()
882890
loop_count = 0
883891
remaining_bytes = content_length
884-
while chunk := self._value.read(READ_SIZE):
892+
while chunk := self._value.read(DEFAULT_CHUNK_SIZE):
885893
if loop_count > 0:
886894
# Avoid blocking the event loop
887895
# if they pass a large BytesIO object

aiohttp/streams.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from .base_protocol import BaseProtocol
88
from .helpers import (
99
_EXC_SENTINEL,
10+
DEFAULT_CHUNK_SIZE,
1011
BaseTimerContext,
1112
TimerNoop,
1213
set_exception,
@@ -167,7 +168,7 @@ def __repr__(self) -> str:
167168
info.append("%d bytes" % self._size)
168169
if self._eof:
169170
info.append("eof")
170-
if self._low_water != 2**18: # default limit
171+
if self._low_water != DEFAULT_CHUNK_SIZE:
171172
info.append("low=%d high=%d" % (self._low_water, self._high_water))
172173
if self._waiter:
173174
info.append("w=%r" % self._waiter)

aiohttp/web_fileresponse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
from . import hdrs
2828
from .abc import AbstractStreamWriter
29-
from .helpers import ETAG_ANY, ETag, must_be_empty_body
29+
from .helpers import DEFAULT_CHUNK_SIZE, ETAG_ANY, ETag, must_be_empty_body
3030
from .typedefs import LooseHeaders, PathLike
3131
from .web_exceptions import (
3232
HTTPForbidden,
@@ -95,7 +95,7 @@ class FileResponse(StreamResponse):
9595
def __init__(
9696
self,
9797
path: PathLike,
98-
chunk_size: int = 256 * 1024,
98+
chunk_size: int = DEFAULT_CHUNK_SIZE,
9999
status: int = 200,
100100
reason: str | None = None,
101101
headers: LooseHeaders | None = None,

0 commit comments

Comments
 (0)