Skip to content

Commit 25323fd

Browse files
committed
CRF: use internal exception to mark the retry
1 parent a4731c8 commit 25323fd

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

asyncpg/connect_utils.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -624,26 +624,31 @@ async def _connect_addr(
624624
params_retry = params._replace(ssl=None)
625625
else:
626626
# skip retry if we don't have to
627-
return await __connect_addr(params, timeout, *args)
627+
return await __connect_addr(params, timeout, False, *args)
628628

629629
# first attempt
630630
before = time.monotonic()
631631
try:
632-
return await __connect_addr(params, timeout, *args)
633-
except ConnectionError:
632+
return await __connect_addr(params, timeout, True, *args)
633+
except _Retry:
634634
pass
635635

636636
# second attempt
637637
timeout -= time.monotonic() - before
638638
if timeout <= 0:
639639
raise asyncio.TimeoutError
640640
else:
641-
return await __connect_addr(params_retry, timeout, *args)
641+
return await __connect_addr(params_retry, timeout, False, *args)
642+
643+
644+
class _Retry(Exception):
645+
pass
642646

643647

644648
async def __connect_addr(
645649
params,
646650
timeout,
651+
retry,
647652
addr,
648653
loop,
649654
config,
@@ -681,17 +686,18 @@ async def __connect_addr(
681686
):
682687
tr.close()
683688

684-
if (
689+
# retry=True here is a redundant check because we don't want to
690+
# accidentally raise the internal _Retry to the outer world
691+
if retry and (
685692
params.sslmode == SSLMode.allow and not pr.is_ssl or
686693
params.sslmode == SSLMode.prefer and pr.is_ssl
687694
):
688-
# Elevate the error to ConnectionError to trigger retry when:
695+
# Trigger retry when:
689696
# 1. First attempt with sslmode=allow, ssl=None failed
690697
# 2. First attempt with sslmode=prefer, ssl=ctx failed while the
691698
# server claimed to support SSL (returning "S" for SSLRequest)
692699
# (likely because pg_hba.conf rejected the connection)
693-
raise ConnectionError("Connection rejected trying {} SSL".format(
694-
'with' if pr.is_ssl else 'without'))
700+
raise _Retry()
695701

696702
else:
697703
# but will NOT retry if:

0 commit comments

Comments
 (0)