46
46
from pymongo .monitoring import _is_speculative_authenticate
47
47
from pymongo .network_layer import (
48
48
_UNPACK_COMPRESSION_HEADER ,
49
- _UNPACK_HEADER , async_sendall_stream ,
49
+ _UNPACK_HEADER , async_sendall , async_receive_data ,
50
50
)
51
51
52
52
if TYPE_CHECKING :
@@ -201,15 +201,15 @@ async def command_stream(
201
201
202
202
try :
203
203
write_start = time .monotonic ()
204
- await async_sendall_stream (conn , msg )
204
+ await async_sendall (conn , msg )
205
205
write_elapsed = time .monotonic () - write_start
206
206
if use_op_msg and unacknowledged :
207
207
# Unacknowledged, fake a successful command response.
208
208
reply = None
209
209
response_doc : _DocumentOut = {"ok" : 1 }
210
210
else :
211
211
read_start = time .monotonic ()
212
- reply = await receive_message_stream (conn , request_id )
212
+ reply = await receive_message (conn , request_id )
213
213
read_elapsed = time .monotonic () - read_start
214
214
# if name == "insert":
215
215
# TOTAL.append(write_elapsed + read_elapsed)
@@ -316,7 +316,7 @@ async def command_stream(
316
316
return response_doc # type: ignore[return-value]
317
317
318
318
319
- async def receive_message_stream (
319
+ async def receive_message (
320
320
conn : AsyncConnectionStream , request_id : Optional [int ], max_message_size : int = MAX_MESSAGE_SIZE
321
321
) -> Union [_OpReply , _OpMsg ]:
322
322
"""Receive a raw BSON message or raise socket.error."""
@@ -330,35 +330,27 @@ async def receive_message_stream(
330
330
# deadline = None
331
331
deadline = None
332
332
# Ignore the response's request id.
333
- # data = bytearray(max_message_size)
334
- conn .conn [1 ].reset ()
335
- # try:
336
- data , op_code = await asyncio .wait_for (conn .conn [1 ].read (), timeout = 5 )
337
- # except asyncio.TimeoutError:
338
- # print(f"Timed out on read in {asyncio.current_task()}. Start of reading memory at {conn.conn[1].ready_offset}, start of writing memory at {conn.conn[1].empty_offset}, max of {MAX_MESSAGE_SIZE}, messages: {conn.conn[1]._messages}")
339
-
340
-
341
- # length, _, response_to, op_code = _UNPACK_HEADER(await async_receive_data_stream(conn, 16, deadline))
342
- # # No request_id for exhaust cursor "getMore".
343
- # if request_id is not None:
344
- # if request_id != response_to:
345
- # raise ProtocolError(f"Got response id {response_to!r} but expected {request_id!r}")
346
- # if length <= 16:
347
- # raise ProtocolError(
348
- # f"Message length ({length!r}) not longer than standard message header size (16)"
349
- # )
350
- # if length > max_message_size:
351
- # raise ProtocolError(
352
- # f"Message length ({length!r}) is larger than server max "
353
- # f"message size ({max_message_size!r})"
354
- # )
355
- # if op_code == 2012:
356
- # op_code, _, compressor_id = _UNPACK_COMPRESSION_HEADER(
357
- # await async_receive_data_stream(conn, 9, deadline)
358
- # )
359
- # data = decompress(await async_receive_data_stream(conn, length - 25, deadline), compressor_id)
360
- # else:
361
- # data = await async_receive_data_stream(conn, length - 16, deadline)
333
+ length , _ , response_to , op_code = _UNPACK_HEADER (await async_receive_data (conn , 16 , deadline ))
334
+ # No request_id for exhaust cursor "getMore".
335
+ if request_id is not None :
336
+ if request_id != response_to :
337
+ raise ProtocolError (f"Got response id { response_to !r} but expected { request_id !r} " )
338
+ if length <= 16 :
339
+ raise ProtocolError (
340
+ f"Message length ({ length !r} ) not longer than standard message header size (16)"
341
+ )
342
+ if length > max_message_size :
343
+ raise ProtocolError (
344
+ f"Message length ({ length !r} ) is larger than server max "
345
+ f"message size ({ max_message_size !r} )"
346
+ )
347
+ if op_code == 2012 :
348
+ op_code , _ , compressor_id = _UNPACK_COMPRESSION_HEADER (
349
+ await async_receive_data (conn , 9 , deadline )
350
+ )
351
+ data = decompress (await async_receive_data (conn , length - 25 , deadline ), compressor_id )
352
+ else :
353
+ data = await async_receive_data (conn , length - 16 , deadline )
362
354
363
355
try :
364
356
unpack_reply = _UNPACK_REPLY [op_code ]
@@ -367,4 +359,3 @@ async def receive_message_stream(
367
359
f"Got opcode { op_code !r} but expected { _UNPACK_REPLY .keys ()!r} "
368
360
) from None
369
361
return unpack_reply (data )
370
-
0 commit comments