Skip to content

Commit 0904b3d

Browse files
committed
Refactor AntigravityProvider to improve retry logic on 503 errors; update BrowserConfig to load executable path and connection timeout from environment variables
1 parent c450626 commit 0904b3d

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

g4f/Provider/needs_auth/Antigravity.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,14 +1190,15 @@ async def parse_sse_stream(stream: aiohttp.StreamReader) -> AsyncGenerator[Dict[
11901190
if not resp.ok:
11911191
if resp.status == 503:
11921192
try:
1193-
max_retry_delay = int(max([d.get("retryDelay", 0) for d in (await resp.json(content_type=None)).get("error", {}).get("details", [])]))
1193+
retry_delay = int(max([float(d.get("retryDelay", 0)) for d in (await resp.json(content_type=None)).get("error", {}).get("details", [])]))
11941194
except ValueError:
1195-
max_retry_delay = 30 # Default retry delay if not specified
1196-
debug.log(f"Received 503 error, retrying after {max_retry_delay}")
1197-
await asyncio.sleep(max_retry_delay)
1198-
resp = await session.post(url, json=req_body)
1199-
if not resp.ok:
1200-
debug.error(f"Retry after 503 failed with status {resp.status}")
1195+
retry_delay = 30 # Default retry delay if not specified
1196+
debug.log(f"Received 503 error, retrying after {retry_delay}")
1197+
if retry_delay <= 120:
1198+
await asyncio.sleep(retry_delay)
1199+
resp = await session.post(url, json=req_body)
1200+
if not resp.ok:
1201+
debug.error(f"Retry after 503 failed with status {resp.status}")
12011202
if not resp.ok:
12021203
if resp.status == 401:
12031204
raise MissingAuthError("Unauthorized (401) from Antigravity API")

g4f/cookies.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,19 @@ class BrowserConfig:
5454
port: int = None
5555
host: str = "127.0.0.1"
5656
impersonate: str = "chrome"
57+
executable_path: str = None
58+
connection_timeout: float = 0.25
5759

5860
@staticmethod
5961
async def stop_browser():
6062
return None
6163

62-
browser_executable_path: str = None
64+
@classmethod
65+
def load_from_env(cls):
66+
cls.port = os.environ.get("G4F_BROWSER_PORT", cls.port)
67+
cls.host = os.environ.get("G4F_BROWSER_HOST", cls.host)
68+
cls.executable_path = os.environ.get("G4F_BROWSER_EXECUTABLE_PATH", cls.executable_path)
69+
cls.connection_timeout = float(os.environ.get("G4F_BROWSER_CONNECTION_TIMEOUT", cls.connection_timeout))
6370

6471
COOKIE_DOMAINS = (
6572
".bing.com",
@@ -214,8 +221,7 @@ def read_cookie_files(dir_path: Optional[str] = None, domains_filter: Optional[L
214221

215222
AppConfig.load_from_env()
216223

217-
BrowserConfig.port = os.environ.get("G4F_BROWSER_PORT", BrowserConfig.port)
218-
BrowserConfig.host = os.environ.get("G4F_BROWSER_HOST", BrowserConfig.host)
224+
BrowserConfig.load_from_env()
219225
if BrowserConfig.port:
220226
BrowserConfig.port = int(BrowserConfig.port)
221227
debug.log(f"Using browser: {BrowserConfig.host}:{BrowserConfig.port}")

g4f/requests/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ async def get_nodriver(
167167
'Install "zendriver" and "platformdirs" package | pip install -U zendriver platformdirs')
168168
user_data_dir = user_config_dir(f"g4f-{user_data_dir}") if user_data_dir and has_platformdirs else None
169169
if browser_executable_path is None:
170-
browser_executable_path = BrowserConfig.browser_executable_path
170+
browser_executable_path = BrowserConfig.executable_path
171171
if browser_executable_path is None:
172172
try:
173173
browser_executable_path = find_executable()
@@ -216,7 +216,7 @@ async def get_nodriver(
216216
browser_executable_path=browser_executable_path,
217217
port=BrowserConfig.port,
218218
host=BrowserConfig.host,
219-
browser_connection_timeout=1,
219+
connection_timeout=BrowserConfig.connection_timeout,
220220
**kwargs
221221
)
222222
except FileNotFoundError as e:

0 commit comments

Comments
 (0)