Skip to content

Commit 6a5e73e

Browse files
committed
FIX: Fix excessive receive buffer allocation
1 parent ce58cd8 commit 6a5e73e

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

databento/live/client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ def __next__(self) -> DBNRecord:
145145
"yielding %s record from next",
146146
type(record).__name__,
147147
)
148-
self._dbn_queue.task_done()
149148
return record
150149
finally:
151150
if not self._dbn_queue.half_full() and not self._session.is_reading():

databento/live/protocol.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from databento.live.gateway import SubscriptionRequest
3232

3333

34-
MIN_BUFFER_SIZE: int = 64 * 1024 # 64kb
34+
RECV_BUFFER_SIZE: int = 64 * 2**10 # 64kb
3535

3636
logger = logging.getLogger(__name__)
3737

@@ -100,7 +100,7 @@ def __init__(
100100
) -> None:
101101
self.__api_key = api_key
102102
self.__transport: asyncio.Transport | None = None
103-
self.__buffer: bytearray
103+
self.__buffer: bytearray = bytearray(RECV_BUFFER_SIZE)
104104

105105
self._dataset = validate_semantic_string(dataset, "dataset")
106106
self._ts_out = ts_out
@@ -231,7 +231,8 @@ def get_buffer(self, sizehint: int) -> bytearray:
231231
asycnio.BufferedProtocol.get_buffer
232232
233233
"""
234-
self.__buffer = bytearray(max(sizehint, MIN_BUFFER_SIZE))
234+
if len(self.__buffer) < sizehint:
235+
self.__buffer = bytearray(sizehint)
235236
return self.__buffer
236237

237238
def buffer_updated(self, nbytes: int) -> None:

tests/test_live_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -822,9 +822,9 @@ async def test_live_stream_to_dbn_with_tiny_buffer(
822822
) -> None:
823823
"""
824824
Test that DBN data streamed by the MockLiveServer is properly re-
825-
constructed client side when using the small values for MIN_BUFFER_SIZE.
825+
constructed client side when using the small values for RECV_BUFFER_SIZE.
826826
"""
827-
monkeypatch.setattr(protocol, "MIN_BUFFER_SIZE", buffer_size)
827+
monkeypatch.setattr(protocol, "RECV_BUFFER_SIZE", buffer_size)
828828
output = tmp_path / "output.dbn"
829829

830830
live_client.subscribe(

0 commit comments

Comments
 (0)