MCPcopy Create free account
hub / github.com/MagicStack/asyncpg / _connect

Function _connect

asyncpg/connect_utils.py:1265–1310  ·  view source on GitHub ↗
(*, loop, connection_class, record_class, **kwargs)

Source from the content-addressed store, hash-verified

1263
1264
1265async def _connect(*, loop, connection_class, record_class, **kwargs):
1266 if loop is None:
1267 loop = asyncio.get_event_loop()
1268
1269 addrs, params, config = _parse_connect_arguments(**kwargs)
1270 target_attr = params.target_session_attrs
1271
1272 candidates = []
1273 chosen_connection = None
1274 last_error = None
1275 try:
1276 for addr in addrs:
1277 try:
1278 conn = await _connect_addr(
1279 addr=addr,
1280 loop=loop,
1281 params=params,
1282 config=config,
1283 connection_class=connection_class,
1284 record_class=record_class,
1285 )
1286 candidates.append(conn)
1287 if await _can_use_connection(conn, target_attr):
1288 chosen_connection = conn
1289 break
1290 except OSError as ex:
1291 last_error = ex
1292 except _NextHostSignal as ex:
1293 last_error = ex.__cause__
1294 else:
1295 if target_attr == SessionAttribute.prefer_standby and candidates:
1296 chosen_connection = random.choice(candidates)
1297 finally:
1298 # Do not wait for rejected hosts to disconnect: their cleanup must
1299 # neither delay a successful connection nor outlive the event loop.
1300 for c in candidates:
1301 if c is not chosen_connection:
1302 c.terminate()
1303
1304 if chosen_connection:
1305 return chosen_connection
1306
1307 raise last_error or exceptions.TargetServerAttributeNotMatched(
1308 'None of the hosts match the target attribute requirement '
1309 '{!r}'.format(target_attr)
1310 )
1311
1312
1313async def _cancel(*, loop, addr, params: _ConnectionParameters,

Callers

nothing calls this directly

Calls 4

_parse_connect_argumentsFunction · 0.85
_connect_addrFunction · 0.85
_can_use_connectionFunction · 0.85
terminateMethod · 0.45

Tested by

no test coverage detected