Skip to content

Commit 4ad3399

Browse files
committed
fix(proxy): add retry logic for dropped connections during 2fa wait
1 parent eaec15b commit 4ad3399

1 file changed

Lines changed: 16 additions & 11 deletions

File tree

app/services/instagram_client.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import contextlib
44
import json
55
import secrets
6+
import time
67
from collections.abc import Callable, Coroutine, Mapping
78
from functools import wraps
89
from pathlib import Path
910
from typing import Any, cast
1011

1112
import requests.exceptions
13+
import urllib3.exceptions
1214
from cryptography.fernet import Fernet, InvalidToken
1315
from instagrapi import Client # type: ignore[reportMissingTypeStubs]
1416
from instagrapi.exceptions import ( # type: ignore[reportMissingTypeStubs]
@@ -70,9 +72,9 @@ async def wrapper(*args: Any, **kwargs: Any) -> T:
7072

7173

7274
class NoFallbackProxyAdapter(HTTPAdapter):
73-
"""Забороняє fallback на локальний IP, але дозволяє retry на SSL помилки."""
75+
"""Забороняє fallback на локальний IP, дозволяє retry на SSL та обриви з'єднання."""
7476

75-
MAX_SSL_RETRIES = 3
77+
MAX_RETRIES = 3
7678

7779
def send(
7880
self,
@@ -85,7 +87,7 @@ def send(
8587
) -> Response:
8688
last_error: Exception | None = None
8789

88-
for attempt in range(1, self.MAX_SSL_RETRIES + 1):
90+
for attempt in range(1, self.MAX_RETRIES + 1):
8991
try:
9092
return super().send(
9193
request,
@@ -95,22 +97,25 @@ def send(
9597
cert=cert,
9698
proxies=proxies,
9799
)
98-
except requests.exceptions.SSLError as e:
100+
except (
101+
requests.exceptions.SSLError,
102+
requests.exceptions.ProxyError,
103+
requests.exceptions.ConnectionError,
104+
urllib3.exceptions.ProtocolError,
105+
) as e:
99106
last_error = e
100-
logger.warning(f"SSL error on attempt {attempt}/{self.MAX_SSL_RETRIES}: {e}")
101-
if attempt == self.MAX_SSL_RETRIES:
107+
logger.warning(f"Network/Proxy error on attempt {attempt}/{self.MAX_RETRIES}: {e}")
108+
if attempt == self.MAX_RETRIES:
102109
raise RuntimeError(
103-
f"Proxy SSL failed after {self.MAX_SSL_RETRIES} attempts: {e}"
110+
f"Proxy connection failed after {self.MAX_RETRIES} attempts: {e}"
104111
) from e
105112

106-
import time
107-
108113
time.sleep(1.0 * attempt)
109114

110115
except Exception as e:
111-
raise RuntimeError(f"Proxy connection failed mid-request: {e}") from e
116+
raise RuntimeError(f"Unexpected connection error mid-request: {e}") from e
112117

113-
raise RuntimeError(f"Proxy SSL failed: {last_error}")
118+
raise RuntimeError(f"Proxy SSL/Connection failed: {last_error}")
114119

115120

116121
class InstagramClient:

0 commit comments

Comments
 (0)