Skip to content

Commit

Permalink
[FIX] Default stream buffer size is now set to 16KiB
Browse files Browse the repository at this point in the history
  • Loading branch information
francis-clairicia committed Oct 28, 2023
1 parent e3c9bb9 commit 69d0374
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/easynetwork/api_async/client/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def __init__(

backend = AsyncBackendFactory.ensure(backend, backend_kwargs)
if max_recv_size is None:
max_recv_size = constants.MAX_STREAM_BUFSIZE
max_recv_size = constants.DEFAULT_STREAM_BUFSIZE
if not isinstance(max_recv_size, int) or max_recv_size <= 0:
raise ValueError("'max_recv_size' must be a strictly positive integer")

Expand Down
2 changes: 1 addition & 1 deletion src/easynetwork/api_async/server/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ def __init__(
log_client_connection = True

if max_recv_size is None:
max_recv_size = constants.MAX_STREAM_BUFSIZE
max_recv_size = constants.DEFAULT_STREAM_BUFSIZE
if not isinstance(max_recv_size, int) or max_recv_size <= 0:
raise ValueError("'max_recv_size' must be a strictly positive integer")

Expand Down
2 changes: 1 addition & 1 deletion src/easynetwork/api_sync/client/tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def __init__(
super().__init__()

if max_recv_size is None:
max_recv_size = constants.MAX_STREAM_BUFSIZE
max_recv_size = constants.DEFAULT_STREAM_BUFSIZE

if server_hostname is not None and not ssl:
raise ValueError("server_hostname is only meaningful with ssl")
Expand Down
4 changes: 2 additions & 2 deletions src/easynetwork/lowlevel/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
__all__ = [
"ACCEPT_CAPACITY_ERRNOS",
"ACCEPT_CAPACITY_ERROR_SLEEP_TIME",
"DEFAULT_STREAM_BUFSIZE",
"MAX_DATAGRAM_BUFSIZE",
"MAX_STREAM_BUFSIZE",
"NOT_CONNECTED_SOCKET_ERRNOS",
"SSL_HANDSHAKE_TIMEOUT",
"SSL_SHUTDOWN_TIMEOUT",
Expand All @@ -31,7 +31,7 @@
from typing import Final

# Buffer size for a recv(2) operation
MAX_STREAM_BUFSIZE: Final[int] = 256 * 1024 # 256KiB
DEFAULT_STREAM_BUFSIZE: Final[int] = 16 * 1024 # 16KiB

# Buffer size for a recvfrom(2) operation
MAX_DATAGRAM_BUFSIZE: Final[int] = 64 * 1024 # 64KiB
Expand Down
21 changes: 13 additions & 8 deletions tests/unit_test/test_async/test_api/test_client/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
from easynetwork.api_async.client.tcp import AsyncTCPNetworkClient
from easynetwork.exceptions import ClientClosedError, IncrementalDeserializeError, StreamProtocolParseError
from easynetwork.lowlevel._stream import StreamDataConsumer
from easynetwork.lowlevel.constants import CLOSED_SOCKET_ERRNOS, MAX_STREAM_BUFSIZE, SSL_HANDSHAKE_TIMEOUT, SSL_SHUTDOWN_TIMEOUT
from easynetwork.lowlevel.constants import (
CLOSED_SOCKET_ERRNOS,
DEFAULT_STREAM_BUFSIZE,
SSL_HANDSHAKE_TIMEOUT,
SSL_SHUTDOWN_TIMEOUT,
)
from easynetwork.lowlevel.socket import IPv4SocketAddress, IPv6SocketAddress, SocketProxy, _get_socket_extra
from easynetwork.lowlevel.typed_attr import TypedAttributeProvider

Expand Down Expand Up @@ -348,7 +353,7 @@ async def test____dunder_init____max_recv_size____valid_value(
mock_stream_protocol: MagicMock,
) -> None:
# Arrange
expected_size: int = max_recv_size if max_recv_size is not None else MAX_STREAM_BUFSIZE
expected_size: int = max_recv_size if max_recv_size is not None else DEFAULT_STREAM_BUFSIZE

# Act
client: AsyncTCPNetworkClient[Any, Any]
Expand Down Expand Up @@ -1175,7 +1180,7 @@ async def test____recv_packet____receive_bytes_from_socket(
packet: Any = await client_connected_or_not.recv_packet()

# Assert
mock_stream_socket_adapter.recv.assert_awaited_once_with(MAX_STREAM_BUFSIZE)
mock_stream_socket_adapter.recv.assert_awaited_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_called_once_with(b"packet\n")
assert packet is mocker.sentinel.packet

Expand All @@ -1196,7 +1201,7 @@ async def test____recv_packet____partial_data(

# Assert
mock_backend.coro_yield.assert_not_awaited()
assert mock_stream_socket_adapter.recv.call_args_list == [mocker.call(MAX_STREAM_BUFSIZE) for _ in range(2)]
assert mock_stream_socket_adapter.recv.call_args_list == [mocker.call(DEFAULT_STREAM_BUFSIZE) for _ in range(2)]
assert mock_stream_data_consumer.feed.call_args_list == [mocker.call(b"pac"), mocker.call(b"ket\n")]
assert packet is mocker.sentinel.packet

Expand Down Expand Up @@ -1239,7 +1244,7 @@ async def test____recv_packet____eof_error____default(
_ = await client_connected_or_not.recv_packet()

# Assert
mock_stream_socket_adapter.recv.assert_awaited_once_with(MAX_STREAM_BUFSIZE)
mock_stream_socket_adapter.recv.assert_awaited_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()
mock_backend.coro_yield.assert_not_awaited()

Expand All @@ -1263,7 +1268,7 @@ async def test____recv_packet____protocol_parse_error(
exception = exc_info.value

# Assert
mock_stream_socket_adapter.recv.assert_awaited_once_with(MAX_STREAM_BUFSIZE)
mock_stream_socket_adapter.recv.assert_awaited_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_called_once_with(b"packet\n")
mock_backend.coro_yield.assert_not_awaited()
assert exception is expected_error
Expand Down Expand Up @@ -1325,7 +1330,7 @@ async def test____recv_packet____convert_connection_errors(
_ = await client_connected_or_not.recv_packet()

# Assert
mock_stream_socket_adapter.recv.assert_awaited_once_with(MAX_STREAM_BUFSIZE)
mock_stream_socket_adapter.recv.assert_awaited_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()
mock_backend.coro_yield.assert_not_awaited()

Expand All @@ -1347,7 +1352,7 @@ async def test____recv_packet____convert_closed_socket_errors(
_ = await client_connected_or_not.recv_packet()

# Assert
mock_stream_socket_adapter.recv.assert_awaited_once_with(MAX_STREAM_BUFSIZE)
mock_stream_socket_adapter.recv.assert_awaited_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()
mock_backend.coro_yield.assert_not_awaited()

Expand Down
27 changes: 16 additions & 11 deletions tests/unit_test/test_sync/test_client/test_tcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
from easynetwork.api_sync.client.tcp import TCPNetworkClient
from easynetwork.exceptions import ClientClosedError, IncrementalDeserializeError
from easynetwork.lowlevel._stream import StreamDataConsumer
from easynetwork.lowlevel.constants import CLOSED_SOCKET_ERRNOS, MAX_STREAM_BUFSIZE, SSL_HANDSHAKE_TIMEOUT, SSL_SHUTDOWN_TIMEOUT
from easynetwork.lowlevel.constants import (
CLOSED_SOCKET_ERRNOS,
DEFAULT_STREAM_BUFSIZE,
SSL_HANDSHAKE_TIMEOUT,
SSL_SHUTDOWN_TIMEOUT,
)
from easynetwork.lowlevel.socket import IPv4SocketAddress, IPv6SocketAddress

import pytest
Expand Down Expand Up @@ -562,7 +567,7 @@ def test____dunder_init____max_size____valid_value(
server_hostname: Any | None,
) -> None:
# Arrange
expected_size: int = max_recv_size if max_recv_size is not None else MAX_STREAM_BUFSIZE
expected_size: int = max_recv_size if max_recv_size is not None else DEFAULT_STREAM_BUFSIZE

# Act
client: TCPNetworkClient[Any, Any]
Expand Down Expand Up @@ -1495,7 +1500,7 @@ def test____recv_packet____blocking_or_not____receive_bytes_from_socket(
mock_selector_register.assert_not_called()
mock_selector_select.assert_not_called()

mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_called_once_with(b"packet\n")
assert packet is mocker.sentinel.packet

Expand All @@ -1518,7 +1523,7 @@ def test____recv_packet____blocking____partial_data(
# Assert
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
assert mock_used_socket.recv.call_args_list == [mocker.call(MAX_STREAM_BUFSIZE) for _ in range(2)]
assert mock_used_socket.recv.call_args_list == [mocker.call(DEFAULT_STREAM_BUFSIZE) for _ in range(2)]
assert mock_stream_data_consumer.feed.call_args_list == [mocker.call(b"pac"), mocker.call(b"ket\n")]
assert packet is mocker.sentinel.packet

Expand Down Expand Up @@ -1603,7 +1608,7 @@ def test____recv_packet____blocking_or_not____eof_error____default(
# Assert
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()

@pytest.mark.usefixtures("setup_consumer_mock")
Expand All @@ -1624,7 +1629,7 @@ def test____recv_packet____blocking_or_not____eof_error____convert_connection_er
# Assert
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()

@pytest.mark.usefixtures("setup_consumer_mock")
Expand All @@ -1647,7 +1652,7 @@ def test____recv_packet____blocking_or_not____eof_error____convert_closed_socket
# Assert
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()

@pytest.mark.usefixtures("setup_consumer_mock")
Expand All @@ -1673,7 +1678,7 @@ def test____recv_packet____blocking_or_not____protocol_parse_error(
# Assert
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_called_once_with(b"packet\n")
assert exception is expected_error

Expand Down Expand Up @@ -1724,7 +1729,7 @@ def test____recv_packet____blocking_or_not____ssl____eof_error(
# Assert
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()

@pytest.mark.usefixtures("setup_consumer_mock")
Expand All @@ -1747,7 +1752,7 @@ def test____recv_packet____blocking_or_not____ssl____unrelated_ssl_error(
assert exc_info.value is mock_used_socket.recv.side_effect
mock_used_socket.settimeout.assert_not_called()
mock_used_socket.setblocking.assert_not_called()
mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_not_called()

@pytest.mark.parametrize(
Expand Down Expand Up @@ -1937,7 +1942,7 @@ def test____iter_received_packets____avoid_unnecessary_socket_recv_call(
packet_2 = next(iterator)

# Assert
mock_used_socket.recv.assert_called_once_with(MAX_STREAM_BUFSIZE)
mock_used_socket.recv.assert_called_once_with(DEFAULT_STREAM_BUFSIZE)
mock_stream_data_consumer.feed.assert_called_once_with(b"packet_1\npacket_2\n")
assert packet_1 is mocker.sentinel.packet_1
assert packet_2 is mocker.sentinel.packet_2
Expand Down

0 comments on commit 69d0374

Please sign in to comment.