Connect to Mongo and return a new AsyncConnection. Can raise ConnectionFailure. Note that the pool does not keep a reference to the socket -- you must call checkin() when you're done with it.
(self, handler: Optional[_MongoClientErrorHandler] = None)
| 1007 | error._add_error_label("RetryableError") |
| 1008 | |
| 1009 | async def connect(self, handler: Optional[_MongoClientErrorHandler] = None) -> AsyncConnection: |
| 1010 | """Connect to Mongo and return a new AsyncConnection. |
| 1011 | |
| 1012 | Can raise ConnectionFailure. |
| 1013 | |
| 1014 | Note that the pool does not keep a reference to the socket -- you |
| 1015 | must call checkin() when you're done with it. |
| 1016 | """ |
| 1017 | async with self.lock: |
| 1018 | conn_id = self.next_connection_id |
| 1019 | self.next_connection_id += 1 |
| 1020 | # Use a temporary context so that interrupt_connections can cancel creating the socket. |
| 1021 | tmp_context = _CancellationContext() |
| 1022 | self.active_contexts.add(tmp_context) |
| 1023 | |
| 1024 | listeners = self.opts._event_listeners |
| 1025 | if self.enabled_for_cmap: |
| 1026 | assert listeners is not None |
| 1027 | listeners.publish_connection_created(self.address, conn_id) |
| 1028 | if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG): |
| 1029 | _debug_log( |
| 1030 | _CONNECTION_LOGGER, |
| 1031 | message=_ConnectionStatusMessage.CONN_CREATED, |
| 1032 | clientId=self._client_id, |
| 1033 | serverHost=self.address[0], |
| 1034 | serverPort=self.address[1], |
| 1035 | driverConnectionId=conn_id, |
| 1036 | ) |
| 1037 | |
| 1038 | try: |
| 1039 | networking_interface = await _configured_protocol_interface(self.address, self.opts) |
| 1040 | # Catch KeyboardInterrupt, CancelledError, etc. and cleanup. |
| 1041 | except BaseException as error: |
| 1042 | async with self.lock: |
| 1043 | self.active_contexts.discard(tmp_context) |
| 1044 | if self.enabled_for_cmap: |
| 1045 | assert listeners is not None |
| 1046 | listeners.publish_connection_closed( |
| 1047 | self.address, conn_id, ConnectionClosedReason.ERROR |
| 1048 | ) |
| 1049 | if self.enabled_for_logging and _CONNECTION_LOGGER.isEnabledFor(logging.DEBUG): |
| 1050 | _debug_log( |
| 1051 | _CONNECTION_LOGGER, |
| 1052 | message=_ConnectionStatusMessage.CONN_CLOSED, |
| 1053 | clientId=self._client_id, |
| 1054 | serverHost=self.address[0], |
| 1055 | serverPort=self.address[1], |
| 1056 | driverConnectionId=conn_id, |
| 1057 | reason=_verbose_connection_error_reason(ConnectionClosedReason.ERROR), |
| 1058 | error=ConnectionClosedReason.ERROR, |
| 1059 | ) |
| 1060 | self._handle_connection_error(error) |
| 1061 | if isinstance(error, (IOError, OSError, *SSLErrors)): |
| 1062 | details = _get_timeout_details(self.opts) |
| 1063 | _raise_connection_failure(self.address, error, timeout_details=details) |
| 1064 | raise |
| 1065 | |
| 1066 | conn = AsyncConnection(networking_interface, self, self.address, conn_id, self.is_sdam) # type: ignore[arg-type] |
no test coverage detected