Spaces:
Sleeping
Sleeping
Commit ·
baebc75
1
Parent(s): 7bb8ae9
proxy pool race condition fixed
Browse files- freeplay2api.py +19 -36
freeplay2api.py
CHANGED
|
@@ -167,7 +167,8 @@ class FreeplayClient:
|
|
| 167 |
"referer": "https://app.freeplay.ai/signup",
|
| 168 |
}
|
| 169 |
|
| 170 |
-
|
|
|
|
| 171 |
proxy_info = None
|
| 172 |
try:
|
| 173 |
# Wait for proxy pool to be ready
|
|
@@ -208,22 +209,25 @@ class FreeplayClient:
|
|
| 208 |
"balance": 5.0,
|
| 209 |
}
|
| 210 |
|
| 211 |
-
logging.warning(f"Registration attempt failed with status {response.status_code}: {response.text}")
|
| 212 |
|
| 213 |
except requests.exceptions.ProxyError as e:
|
| 214 |
-
logging.warning(f"Proxy error during registration: {e}. Retrying with a new proxy...")
|
| 215 |
if self.proxy_pool and proxy_info:
|
| 216 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 217 |
|
| 218 |
except requests.exceptions.RequestException as e:
|
| 219 |
-
logging.warning(f"Request exception during registration: {e}. Retrying...")
|
| 220 |
if self.proxy_pool and proxy_info:
|
| 221 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 222 |
|
| 223 |
except Exception as e:
|
| 224 |
-
logging.error(f"An unexpected error occurred during registration: {e}. Retrying...")
|
| 225 |
|
| 226 |
time.sleep(1)
|
|
|
|
|
|
|
|
|
|
| 227 |
|
| 228 |
def chat(
|
| 229 |
self,
|
|
@@ -345,42 +349,31 @@ class KeyMaintainer(threading.Thread):
|
|
| 345 |
logging.info("KeyMaintainer: Starting maintenance cycle.")
|
| 346 |
accounts = self.manager.get_all_accounts()
|
| 347 |
|
| 348 |
-
#
|
| 349 |
for account in accounts:
|
| 350 |
balance = self.client.check_balance(account["session_id"])
|
| 351 |
if balance != account.get("balance"):
|
| 352 |
account["balance"] = balance
|
| 353 |
self.manager.update_account(account)
|
| 354 |
-
logging.info(
|
| 355 |
-
f"Account {account['email']} balance updated to ${balance:.4f}"
|
| 356 |
-
)
|
| 357 |
|
| 358 |
-
#
|
| 359 |
healthy_accounts = [
|
| 360 |
-
acc
|
| 361 |
-
for acc in self.manager.get_all_accounts()
|
| 362 |
if acc.get("balance", 0) > self.config["LOW_BALANCE_THRESHOLD"]
|
| 363 |
]
|
| 364 |
needed = self.config["ACTIVE_KEY_THRESHOLD"] - len(healthy_accounts)
|
| 365 |
|
| 366 |
if needed > 0:
|
| 367 |
-
logging.info(
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
with concurrent.futures.ThreadPoolExecutor(
|
| 371 |
-
max_workers=self.config["REGISTRATION_CONCURRENCY"]
|
| 372 |
-
) as executor:
|
| 373 |
-
futures = [
|
| 374 |
-
executor.submit(self.client.register) for _ in range(needed)
|
| 375 |
-
]
|
| 376 |
for future in concurrent.futures.as_completed(futures):
|
| 377 |
new_account = future.result()
|
| 378 |
if new_account:
|
| 379 |
self.manager.add_account(new_account)
|
| 380 |
else:
|
| 381 |
-
logging.info(
|
| 382 |
-
f"Sufficient healthy accounts ({len(healthy_accounts)}). No registration needed."
|
| 383 |
-
)
|
| 384 |
|
| 385 |
except Exception as e:
|
| 386 |
logging.error(f"Error in KeyMaintainer cycle: {e}")
|
|
@@ -439,18 +432,8 @@ def initialize_app():
|
|
| 439 |
logging.info("Initializing proxy pool...")
|
| 440 |
proxy_pool_config = config.get("PROXY_POOL_CONFIG", {})
|
| 441 |
proxy_pool = ProxyPool(proxy_pool_config)
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
init_thread = threading.Thread(target=proxy_pool.initialize, daemon=True)
|
| 445 |
-
init_thread.start()
|
| 446 |
-
|
| 447 |
-
# Wait for the pool to be initialized
|
| 448 |
-
logging.info("Waiting for proxy pool to complete initial refill...")
|
| 449 |
-
while not proxy_pool.is_initialized:
|
| 450 |
-
time.sleep(1)
|
| 451 |
-
init_thread.join() # Ensure initialization thread is complete
|
| 452 |
-
logging.info("Proxy pool initialization process finished.")
|
| 453 |
-
|
| 454 |
else:
|
| 455 |
logging.info("Proxy pool is disabled in config.")
|
| 456 |
|
|
|
|
| 167 |
"referer": "https://app.freeplay.ai/signup",
|
| 168 |
}
|
| 169 |
|
| 170 |
+
max_retries = 5
|
| 171 |
+
for attempt in range(max_retries):
|
| 172 |
proxy_info = None
|
| 173 |
try:
|
| 174 |
# Wait for proxy pool to be ready
|
|
|
|
| 209 |
"balance": 5.0,
|
| 210 |
}
|
| 211 |
|
| 212 |
+
logging.warning(f"Registration attempt {attempt + 1}/{max_retries} failed with status {response.status_code}: {response.text}")
|
| 213 |
|
| 214 |
except requests.exceptions.ProxyError as e:
|
| 215 |
+
logging.warning(f"Proxy error during registration on attempt {attempt + 1}/{max_retries}: {e}. Retrying with a new proxy...")
|
| 216 |
if self.proxy_pool and proxy_info:
|
| 217 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 218 |
|
| 219 |
except requests.exceptions.RequestException as e:
|
| 220 |
+
logging.warning(f"Request exception during registration on attempt {attempt + 1}/{max_retries}: {e}. Retrying...")
|
| 221 |
if self.proxy_pool and proxy_info:
|
| 222 |
self.proxy_pool.remove_proxy(proxy_info['ip'], proxy_info['port'])
|
| 223 |
|
| 224 |
except Exception as e:
|
| 225 |
+
logging.error(f"An unexpected error occurred during registration on attempt {attempt + 1}/{max_retries}: {e}. Retrying...")
|
| 226 |
|
| 227 |
time.sleep(1)
|
| 228 |
+
|
| 229 |
+
logging.error("Failed to register a new account after multiple retries.")
|
| 230 |
+
return None
|
| 231 |
|
| 232 |
def chat(
|
| 233 |
self,
|
|
|
|
| 349 |
logging.info("KeyMaintainer: Starting maintenance cycle.")
|
| 350 |
accounts = self.manager.get_all_accounts()
|
| 351 |
|
| 352 |
+
# Update balances
|
| 353 |
for account in accounts:
|
| 354 |
balance = self.client.check_balance(account["session_id"])
|
| 355 |
if balance != account.get("balance"):
|
| 356 |
account["balance"] = balance
|
| 357 |
self.manager.update_account(account)
|
| 358 |
+
logging.info(f"Account {account['email']} balance updated to ${balance:.4f}")
|
|
|
|
|
|
|
| 359 |
|
| 360 |
+
# Check if new accounts are needed
|
| 361 |
healthy_accounts = [
|
| 362 |
+
acc for acc in self.manager.get_all_accounts()
|
|
|
|
| 363 |
if acc.get("balance", 0) > self.config["LOW_BALANCE_THRESHOLD"]
|
| 364 |
]
|
| 365 |
needed = self.config["ACTIVE_KEY_THRESHOLD"] - len(healthy_accounts)
|
| 366 |
|
| 367 |
if needed > 0:
|
| 368 |
+
logging.info(f"Healthy accounts ({len(healthy_accounts)}) below threshold. Need to register {needed} new accounts.")
|
| 369 |
+
with concurrent.futures.ThreadPoolExecutor(max_workers=self.config["REGISTRATION_CONCURRENCY"]) as executor:
|
| 370 |
+
futures = [executor.submit(self.client.register) for _ in range(needed)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
for future in concurrent.futures.as_completed(futures):
|
| 372 |
new_account = future.result()
|
| 373 |
if new_account:
|
| 374 |
self.manager.add_account(new_account)
|
| 375 |
else:
|
| 376 |
+
logging.info(f"Sufficient healthy accounts ({len(healthy_accounts)}).")
|
|
|
|
|
|
|
| 377 |
|
| 378 |
except Exception as e:
|
| 379 |
logging.error(f"Error in KeyMaintainer cycle: {e}")
|
|
|
|
| 432 |
logging.info("Initializing proxy pool...")
|
| 433 |
proxy_pool_config = config.get("PROXY_POOL_CONFIG", {})
|
| 434 |
proxy_pool = ProxyPool(proxy_pool_config)
|
| 435 |
+
# The initialize method will now run in the background
|
| 436 |
+
threading.Thread(target=proxy_pool.initialize, daemon=True).start()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 437 |
else:
|
| 438 |
logging.info("Proxy pool is disabled in config.")
|
| 439 |
|