37
37
Union ,
38
38
)
39
39
40
- from asyncio import streams
41
40
from bson import DEFAULT_CODEC_OPTIONS
42
41
from pymongo import _csot , helpers_shared
43
42
from pymongo .asynchronous .client_session import _validate_session_write_concern
44
43
from pymongo .asynchronous .helpers import _handle_reauth
45
- from pymongo .asynchronous .network import command_stream , receive_message
44
+ from pymongo .asynchronous .network import command
46
45
from pymongo .common import (
47
46
MAX_BSON_SIZE ,
48
47
MAX_MESSAGE_SIZE ,
80
79
ConnectionCheckOutFailedReason ,
81
80
ConnectionClosedReason ,
82
81
)
83
- from pymongo .network_layer import async_sendall , _UNPACK_HEADER , PyMongoProtocol
82
+ from pymongo .network_layer import PyMongoProtocol , async_receive_message , async_sendall
84
83
from pymongo .pool_options import PoolOptions
85
84
from pymongo .read_preferences import ReadPreference
86
85
from pymongo .server_api import _add_to_command
@@ -534,7 +533,7 @@ async def command(
534
533
if self .op_msg_enabled :
535
534
self ._raise_if_not_writable (unacknowledged )
536
535
try :
537
- return await command_stream (
536
+ return await command (
538
537
self ,
539
538
dbname ,
540
539
spec ,
@@ -578,7 +577,6 @@ async def send_message(self, message: bytes, max_doc_size: int) -> None:
578
577
try :
579
578
await async_sendall (self .conn , message )
580
579
except BaseException as error :
581
- print (error )
582
580
self ._raise_connection_failure (error )
583
581
584
582
async def receive_message (self , request_id : Optional [int ]) -> Union [_OpReply , _OpMsg ]:
@@ -587,7 +585,7 @@ async def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _O
587
585
If any exception is raised, the socket is closed.
588
586
"""
589
587
try :
590
- return await receive_message (self , request_id , self .max_message_size )
588
+ return await async_receive_message (self , request_id , self .max_message_size )
591
589
except BaseException as error :
592
590
self ._raise_connection_failure (error )
593
591
@@ -795,7 +793,11 @@ class AsyncConnectionProtocol:
795
793
"""
796
794
797
795
def __init__ (
798
- self , conn : tuple [asyncio .BaseTransport , PyMongoProtocol ], pool : Pool , address : tuple [str , int ], id : int
796
+ self ,
797
+ conn : tuple [asyncio .BaseTransport , PyMongoProtocol ],
798
+ pool : Pool ,
799
+ address : tuple [str , int ],
800
+ id : int ,
799
801
):
800
802
self .pool_ref = weakref .ref (pool )
801
803
self .conn = conn
@@ -1066,7 +1068,7 @@ async def command(
1066
1068
if self .op_msg_enabled :
1067
1069
self ._raise_if_not_writable (unacknowledged )
1068
1070
try :
1069
- return await command_stream (
1071
+ return await command (
1070
1072
self ,
1071
1073
dbname ,
1072
1074
spec ,
@@ -1118,7 +1120,7 @@ async def receive_message(self, request_id: Optional[int]) -> Union[_OpReply, _O
1118
1120
If any exception is raised, the socket is closed.
1119
1121
"""
1120
1122
try :
1121
- return await receive_message (self , request_id , self .max_message_size )
1123
+ return await async_receive_message (self , request_id , self .max_message_size )
1122
1124
except BaseException as error :
1123
1125
self ._raise_connection_failure (error )
1124
1126
@@ -1316,8 +1318,6 @@ def __repr__(self) -> str:
1316
1318
)
1317
1319
1318
1320
1319
-
1320
-
1321
1321
def _create_connection (address : _Address , options : PoolOptions ) -> socket .socket :
1322
1322
"""Given (host, port) and PoolOptions, connect and return a socket object.
1323
1323
@@ -1400,15 +1400,23 @@ async def _configured_stream(
1400
1400
"""
1401
1401
sock = _create_connection (address , options )
1402
1402
ssl_context = options ._ssl_context
1403
+ timeout = sock .gettimeout ()
1403
1404
1404
1405
if ssl_context is None :
1405
- return await asyncio .get_running_loop ().create_connection (lambda : PyMongoProtocol (), sock = sock )
1406
+ return await asyncio .get_running_loop ().create_connection (
1407
+ lambda : PyMongoProtocol (timeout = timeout , buffer_size = 2 ** 16 ), sock = sock
1408
+ )
1406
1409
1407
1410
host = address [0 ]
1408
1411
try :
1409
1412
# We have to pass hostname / ip address to wrap_socket
1410
1413
# to use SSLContext.check_hostname.
1411
- transport , protocol = await asyncio .get_running_loop ().create_connection (lambda : PyMongoProtocol (), sock = sock , server_hostname = host , ssl = ssl_context )
1414
+ transport , protocol = await asyncio .get_running_loop ().create_connection (
1415
+ lambda : PyMongoProtocol (timeout = timeout , buffer_size = 2 ** 14 ),
1416
+ sock = sock ,
1417
+ server_hostname = host ,
1418
+ ssl = ssl_context ,
1419
+ )
1412
1420
except _CertificateError :
1413
1421
transport .close ()
1414
1422
# Raise _CertificateError directly like we do after match_hostname
@@ -1819,7 +1827,9 @@ async def remove_stale_sockets(self, reference_generation: int) -> None:
1819
1827
self .requests -= 1
1820
1828
self .size_cond .notify ()
1821
1829
1822
- async def connect (self , handler : Optional [_MongoClientErrorHandler ] = None ) -> AsyncConnectionProtocol :
1830
+ async def connect (
1831
+ self , handler : Optional [_MongoClientErrorHandler ] = None
1832
+ ) -> AsyncConnectionProtocol :
1823
1833
"""Connect to Mongo and return a new AsyncConnection.
1824
1834
1825
1835
Can raise ConnectionFailure.
@@ -1849,7 +1859,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
1849
1859
)
1850
1860
1851
1861
try :
1852
- sock = await _configured_stream (self .address , self .opts )
1862
+ transport , protocol = await _configured_stream (self .address , self .opts )
1853
1863
except BaseException as error :
1854
1864
async with self .lock :
1855
1865
self .active_contexts .discard (tmp_context )
@@ -1875,7 +1885,7 @@ async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> A
1875
1885
1876
1886
raise
1877
1887
1878
- conn = AsyncConnectionProtocol (sock , self , self .address , conn_id ) # type: ignore[arg-type]
1888
+ conn = AsyncConnectionProtocol (( transport , protocol ) , self , self .address , conn_id ) # type: ignore[arg-type]
1879
1889
async with self .lock :
1880
1890
self .active_contexts .add (conn .cancel_context )
1881
1891
self .active_contexts .discard (tmp_context )
0 commit comments